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

Allow to configure PHPMD with php or yaml file #1058

Open
wants to merge 18 commits into
base: 3.x
Choose a base branch
from

Conversation

kylekatarnls
Copy link
Member

Type: feature
Breaking change: yes

@kylekatarnls kylekatarnls added this to the 3.0.0 milestone Jan 7, 2024
@kylekatarnls
Copy link
Member Author

kylekatarnls commented Jan 7, 2024

Note: the src/conf/phpmd.schema.json file is what allow auto-completion in IDEs:
image

To be automatically applied to phpmd.yml files by IDE, we'll need to submit it in
https://github.com/SchemaStore/schemastore/tree/master/src/schemas/json

Then to be associated to relevant file names in the catalog:
https://github.com/SchemaStore/schemastore/blob/master/src/api/json/catalog.json

@kylekatarnls
Copy link
Member Author

src/test/resources/files/rulesets/phpmd.yml is an example of how it would look to configure rule with a YAML file.

It would work the same by providing JSON file or PHP file returning the same array structure.

Comment on lines +146 to +162
$options = new CommandLineOptions([__FILE__, 'app', 'sarif']);

$args = [__FILE__, 'text', 'design'];
new CommandLineOptions($args);
self::assertSame('cleancode,codesize,controversial,design,naming,unusedcode', $options->getRuleSets());
self::assertSame('sarif', $options->getReportFormat());
self::assertSame('app', $options->getInputPath());

$options = new CommandLineOptions([__FILE__, 'app']);

self::assertSame('cleancode,codesize,controversial,design,naming,unusedcode', $options->getRuleSets());
self::assertSame('text', $options->getReportFormat());
self::assertSame('app', $options->getInputPath());

$options = new CommandLineOptions([__FILE__]);

self::assertSame('cleancode,codesize,controversial,design,naming,unusedcode', $options->getRuleSets());
self::assertSame('text', $options->getReportFormat());
self::assertSame('src', $options->getInputPath());
Copy link
Member Author

Choose a reason for hiding this comment

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

Comment on lines +377 to +380
0 => [file_exists('src') ? 'src' : '.', 'text', $this->getDefaultConfig()],
1 => ['text', $this->getDefaultConfig()],
2 => [$this->getDefaultConfig()],
default => [],
Copy link
Member Author

Choose a reason for hiding this comment

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

  • input path would fallback to "src" if it's exist, "." else
  • report format would fallback to "text"
  • ruleset would fallback to first config file found by getDefaultConfig, or "all" if no file provided.


$validator = new ArgumentsValidator($hasImplicitArguments, $originalArguments, $arguments);

$this->ruleSets = (string)array_pop($arguments);
$ruleSets = (string)array_pop($arguments);
$this->ruleSets = $ruleSets === 'all' ? InternalRuleSet::getNamesConcatenated() : $ruleSets;
Copy link
Member Author

Choose a reason for hiding this comment

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

"all" would be a shorthand for all internal rulesets from PHPMD:

cleancode,codesize,controversial,design,naming,unusedcode

@kylekatarnls kylekatarnls force-pushed the feature/multi-format-support branch 2 times, most recently from dabeb46 to 51c1a92 Compare January 7, 2024 17:06
Base automatically changed from feature/3.x-experiment to 3.x April 29, 2024 15:24
@AJenbo
Copy link
Member

AJenbo commented May 11, 2024

Not using XML is a big improvement in my book. I prefer the PHP option personally.

@kylekatarnls I think now is a good point to rebase this so it's ready for merging.

…ti-format-support

# Conflicts:
#	composer.json
#	src/main/php/PHPMD/RuleSetFactory.php
#	src/main/php/PHPMD/TextUI/CommandLineOptions.php
#	src/test/php/PHPMD/RuleSetFactoryTest.php
#	src/test/php/PHPMD/RuleSetTest.php
#	src/test/php/PHPMD/TextUI/CommandLineOptionsTest.php
@kylekatarnls kylekatarnls changed the title [Tentative] Allow to configure PHPMD with php or yaml file Allow to configure PHPMD with php or yaml file May 16, 2024
@kylekatarnls kylekatarnls force-pushed the feature/multi-format-support branch 2 times, most recently from fa728ee to c7d924c Compare May 16, 2024 17:01
Copy link

codecov bot commented May 16, 2024

Codecov Report

Attention: Patch coverage is 94.78673% with 11 lines in your changes are missing coverage. Please review.

Project coverage is 92.22%. Comparing base (b22ed70) to head (2585b4e).
Report is 7 commits behind head on 3.x.

Files Patch % Lines
src/main/php/PHPMD/RuleSetFactory.php 94.02% 8 Missing ⚠️
src/main/php/PHPMD/Node/FunctionNode.php 33.33% 2 Missing ⚠️
src/main/php/PHPMD/TextUI/CommandLineOptions.php 95.65% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##                3.x    #1058      +/-   ##
============================================
+ Coverage     92.15%   92.22%   +0.07%     
- Complexity     1278     1297      +19     
============================================
  Files           107      109       +2     
  Lines          3378     3448      +70     
============================================
+ Hits           3113     3180      +67     
- Misses          265      268       +3     

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

*/
public function getMetric($name)
public function getMetric(string $name): null|float|int
Copy link
Member

Choose a reason for hiding this comment

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

this doesn't cover numeric-string, i think that might be used some times, though I'm not 100% sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

We should not use string to represent numbers, they all come from getNodeMetrics from PDepend, I will make a PR to ensure/enforce them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I was wrong, npath is a numeric string, it sounds it was to support integer numbers bigger than 64 bits.

Copy link
Member

Choose a reason for hiding this comment

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

We could add a php64 requirement in composer.

@kylekatarnls kylekatarnls force-pushed the feature/multi-format-support branch 2 times, most recently from 1fa1037 to df1b444 Compare May 17, 2024 00:01
@kylekatarnls kylekatarnls marked this pull request as ready for review May 17, 2024 00:17
@@ -32,31 +36,18 @@ class RuleSetFactory
/**
Copy link
Member

@AJenbo AJenbo May 17, 2024

Choose a reason for hiding this comment

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

This calls is getting a bit heavy on pulling out mixed variables and casting them to various types. This is likely going to trigger a lot of unsafe operations on higher PHPStan levels.

@AJenbo
Copy link
Member

AJenbo commented May 17, 2024

Lots of things going on in one PR, most of it is nice, but a bit hard to review and comment on individual things with it all there.

@kylekatarnls
Copy link
Member Author

kylekatarnls commented May 17, 2024

In theory bcmul can go even above 64 bits, but I wonder if it has a real interest in practice for npath (and also why it would be the only one using big number). BTW it also possible to go above by using float (which is automatic in PHP), it just loose a bit of precision but I'm not sure it's an actual problem, if you have billion npath, you probably care about order of magnitude, not an exact value.

@AJenbo
Copy link
Member

AJenbo commented May 17, 2024

i wold normally just use it if decimal fractional rounding is important, I have a hard time imagining the need for values that high.

@kylekatarnls
Copy link
Member Author

Lots of things going on in one PR, most of it is nice, but a bit hard to review and comment on individual things with it all there.

I'm also unsatisfied about how it growth after re-basing. I will split.

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

3 participants