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

Feature request: add sniff support for constant comments #2221

Open
rjgwiz opened this issue Nov 8, 2018 · 9 comments
Open

Feature request: add sniff support for constant comments #2221

rjgwiz opened this issue Nov 8, 2018 · 9 comments

Comments

@rjgwiz
Copy link

rjgwiz commented Nov 8, 2018

phpdoc mentions constants under the programming constructs that should be preceded by a DocBlock.

I can't find anyway to enforce docblock comments for constants.

Docblock comments are currently enforceable via ClassCommentSniff, FunctionCommentSniff, VariableCommentSniff, and FileCommentSniff. None seem appropriate to add support for constants (VariableCommentSniff would be the closest, but it extends AbstractVariableSniff and adding T_CONST to the listen list would break a lot of other sniffs, class constants aren't normal variables, and it wouldn't cover defined constants.

Some sniffs exist for constants, for example UpperCaseConstantNameSniff. Any hesitations around adding a new sniff called ConstantCommentSniff? Scope would be the same as UpperCaseConstantNameSniff so any constant defined via the define() function or the const keyword (e.g. class constant).

With buy-in I could work on ConstantCommentSniff and submit a PR when it's ready.

@gsherwood
Copy link
Member

I think a ConstantComment sniff sounds like a good idea.

I haven't reviewed the doc comment sniffs because I've been waiting for PSR-5 to be complete so I could (hopefully) standardise the checks instead of using the Squiz-specific ones. That, and a lack of time.

If you want to give any of the comment sniffs a go, please do. Huge bonus if you have it enforce PSR-2's recommendations.

@rjgwiz
Copy link
Author

rjgwiz commented Nov 29, 2018

Thank you @gsherwood. I hope to start working on this soon.

@TomasVotruba
Copy link
Contributor

@rjgwiz 👍 You can basically copy https://github.com/Symplify/Symplify/blob/master/packages/CodingStandard/src/Sniffs/Commenting/VarConstantCommentSniff.php + extends a bit to match your needs.

/**
 * @var int
 */
private const VALUE = 10;

It works great with static analysis rule - https://github.com/Symplify/Symplify/blob/master/packages/PHPStanExtensions/src/Rules/Classes/MatchingTypeConstantRule.php, e.g.:

/**
 * @var int
 */
private const VALUE = '10'; // Error!

@jrfnl
Copy link
Contributor

jrfnl commented Feb 5, 2019

Related to #2222 which will make creating this sniff so much easier ;-)

@rjgwiz
Copy link
Author

rjgwiz commented Feb 27, 2019

Thank you both. I plan to get to this in the next week or two.

@TomasVotruba
Copy link
Contributor

@jrfnl Any ETA on that feature coming to tagged version?

@jrfnl
Copy link
Contributor

jrfnl commented Feb 27, 2019

@TomasVotruba I expect it to be ready for PHPCS 3.5.0 which includes lots of other goodies which will be useful for this too. See #2189

@TomasVotruba
Copy link
Contributor

@jrfnl That would be amazing!

@dereuromark
Copy link
Contributor

I guess this wasnt pursued further so far?
The links above to the existing ones are 404 by now. Maybe someone knows the new URLs?

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

No branches or pull requests

5 participants