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
Conversation
@@ -52,12 +52,12 @@ | |||
"bin": ["bin/validate-json"], | |||
"extra": { | |||
"branch-alias": { | |||
"dev-master": "4.0.x-dev" | |||
"dev-master": "5.0.x-dev" |
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.
Thanks
.php_cs.dist
Outdated
$config | ||
->setRules([ | ||
// default | ||
'@PSR2' => true, |
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 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.
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.
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.
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.
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?
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.
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?
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.
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.
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.
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).
Hah, guess I should have actually looked at the changes. Retracting my LGTM! |
@bighappyface My thoughts generally:
|
const CHECK_MODE_COERCE_TYPES = 0x00000004; | ||
const CHECK_MODE_APPLY_DEFAULTS = 0x00000008; | ||
const CHECK_MODE_EXCEPTIONS = 0x00000010; | ||
const CHECK_MODE_NONE = 0x00000000; |
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.
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 :-).
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.
@alcohol are there comment delimiters or other facility to skip code blocks for php-cs-fixer?
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.
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) { |
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.
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
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 know, it is considered a risky fixer. They can be disabled explicitly.
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.
Could we please disable this one then?
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.
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] == '/') { |
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.
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?
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.
As far as I know, it is a string and array access is merely used as a quick way to check the first character.
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.
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.
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.
Sweet - should be fine then :-). Thanks for clarifying.
tests/Constraints/FactoryTest.php
Outdated
return array( | ||
public function invalidConstraintNameProvider() | ||
{ | ||
return array( |
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.
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.]
tests/Constraints/FactoryTest.php
Outdated
array('invalidConstraintName'), | ||
); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedException \InvalidArgumentException |
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.
This is probably incorrect - \JsonSchema\Exception\InvalidArgumentException
is a thing.
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.
But that is not the current namespace?
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.
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".
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.
Sure I can change it, and check if the tests still pass. Give me a few minutes.
tests/Constraints/FactoryTest.php
Outdated
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
* @expectedException \InvalidArgumentException |
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.
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', |
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.
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.
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.
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.
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.
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.
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)? |
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. |
I am fine to skip alignment fixes |
Updated PR accordingly. |
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.
Looks good! Thanks @alcohol :-).
..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.