-
Notifications
You must be signed in to change notification settings - Fork 15
Support ruleset as argument #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
039f3a2
368084f
a769fed
dcb527c
b94d762
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,6 +11,8 @@ | |||||
|
||||||
class PhpcsDiff | ||||||
{ | ||||||
const DEFAULT_RULESET = 'ruleset.xml'; | ||||||
|
||||||
/** | ||||||
* @var array | ||||||
*/ | ||||||
|
@@ -55,18 +57,43 @@ public function __construct(array $argv, CLImate $climate) | |||||
$this->isVerbose = true; | ||||||
} | ||||||
|
||||||
if (!isset($this->argv[1])) { | ||||||
$this->error('Please provide a <bold>base branch</bold> as the first argument.'); | ||||||
return; | ||||||
$this->ruleset = $this->getOption('--ruleset', self::DEFAULT_RULESET); | ||||||
|
||||||
$this->baseBranch = $this->getArgument(0, ''); | ||||||
$this->currentBranch = $this->getArgument(1, ''); | ||||||
|
||||||
if ($this->isVerbose) { | ||||||
if (!$this->baseBranch && !$this->currentBranch) { | ||||||
$this->climate->comment("Comparing unstaged changes."); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use single quotes |
||||||
} else { | ||||||
$this->climate->comment( | ||||||
'Comparing branch: "' . $this->baseBranch . '" against: "' . $this->currentBranch . '"' | ||||||
); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
$this->baseBranch = 'origin/' . str_replace('origin/', '', $this->argv[1]); | ||||||
$this->currentBranch = trim(shell_exec('git rev-parse --verify HEAD')); | ||||||
protected function getArgument($index, $default = null) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please could we add some docblocks to keep the class consistent with the other methods? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing! |
||||||
{ | ||||||
$arguments = array_filter($this->argv, function ($val) { | ||||||
return strpos($val, '-') === false; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we change this to use yoda-style conditions?
Suggested change
|
||||||
}); | ||||||
$arguments = array_values($arguments); | ||||||
|
||||||
if (empty($this->currentBranch)) { | ||||||
$this->error('Unable to get <bold>current</bold> branch.'); | ||||||
return; | ||||||
return isset($arguments[$index]) ? $arguments[$index] : $default; | ||||||
} | ||||||
|
||||||
protected function getOption($name, $default = null) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, some dockblocks would help keep the class consistent. |
||||||
{ | ||||||
foreach ($this->argv as $arg) { | ||||||
list($flag, $val) = explode('=', $arg); | ||||||
|
||||||
if ($flag == $name) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use strict-type checking
Suggested change
|
||||||
return $val; | ||||||
} | ||||||
} | ||||||
|
||||||
return $default; | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -75,19 +102,7 @@ public function __construct(array $argv, CLImate $climate) | |||||
*/ | ||||||
protected function isFlagSet($flag) | ||||||
{ | ||||||
$isFlagSet = false; | ||||||
$argv = $this->argv; | ||||||
|
||||||
$key = array_search($flag, $argv, true); | ||||||
if (false !== $key) { | ||||||
unset($argv[$key]); | ||||||
$argv = array_values($argv); | ||||||
|
||||||
$isFlagSet = true; | ||||||
} | ||||||
|
||||||
$this->argv = $argv; | ||||||
return $isFlagSet; | ||||||
return in_array($flag, array_values($this->argv)); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -112,7 +127,6 @@ public function getExitCode() | |||||
|
||||||
/** | ||||||
* @todo Automatically look at server envs for the travis base branch, if not provided? | ||||||
* @todo Define custom ruleset from command line argv for runPhpcs() | ||||||
*/ | ||||||
public function run() | ||||||
{ | ||||||
|
@@ -133,12 +147,11 @@ public function run() | |||||
if ($this->isVerbose) { | ||||||
$fileDiffCount = count($fileDiff); | ||||||
$this->climate->comment( | ||||||
'Checking ' . $fileDiffCount . ' ' . | ||||||
ngettext('file', 'files', $fileDiffCount) . ' for violations.' | ||||||
'Checking ' . $fileDiffCount . ' file(s) for violations.' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be able to fit on the same line now. |
||||||
); | ||||||
} | ||||||
|
||||||
$phpcsOutput = $this->runPhpcs($fileDiff); | ||||||
$phpcsOutput = $this->runPhpcs($fileDiff, $this->ruleset); | ||||||
|
||||||
if (is_null($phpcsOutput)) { | ||||||
$this->error('Unable to run phpcs executable.'); | ||||||
|
@@ -248,17 +261,19 @@ protected function getChangedFiles() | |||||
protected function getChangedLinesPerFile(array $files) | ||||||
{ | ||||||
$extract = []; | ||||||
$pattern = '@@ -[0-9]+(?:,[0-9]+)? \+([0-9]+)(?:,([0-9]+))? @@'; | ||||||
$pattern = '/@@ -[0-9]+(?:,[0-9]+)? \+([0-9]+)(?:,([0-9]+))? @@/'; | ||||||
|
||||||
foreach ($files as $file => $data) { | ||||||
$command = 'git diff -U0 ' . $this->baseBranch . ' ' . $this->currentBranch . ' ' . $file . | ||||||
' | grep -P ' . escapeshellarg($pattern); | ||||||
$lineDiff = shell_exec($command); | ||||||
$lines = array_filter(explode(PHP_EOL, $lineDiff)); | ||||||
$command = 'git diff -U0 ' . $this->baseBranch . ' ' . $this->currentBranch . ' ' . $file; | ||||||
$output = shell_exec($command); | ||||||
preg_match_all($pattern, $output, $lineDiff); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The However, I do agree that this feels like a cleaner approach, but I would prioritise functionality before clean code. If that helps explain why it was done in the first place, do you think a comment to explain why would suffice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand. The reason I replaced the current implementation is due to the differences in flags between grep on OSX/bsd and GNU. Running it on OSX gave an error. Maybe we can work on providing strategies that can be configured (ie, use grep, use preg_match)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that sounds like a great idea. That, or we can automatically detect the environment to decide on the method or command to use. |
||||||
$lines = array_values($lineDiff[0]); | ||||||
|
||||||
$linesChanged = []; | ||||||
|
||||||
foreach ($lines as $line) { | ||||||
preg_match('/' . $pattern . '/', $line, $matches); | ||||||
preg_match($pattern, $line, $matches); | ||||||
|
||||||
|
||||||
$start = $end = (int)$matches[1]; | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather this option be added as a feature, rather than a breaking change to existing functionality.
With your changes, we will have 3 variations:
phpcs-diff
- Run a diff against unstaged changes.phpcs-diff origin/master
- Run a diff against the base branch (defined) and the current branch (determined bygit rev-parse --verify HEAD
)phpcs-diff origin/ master master
- Run a diff against the base branch and current branch (both defined)My only ask here is to keep the functionality of 1.