Skip to content
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

Make code-style more consistent #355

Merged
merged 4 commits into from Feb 21, 2017
Merged

Make code-style more consistent #355

merged 4 commits into from Feb 21, 2017

Conversation

alcohol
Copy link
Contributor

@alcohol alcohol commented Feb 16, 2017

..through the use of a php-cs-fixer configuration and applying it.

Since there were a lot of inconsistencies already present in the codebase, I was not sure which rules would be preferred in some access (e.g. align double arrows in array notation, etc).

Putting this PR up here for feedback and discussion.

@@ -52,12 +52,12 @@
"bin": ["bin/validate-json"],
"extra": {
"branch-alias": {
"dev-master": "4.0.x-dev"
"dev-master": "5.0.x-dev"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks

.php_cs.dist Outdated
$config
->setRules([
// default
'@PSR2' => true,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am good with PHPCS fixer being run at large to get the baseline style into the repo, but may we have PHPCS evaluation added to the TravisCI config to catch infractions moving forward?

Thanks a lot for this contribution. I have been thinking about this for awhile but never got around to it.

Copy link
Contributor Author

@alcohol alcohol Feb 16, 2017

Choose a reason for hiding this comment

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

Is there a specific need or requirement that phpcodesniffer solves that php-cs-fixer does not? If Travis integration is the only thing, then I would like to point out that php-cs-fixer could just as easily be integrated into the Travis process. I just did not do it right away as not everyone is always in favor of this. E.g. at @composer we usually let style issues slip through in PR's because we do not want those to be a barrier for contributors. We run php-cs-fixer occasionally over the codebase ourselves manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or maybe I misunderstood and you were not referring to phpcodesniffer (which is generally called phpcs) but rather php-cs-fixer? If that is the case, I'll add it to the Travis flow. Any feedback on the ruleset? Removals, additions?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry for the confusion. I am cool with using php-cs-fixer to evaluate code style and am not tied to phpcs specifically. Adding to the TravisCI flow will be very helpful.

I am fine with the ruleset but am curious if the additional configs override/supplement defaults for the PSR-2 spec or the Symfony spec?

Copy link
Contributor Author

@alcohol alcohol Feb 16, 2017

Choose a reason for hiding this comment

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

Mostly Symfony overrides and some contrib rules (not part of Symfony nor any PSR).

  • The array syntax is because of the PHP supported version.
  • The concat with space seemed the most prevalent choice already in the codebase.
  • No unused imports I generally turn off since the detection does not work well with use statements that are only referenced in docblocks (at least that used to be my experience).
  • No useless return and ordered imports are part of contrib.
  • PHP doc align is a part of Symfony but didn't seem the most prevalent approach in current codebase.
  • PHP doc no package is part of Symfony and removes all @Package docblocks. Not sure if you wanted to keep these so I did not remove them for now.
  • Trailing comma in array is part of Symfony was not yet the most prevalent approach so I excluded it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately php-cs-fixer does not play nice together with HHVM. Since HHVM is one of the required versions in the matrix that must pass, I have to resort to dynamically installing php-cs-fixer. I opted to only install it on one specific build (chose the same one that is used for codecoverage, but can also move it to 7.1 if preferred).

@bighappyface
Copy link
Collaborator

@shmax @erayd thoughts before this is merged and other PRs are subject to code style analysis?

@shmax
Copy link
Collaborator

shmax commented Feb 16, 2017

I have no objections. LGTM.

Hah, guess I should have actually looked at the changes. Retracting my LGTM!

@erayd
Copy link
Collaborator

erayd commented Feb 16, 2017

@bighappyface My thoughts generally:

  • I really like the idea of having code style be a travis requirement to pass. It's good for the quality of submissions generally.
  • I do not like the idea of manually running the fixer on the codebase from time-to-time - it makes git blame annoying, because it'll flag the style commit rather than who actually changed stuff unless you start stepping back through the history. IMO much better to have the code be styled correctly in the first place before it's merged.
  • I like the choice of PSR2 as a style.
  • Quite happy to have an initial bulk run of the fixer over the current codebase to get things consistent. It'll wreck git blame, but I don't think there's any sensible way to avoid this, so let's just do it once.
  • I don't care which style validator / fixer is used, as long as it's reliable. I'm more familiar with PHPCS, but anything that does the job is fine with me.
  • To make contributions a bit smoother, having a 'fix-style' or similar composer script seems like a good idea. That way contributors have an easy way to force their own code to be compliant before submitting it.

const CHECK_MODE_COERCE_TYPES = 0x00000004;
const CHECK_MODE_APPLY_DEFAULTS = 0x00000008;
const CHECK_MODE_EXCEPTIONS = 0x00000010;
const CHECK_MODE_NONE = 0x00000000;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we have a rule that allows / enforces alignment of constants like this, rather than removing the whitespace?

I'd rather have enforced style than aligned constants, but both would be nice :-).

Copy link
Collaborator

Choose a reason for hiding this comment

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

@alcohol are there comment delimiters or other facility to skip code blocks for php-cs-fixer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not to my knowledge, but there is a whitespace rule to align equal signs.

@@ -136,7 +137,7 @@ protected function validateItems(&$value, $schema = null, JsonPointer $path = nu

// Treat when we have more schema definitions than values, not for empty arrays
if (count($value) > 0) {
for ($k = count($value); $k < count($schema->items); $k++) {
for ($k = count($value); $k < count($schema->items); ++$k) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems dangerous if the auto-fixer made this change; they aren't syntactically equivalent. It doesn't matter in this particular case, but if the same change is made in a place that relies on the return value of ++, then it'll alter the logic.

$var++; //increments $var and returns the pre-increment value
++$var; // increments $var and returns the post-increment value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know, it is considered a risky fixer. They can be disabled explicitly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we please disable this one then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, let me revert/rebase the changes and re-apply them.

*/
public static function combineRelativePathWithBasePath($relativePath, $basePath)
{
$relativePath = self::normalizePath($relativePath);
if ($relativePath == '') {
return $basePath;
}
if ($relativePath{0} == '/') {
if ($relativePath[0] == '/') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this intended to be $relativePath[0], or $relativePath->{'0'}? Both options are a possibility; dynamic object fields are used elsewhere in the code. Is $relativePath always an array (or an object that implements array access), or can it sometimes be a non-array-compliant object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I know, it is a string and array access is merely used as a quick way to check the first character.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is just a string. The curly and square brace styles are equivalent, but most people are more likely to be familiar with the square bracket notation, so it's probably the best way to go.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sweet - should be fine then :-). Thanks for clarifying.

return array(
public function invalidConstraintNameProvider()
{
return array(
Copy link
Collaborator

@erayd erayd Feb 16, 2017

Choose a reason for hiding this comment

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

The indenting went from OK to broken here. [ed: the broken indenting is not included in GitHub's highlighted lines for this comment; need to view the code just below this as well to see the problem.]

array('invalidConstraintName'),
);
}

/**
* @expectedException InvalidArgumentException
* @expectedException \InvalidArgumentException
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is probably incorrect - \JsonSchema\Exception\InvalidArgumentException is a thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But that is not the current namespace?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's not the current namespace, but it's probably what was intended, as convention everywhere else in the project is to use the namespaced exceptions. This one is less "the auto-fixer screwed up", and more "the original source screwed up, and the auto-fixer perpetuated that, so we should fix now it rather than making the error worse".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure I can change it, and check if the tests still pass. Give me a few minutes.

}

/**
* @expectedException InvalidArgumentException
* @expectedException \InvalidArgumentException
Copy link
Collaborator

Choose a reason for hiding this comment

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

As per above, this probably isn't intended to be a root-namespace exception.

"pointer" => "/prop2",
"message" => "Array value found, but a string is required",
"constraint" => "type",
'property' => 'prop2',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any option for an alignment rule for this kind of thing? Not a huge problem if there is not, but it would be nice to keep things neat.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes there is, but since the majority of the codebase did not align the double arrow in array notations, I opted to make this consistent everywhere. If however it is desired that these be aligned, then the rule can simply be changed to that and the rest of the code will be adjusted accordingly.

Copy link
Collaborator

@erayd erayd Feb 17, 2017

Choose a reason for hiding this comment

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

That's a fair call. @bighappyface & @shmax - what are your thoughts on enabling this globally?

@alcohol - If global is undesirable for some reason, would it be possible to enable it just for the testsuite? That's where most of the array stuff that needs aligning is located anyway.

@alcohol
Copy link
Contributor Author

alcohol commented Feb 17, 2017

The alignment rules make things more consistent but unfortunately also look worse in some cases. Perhaps you would prefer to just turn them off (both, as in don't align and don't un-align)?

@shmax
Copy link
Collaborator

shmax commented Feb 17, 2017

I guess I could go either way on the object alignment, but I've never seen the other kind of inline alignment happening here and there in the code before, so my instinct is to shy away from it--it gives the code a kind of jittery feel, and it seems like the sort of thing that might be tough to second-guess as one is styling by hand. I guess my vote is to not do it, as @alcohol says.

@bighappyface
Copy link
Collaborator

I am fine to skip alignment fixes

@alcohol
Copy link
Contributor Author

alcohol commented Feb 17, 2017

Updated PR accordingly.

Copy link
Collaborator

@erayd erayd left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks @alcohol :-).

@bighappyface bighappyface merged commit 42c1043 into justinrainbow:master Feb 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants