Skip to content

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

Closed

Conversation

desyncr
Copy link

@desyncr desyncr commented Jul 7, 2019

Added support for ruleset option and defining branches

  • Add support for passing ruleset as option/argument

Facilitates working with custom ruleset / experiment on rules.

  • Add support for passing branches (base and current) as arguments

Now it's feasible to run php-cs / php-cs-diff against unstaged changes (neat) and customize branching options.

  • Added helper methods to handle flags, options and arguments

  • Remove usage of ngettext as extension may not be present and it's overkill

  • Remove usage of grep binary and use preg_* extension instead

desyncr added 5 commits July 7, 2019 03:48
- Add support for passing ruleset as option/argument

Facilitates working with custom ruleset / experiment on rules.

- Add support for passing branches (base and current) as arguments

Now it's feasible to run php-cs / php-cs-diff against unstaged changes (neat)
and customize branching options.

- Added helper methods to handle flags, options and arguments
- Remove usage of ngettext as extension may not be present and it's overkill
@olivertappin olivertappin self-requested a review July 24, 2019 13:47
Copy link
Owner

@olivertappin olivertappin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution 🎉

Great work 💪 I've left a few changes and comments for your review.

$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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The grep command was used originally (within the shell_exec()) to reduce the memory footprint for PHP. I would be concerned that running this on a very large diff would create an unnecessary memory footprint.

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?

Copy link
Author

Choose a reason for hiding this comment

The 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)?

Copy link
Owner

@olivertappin olivertappin Aug 7, 2019

Choose a reason for hiding this comment

The 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.


$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)
Copy link
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing!

return isset($arguments[$index]) ? $arguments[$index] : $default;
}

protected function getOption($name, $default = null)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, some dockblocks would help keep the class consistent.


if ($this->isVerbose) {
if (!$this->baseBranch && !$this->currentBranch) {
$this->climate->comment("Comparing unstaged changes.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use single quotes

foreach ($this->argv as $arg) {
list($flag, $val) = explode('=', $arg);

if ($flag == $name) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use strict-type checking

Suggested change
if ($flag == $name) {
if ($flag === $name) {

protected function getArgument($index, $default = null)
{
$arguments = array_filter($this->argv, function ($val) {
return strpos($val, '-') === false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we change this to use yoda-style conditions?

Suggested change
return strpos($val, '-') === false;
return false === strpos($val, '-');


Basic example

```shell
phpcs-diff develop -v
phpcs-diff -v origin/develop develop
Copy link
Owner

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:

  1. phpcs-diff - Run a diff against unstaged changes.
  2. phpcs-diff origin/master - Run a diff against the base branch (defined) and the current branch (determined by git rev-parse --verify HEAD)
  3. 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.

@@ -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.'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be able to fit on the same line now.

@desyncr
Copy link
Author

desyncr commented Jul 27, 2019

Thanks for the comments. I'll address them shortly 👍

@olivertappin olivertappin added the enhancement New feature or request label Dec 17, 2019
@desyncr desyncr closed this Apr 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants