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

Add new sniff for checking Control structure brackets are on a new line #1847

Closed
wants to merge 42 commits into from

Conversation

photodude
Copy link
Contributor

Add new sniff for checking Control structure brackets are on a new line
Add new test for the new sniff checking Control structure brackets are on a new line

@photodude
Copy link
Contributor Author

@gsherwood What Code standard should I use to format things here? I seem to have chosen the wrong one for the formatting.

@jrfnl
Copy link
Contributor

jrfnl commented Jan 8, 2018

@photodude PHPCS has its own ruleset you can use: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/phpcs.xml.dist 😄

@photodude
Copy link
Contributor Author

Thanks @jrfnl, hopefully I implemented the fixer right since I just ran it directly from the github files so I didn't have to mess with my PHPCS 2.9 install.

@jrfnl
Copy link
Contributor

jrfnl commented Jan 9, 2018

@photodude Want me to have a look/review ?

@photodude
Copy link
Contributor Author

photodude commented Jan 9, 2018

Do you mean to review the sniff? Yes, please.
There are probably better was to do this sniff. The sniff was originally based off the PEAR.Classes.ClassDeclaration Sniff but modified to work with control structure tokens.

I think the code style is fixed, if I can get the one comment section to stop yelling at me that it wants more or less indenting, or things at the end.

@photodude
Copy link
Contributor Author

code style should be all in the green now

@photodude
Copy link
Contributor Author

@jrfnl @gsherwood This is now good to go for review consideration

@@ -0,0 +1,170 @@
<?php
Copy link
Member

Choose a reason for hiding this comment

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

The test file doesn't have any examples of comments being placed between the closing parenthesis and the opening brace of the control structures, so it's not obvious how those would be fixed. A few examples of using // comment and the phpcs annotations like // phpcs:ignore -- for reasons would be good, just to make sure the sniff copes with them on other people's code.

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'll see about adding those cases

@photodude
Copy link
Contributor Author

Quick question,
I'm getting a CS error on "Implicit true comparisons prohibited; use === TRUE instead"
the error occurs for this comparison

if (array_key_exists('nested_parenthesis', $tokens[$stackPtr])) {

What is the correct solution for this?

@gsherwood
Copy link
Member

@photodude

if (array_key_exists('nested_parenthesis', $tokens[$stackPtr]) === true) {

@photodude
Copy link
Contributor Author

photodude commented Oct 2, 2018

Thanks @gsherwood (and @jrfnl I saw your comment too for a moment)

I believe the last items here are

  • convert the unit test into fake code
  • add unit test cases using // comment and the phpcs annotations like // phpcs:ignore -- for reasons

@gsherwood gsherwood added this to the 3.4.0 milestone Nov 7, 2018
@gsherwood gsherwood removed this from the 3.4.0 milestone Dec 10, 2018
@gsherwood
Copy link
Member

I've done another review of this but it's not ready to merge yet. It still needs those last two items completed (remove real code from tests + add tests for comment syntax) plus:

  • I think this would be worth renaming to BraceNewLine to match other sniffs
  • Please replace tabs with spaces in the unit tests files unless the tab is testing something

I've also put a few minor changes into a review for you.

@gsherwood
Copy link
Member

I've done another review of this and it's not ready to merge:

  • The unit test files don't match the sniff name, so they aren't actually running. The files need to be renamed so they are tested.
  • If the files are renamed, the unit tests will fail because the list of line numbers to error counts is wrong.
  • The sniff enforces indentation using tabs, which is not something other sniffs do. If the sniff wants to check indentation of the opening brace, it needs to do so using spaces. If you use tabs in your coding standard, the DisallowSpaceIndent sniff will go ahead and fix that for you, understanding where the proper tab stops are.
  • The unit test files is indented using tabs, so this needs to be replaced with spaces.
  • There is no need for an indent property in this sniff if you are just trying to align the opening brace under the IF statement. PHPCS provides 'column' indexes in the tokens array so you know where to align things. This will make the sniff possible to use for any indent type and length.

I'm going to remove this from the 3.5.0 release and there looks to be too much more to do here to get it ready.

@gsherwood gsherwood removed this from the 3.5.0 milestone May 22, 2019
@pimjansen
Copy link

@photodude any way that this will be finished? If you need some assistance let me know

@photodude
Copy link
Contributor Author

photodude commented Jul 9, 2019

@pimjansen I would love to get this finished. Assistance would be greatly appreciated and would greatly speed along the process. (I, unfortunately, have reduced availability to work on side projects like this at the moment)

  • fix issue with closure combined with a chained function call
  • fix issue with closure combined with switch case control structure
  • The sniff enforces indentation using tabs, which is not something other sniffs do. If the sniff wants to check indentation of the opening brace, it needs to do so using spaces. If you use tabs in your coding standard, the DisallowSpaceIndent sniff will go ahead and fix that for you, understanding where the proper tab stops are.
  • There is no need for an indent property in this sniff if you are just trying to align the opening brace under the IF statement. PHPCS provides 'column' indexes in the tokens array so you know where to align things. This will make the sniff possible to use for any indent type and length.
  • The unit test files are indented using tabs, replace tabs with spaces unless the tab is testing something (add comments to specify if the tabs are testing something specific)
  • The test file doesn't have any examples of comments being placed between the closing parenthesis and the opening brace of the control structures, so it's not obvious how those would be fixed. A few examples of using // comment and the phpcs annotations like // phpcs:ignore -- for reasons would be good, just to make sure the sniff copes with them on other people's code.

@pimjansen
Copy link

What is the easiest way to help here since this is open for way too long. What is still open and the fastest way to continue on this branch?

@photodude @gsherwood

@photodude
Copy link
Contributor Author

photodude commented Sep 28, 2019

What is the easiest way to help here since this is open for way too long. What is still open and the fastest way to continue on this branch?

@pimjansen If you can work on any of the open items in the to do checklist that would help complete this sniff.

@photodude
Copy link
Contributor Author

@pimjansen I don't think I'm going to get back to working on this, as I'm now working in a different field. If you would like to take over and finish this PR all the necessary information on what needs to be completed is here.

photodude and others added 19 commits February 27, 2022 11:41
We don't want "real" code in the test just code structure as we are only testing code structure
return an empty array as getWarningList() must return an array
First round of correcting the line numbers for the Error list
Round 2 of correcting the line numbers for the Error list
Expected errors should now be correct... 
LINE 225 is reporting expected 1 error but found 0 errors. but there is an indent error as the line should be indented more to match the control structure. Seems like line 225 is thinking it belongs to the closure function and not the control structure.
I believe this sniff's scope is restricted to checking the first bracket for new line and associated alignment.
The unit test files are indented using tabs, replace tabs with spaces unless the tab is testing something (add comments to specify if the tabs are testing something specific)
Examples of comments being placed between the closing parenthesis () and the opening brace {} of the control structures using // comment
Add examples using phpcs annotations to make sure the sniff copes with them on other people's code.
Add examples using phpcs annotations to make sure the sniff copes with them on other people's code.
@photodude
Copy link
Contributor Author

photodude commented Jan 29, 2023

Closing as Joomla4 is moving to PSR12. There is work to make this PR happen and I'm currently not working in PHP and don't know when I would be able to work on this again.
Sadly others Who indicated they were willing to finish this PR did not submit anything for finishing this PR.
Overall PR is now stale and should be closed.

@photodude photodude closed this Jan 29, 2023
@photodude photodude deleted the ControlBracketsNewLine branch January 29, 2023 20:48
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.

5 participants