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

Exception to the newline requirement on the first member var after class opener. #1355

Closed
wants to merge 6 commits into from

Conversation

wilsonge
Copy link

Based on the draft PHPCS2 Joomla Custom member var sniff https://github.com/photodude/coding-standards/blob/phpcs-2/Joomla/Sniffs/WhiteSpace/MemberVarSpacingSniff.php#L102-L137

Addresses issues #920 and #725

This is a successor PR to #972 based on the feedback by @gsherwood which @photodude hadn't had time to work on yet

@photodude
Copy link
Contributor

@gsherwood any thoughts on merging this?

$error = 'Expected %s blank lines before first member var; %s found';
}

if ($foundLines === $this->spacing || ($isFirst === true && $foundLines === $this->firstMemberSpacing)) {
Copy link

Choose a reason for hiding this comment

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

I found a bug with this line...

If I have a case where $this->spacing is set to 1 and $this->firstMemberSpacing is set to 0 and $isFirst is true, then if I happen to have exactly one space above the first member, I would expect to receive an error saying:

Expected 0 blank lines before first member var; 1 found

However, since $foundLines === $this->spacing is evaluated first, and it is true (since $foundLines is 1), then no error occurs.

I think the proper approach would be to change this line to:

if ($foundLines === $expected) {

Copy link
Author

Choose a reason for hiding this comment

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

Ahh good spot. Fixed :)

@apaarmann
Copy link

As many popular projects (some references in #1464) use no spacing between the class opening and first var declaration, shouldn't $firstMemberSpacing have default value as 0 ?
I know this change won't be backward compatible but maybe its the right way to go.

@gsherwood
Copy link
Member

shouldn't $firstMemberSpacing have default value as 0 ?

It must be defaulted to 1 to maintain backwards compatibility. Projects that require no spaces are probably not even using the current sniff as it is not configurable, so there is no BC issue with them.

@wilsonge
Copy link
Author

Rebased to fix conflicts

@photodude
Copy link
Contributor

@wilsonge would you fix the CS items

FILE: src/Standards/Squiz/Sniffs/WhiteSpace/MemberVarSpacingSniff.php
 23 | ERROR | [x] Expected "integer" but found "int" for @var tag in member variable comment
 31 | ERROR | [x] Expected "integer" but found "int" for @var tag in member variable comment

@wilsonge
Copy link
Author

Done :) Thanks for the ping :)

@photodude
Copy link
Contributor

You are welcome, thanks for fixing those items.

@gsherwood gsherwood added this to the 3.1.0 milestone Jun 13, 2017
$error = 'Expected %s blank lines before first member var; %s found';
}

if ($foundLines === $this->spacing || ($isFirst === true && $foundLines === $this->firstMemberSpacing)) {

Choose a reason for hiding this comment

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

I got an error when I pass in zero as a parameter:

<property name="firstMemberSpacing" value="0" />

It seems to me a cast to int is missing:

$foundLines === (int)$this->firstMemberSpacing

gsherwood added a commit that referenced this pull request Sep 1, 2017
…n member vars and spacing before the first member var (refs #1355 and #725)
@gsherwood
Copy link
Member

Thanks for this PR, although I didn't merge it in as I did things a little differently once I added a bunch of test cases. See the linked commit for the diff if you are interested.

@gsherwood gsherwood closed this Sep 1, 2017
@photodude
Copy link
Contributor

thanks @gsherwood glad to see this feature added.

@wilsonge wilsonge deleted the first_class_new_line branch September 2, 2017 17:46
@wilsonge
Copy link
Author

wilsonge commented Sep 2, 2017

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants