Skip to content

Instantly share code, notes, and snippets.

@erunion
Last active August 29, 2015 14:13
Show Gist options
  • Save erunion/9ae01c39652950ad4be1 to your computer and use it in GitHub Desktop.
Save erunion/9ae01c39652950ad4be1 to your computer and use it in GitHub Desktop.
PHP linter command for Laravel 4.2.*
<?php
class Lint extends \Illuminate\Console\Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'command:lint';
/**
* The console command description.
*
* @var string
*/
protected $description = 'PHP code linter.';
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$this->check_php(dirname(dirname(dirname(__FILE__))));
}
/**
* Checks syntax of PHP scripts.
* @param $directory The directory of files to recursively process.
* @return void
*/
private function check_php($directory)
{
$dir = opendir($directory);
while (false !== ($file = readdir($dir))) {
if (in_array($file, array('.', '..', 'public', 'vendor')) || substr($file, 0, 1) == '.') {
continue;
}
$filename = $directory . '/' . $file;
if (!is_dir($filename) && strtolower(substr($file, -4)) == ".php") {
$result = shell_exec('php -l ' . $filename);
if (strpos($result, 'No syntax errors detected in ' . $filename) === false) {
echo $result;
exit(2);
}
}
if (is_dir($filename)) {
$this->check_php($filename);
}
}
}
}
@erunion
Copy link
Author

erunion commented Jan 15, 2015

  1. Drop LintCommand into your app/commands folder.
  2. Add Artisan::add(new LintCommand); to app/start/artisan.php.
  3. Lint your code by running php artisan command:lint.
$ php artisan command:lint

Parse error: syntax error, unexpected end of file, expecting function (T_FUNCTION) in 
/home/user/code/app/controllers/BaseController.php on line 23

Errors parsing /home/user/code/app/controllers/BaseController.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment