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

Implement LongMethodName #1045

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft

Conversation

kylekatarnls
Copy link
Member

Type: feature
Issue: Resolves Fix #486
Breaking change: no

@kylekatarnls kylekatarnls added this to the 2.15.0 milestone Nov 28, 2023
@kylekatarnls kylekatarnls force-pushed the feature/long-method-name branch 2 times, most recently from 5aacc5d to 2c4a806 Compare November 29, 2023 08:27
Copy link

codecov bot commented Nov 29, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (4a75765) 92.53% compared to head (9e37925) 92.58%.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1045      +/-   ##
============================================
+ Coverage     92.53%   92.58%   +0.04%     
- Complexity     1239     1244       +5     
============================================
  Files           111      112       +1     
  Lines          3404     3424      +20     
============================================
+ Hits           3150     3170      +20     
  Misses          254      254              

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

@kylekatarnls kylekatarnls force-pushed the feature/long-method-name branch 3 times, most recently from c298eb2 to 9ba3709 Compare November 29, 2023 22:21
@kylekatarnls kylekatarnls force-pushed the feature/long-method-name branch 4 times, most recently from e40380d to 9526335 Compare November 30, 2023 22:15
@kylekatarnls kylekatarnls marked this pull request as ready for review December 1, 2023 08:09
Copy link
Member

@ravage84 ravage84 left a comment

Choose a reason for hiding this comment

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

Thank you very much for the new rule.

Reviewed code-only, didn't test it.

.github/workflows/tests.yml Outdated Show resolved Hide resolved
src/main/php/PHPMD/Rule/CleanCode/BooleanArgumentFlag.php Outdated Show resolved Hide resolved
src/main/php/PHPMD/Rule/Naming/LongMethodName.php Outdated Show resolved Hide resolved
src/test/php/PHPMD/Rule/Naming/LongMethodNameTest.php Outdated Show resolved Hide resolved
}

/**
* @return list<string>
Copy link
Member

Choose a reason for hiding this comment

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

I don't think list is supported by all API doc tools:

https://docs.phpdoc.org/3.0/guide/guides/types.html#supported-types

Copy link
Member Author

Choose a reason for hiding this comment

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

list is now supported for years by PHPStorm, Plsam, PHPStan and Phan, it adds an interesting precision that keys of an array $value are strictly equal to range(0, count($value)-1) (I.E. being ordered integer sequence starting at 0, see array_is_list). Such as the result of explode, array_values, array_keys.

IMO giving the benefit to the majority of tools who support it worth to drop other tools supports (which should fallback if they don't know how to read the annotation: to array if the return type is specified, else to mixed).

src/main/php/PHPMD/Utility/ExceptionProperty.php Outdated Show resolved Hide resolved
/**
* Utility class to handle rule exceptions property
*/
class ExceptionProperty
Copy link
Member

Choose a reason for hiding this comment

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

This is quite a lot of code to replace a few lines previously. Not sure what to feel about it...

Copy link
Member Author

Choose a reason for hiding this comment

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

This will make getExceptionList() to disappear completely in the next version and we can simply make exceptions available and working the same way for all the rule.

So on the next version it's more feature with way less (redundant) code.

Copy link
Member Author

Choose a reason for hiding this comment

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

And introduce it now creates an easier step for people who created custom routes to prepare for upgrade.

Copy link
Member Author

Choose a reason for hiding this comment

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

It would need more discussion as it's a bit more opinionated but other types of config could also be shared so to make them also working the same way across different rules and auto-discoverable and auto-documented.

For instance with attributes (but would need to drop PHP <= 7):

class LongMethodName extends AbstractRule
{
  #[ConfigProperty(description: 'Comma-separated list of exceptions')]
  protected ExceptionProperty $exceptions;

  #[ConfigProperty(description: 'Maximum length for a method or function name')]
  protected Threshold $maximum;
}

Without dropping PHP version, it can be dependency-injection:

class LongMethodName extends AbstractRule
{
  public function __construct(
    /** Comma-separated list of exceptions */
    ExceptionProperty $exceptions,

    /** Maximum length for a method or function name */
    Threshold $maximum,
  ) {
  }
}

/**
* @return array<string, int>
*/
public function flip()
Copy link
Member

Choose a reason for hiding this comment

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

Isn't there any less technical name to express what we actually want to achieve or what we actually get back from this method?

Also, not having doc blocks, doesn't help either. 😞

Copy link
Member Author

Choose a reason for hiding this comment

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

array_flip is a micro-optimization so index and isset() can be used instead of in_array(). However if we plan to support pattern everywhere we allow exceptions, then we would no longer need it in next major version.

src/main/php/PHPMD/Utility/ExceptionProperty.php Outdated Show resolved Hide resolved
@kylekatarnls kylekatarnls force-pushed the feature/long-method-name branch 2 times, most recently from 7610a25 to b8cdf88 Compare December 2, 2023 19:26
@kylekatarnls kylekatarnls modified the milestones: 2.15.0, 2.16.0 Dec 10, 2023
Co-authored-by: Marc Würth <ravage@bluewin.ch>
@kylekatarnls kylekatarnls marked this pull request as draft December 10, 2023 19:53
</description>
<priority>3</priority>
<properties>
<property name="maximum" description="Maximum length for a method or function name" value="20"/>
Copy link
Member Author

Choose a reason for hiding this comment

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

Taking a look at https://github.com/phpmd/phpmd/actions/runs/7159958473/job/19493792510?pr=1045#step:9:1769

I guess 20 might be a little short for a default value.

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.

New LongMethodName Rule
2 participants