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 prefix removal for class names and variable names #962

Merged

Conversation

JoshuaBehrens
Copy link
Contributor

Type: feature / documentation update
Issue: Adds prefix removal for variables and class names
Breaking change: no
Changed documentation pages:

My issue is for class names that need to follow a pattern. E.g. doctrine/migrations expect migration classes to have a prefix. Following a functional pattern should not count to a meaningful name limitation. phpmd does not support this yet.

As we also have a rule for variable names I thought about this change being useful there as well. I think about Hungarian notation name schemas. I don't use them and I support statements that they are dated but it is a thing. In addition I can imagine similar situations for properties like patterns in the class name situation. Sooo I just added it as well.

Please check this points before submitting your PR.

  • Add test to cover the changes you made on the code.
  • If you have a change on the documentation, please link to the page that you change.
  • If you add a new feature please update the documentation in the same PR.
  • If you really need to add a breaking change, explain why it is needed. Understand that this result in a lower change to get the PR accepted.

    I made it non-breaking 😃
  • Any PR need 2 approvals before it get merged, sometimes this can take some time. Please be patient.

Adding a New Rule Property

  • Add the new property to rule set XML, e.g. src/main/resources/rulesets/naming.xml
  • Add documentation for the new property, e.g. src/site/rst/rules/naming.rst
  • Implement new property in rule, e.g. src/main/php/PHPMD/Rule/Naming/LongVariable.php
  • Cover cases for the new property in rule test, e.g. src/test/php/PHPMD/Rule/Naming/LongVariableTest.php
    • Cover the case when the new property is not set and the rule should not apply
    • Cover the case when the new property is not set and the rule should apply
    • Cover case when the new property is set and the rule should not apply
    • Cover case when the new property is set and the rule should apply
    • Cover edge cases of the new property, if any

Comment

I admit I did not look out for contribution guides. I just did. Reviewing your checklist here I think I already covered most of the parts. As I shall be patient I can also later amend my tests. For now you can already get what I want to do and review my approach. I assume I did not match something and I have to adjust anyway 😅

@tvbeek
Copy link
Member

tvbeek commented Jun 16, 2022

@JoshuaBehrens thanks for your PR. The first high over impression is that you did a good job. I have enabled the GitHub actions to run but now I see that they fail, can you take a look to it?

@JoshuaBehrens
Copy link
Contributor Author

Thank you for the quick feedback. I was not sure we are talking about that level of patience :D
Sure. I think I already saw the issue. Test fixture lookup by method name fails because I had some test method renaming going on.

@JoshuaBehrens JoshuaBehrens force-pushed the feature/prefix-removal-for-name-lengths branch from 570a388 to 07048f7 Compare June 16, 2022 17:46
@JoshuaBehrens JoshuaBehrens force-pushed the feature/prefix-removal-for-name-lengths branch from 07048f7 to 2d017b5 Compare June 16, 2022 17:48
Copy link
Member

@kylekatarnls kylekatarnls left a comment

Choose a reason for hiding this comment

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

Just a test to fix and good to go.

src/test/php/PHPMD/Utility/StringsTest.php Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Sep 10, 2022

Codecov Report

Patch coverage: 100.00% and project coverage change: -0.35% ⚠️

Comparison is base (10f8159) 89.10% compared to head (05b31ec) 88.75%.

❗ Current head 05b31ec differs from pull request most recent head 51f12f8. Consider uploading reports for the commit 51f12f8 to get more accurate results

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #962      +/-   ##
============================================
- Coverage     89.10%   88.75%   -0.35%     
+ Complexity     1160     1082      -78     
============================================
  Files           107       96      -11     
  Lines          2956     2722     -234     
============================================
- Hits           2634     2416     -218     
+ Misses          322      306      -16     
Files Changed Coverage Δ
src/main/php/PHPMD/Rule/Naming/LongClassName.php 100.00% <100.00%> (ø)
src/main/php/PHPMD/Rule/Naming/LongVariable.php 100.00% <100.00%> (ø)
src/main/php/PHPMD/Utility/Strings.php 100.00% <100.00%> (ø)

... and 19 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kylekatarnls kylekatarnls added this to the 2.14.0 milestone Sep 13, 2022
@JoshuaBehrens
Copy link
Contributor Author

Is a rebase beneficial for this to get merged? :) I can do that if it helps

@@ -109,7 +116,11 @@ protected function checkMaximumLength(AbstractNode $node)
{
$threshold = $this->getIntProperty('maximum');
$variableName = $node->getImage();
$lengthWithoutDollarSign = Strings::lengthWithoutSuffixes($variableName, $this->getSubtractSuffixList()) - 1;
$lengthWithoutDollarSign = Strings::lengthWithoutPrefixesAndSuffixes(
\ltrim($variableName, '$'),
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I dont' think we normally name space functions

Suggested change
\ltrim($variableName, '$'),
ltrim($variableName, '$'),

@AJenbo AJenbo merged commit 7e0fa29 into phpmd:master Jul 25, 2023
26 of 27 checks passed
@AJenbo
Copy link
Member

AJenbo commented Jul 25, 2023

@JoshuaBehrens this PR was from before I joined the project I think so I wasn't aware of it, thanks for bringing it to my attention. We normally require 2 maintainers to approve a PR before it gets merged and it seems that was all that was missing.

@JoshuaBehrens
Copy link
Contributor Author

Thank you @AJenbo for taking care of the pull request :) love to contribute again. I have some more ideas

@JoshuaBehrens JoshuaBehrens deleted the feature/prefix-removal-for-name-lengths branch July 26, 2023 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

5 participants