Created
March 16, 2018 08:31
-
-
Save kevincobain2000/97b07a19aa2e9cf6dc92ab42ecde9396 to your computer and use it in GitHub Desktop.
CodeSniffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Illuminate\Console\Command; | |
class Codesniffer extends Command | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
protected $name = "code-sniffer"; | |
/** | |
* {@inheritdoc} | |
*/ | |
protected $description = "CodeSniffer sniffs only modified php files"; | |
/** | |
* @return void | |
*/ | |
public function fire() | |
{ | |
$command = "git diff --name-only --diff-filter=ACMR origin/develop...HEAD"; | |
$command .= "| egrep -v 'database/|resources/|provision/'"; | |
$command .= "| grep .php"; | |
exec($command, $output); | |
$this->grepDiffForDebugCode(); | |
// remain the same | |
if (empty($output)) { | |
$this->info("CodeSniffer has nothing to do."); | |
return; | |
} | |
$this->info("CodeSniffer sniffs files below...\n\n" . implode("\n", $output) . "\n---\n"); | |
$targetFiles = implode(" ", $output); | |
$command = "vendor/bin/phpcs --standard=./phpcs.xml " . $targetFiles; | |
exec($command, $output, $return); | |
if ($return === 0) { | |
$this->info("Your codes are based on PSR-2, Yeah."); | |
return; | |
} else { | |
$this->error(implode("\n", $output)); | |
exit(1); | |
} | |
} | |
private function grepDiffForDebugCode() | |
{ | |
$filesCmd = 'git diff --name-only|grep -v Codesniffer.php'; | |
exec($filesCmd, $files); | |
foreach ($files as $file) { | |
exec("git diff $file", $out); | |
if (count($out)) { | |
$this->info("Debug Code found in $file"); | |
exit(1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment