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

New Linter : PHP-CS-Fixer #3550

Open
llaville opened this issue May 9, 2024 · 14 comments
Open

New Linter : PHP-CS-Fixer #3550

llaville opened this issue May 9, 2024 · 14 comments
Assignees
Labels
enhancement New feature or request

Comments

@llaville
Copy link
Collaborator

llaville commented May 9, 2024

Is your feature request related to a problem? Please describe.
It's not a problem, and we may consider it as an improvement to PHP linters

Describe the solution you'd like
Add first integration of the official https://github.com/PHP-CS-Fixer/PHP-CS-Fixer tool to automatically check/fix PHP Coding Standards issues

Describe alternatives you've considered
It's an alternative to PHP_CodeSniffer

Additional context
Actually, I've already worked on a SARIF support (and my converter PhpCsFixerConverter for bartlett/sarif-php-sdk : https://github.com/llaville/sarif-php-sdk is ready)

Pre-conditions :

Here is a preview of such integration to MegaLinter

new_linter

@llaville llaville added the enhancement New feature or request label May 9, 2024
@llaville llaville self-assigned this May 9, 2024
@echoix
Copy link
Collaborator

echoix commented May 9, 2024

#3515 doesn't appear like a PR for me

@llaville
Copy link
Collaborator Author

llaville commented May 9, 2024

@echoix Of course, because I'm not opened a PR yet.
I wanted to keep it under test, especially due to this new linter, to see if I've missed something.
But, as it is not the case, I'll open a PR tomorrow.

@llaville
Copy link
Collaborator Author

I'm sad but I must admit that the SARIF support to PHP-CS-Fixer won't be for tomorrow (next days and/or months).

BTW, I should at least propose a new PHP linter (following official project in current version 3.56 or greater)

@llaville
Copy link
Collaborator Author

llaville commented May 14, 2024

I think it's a shame not to be able to use the autofix feature for PHP Linters.

So, even if PHP-CS-Fixer new linter is ready, I'm still working to be able to use the APPLY_FIXES with at least PHP_PHPCSFIXER

First run that seems matched

php-cs-fixer

At least PHP-CS-Fixer do its job, and the fix was applied, but the UPDATED_SOURCES_REPORTER said the opposite !

@nvuillam Do you prefer to have a separate PR for PHP linter autofix ? or may I include in the upcoming PR for adding this new linter ?

Finally, I've choosen to prepare a PR without autofix feature fix for this linter

@llaville
Copy link
Collaborator Author

@nvuillam I need your help to finish this linter.
Following https://megalinter.io/latest/contributing/#add-a-new-linter I don't know what is missing !

see chunk of deployment/test workflow

=========================== short test summary info ============================
FAILED megalinter/tests/test_megalinter/linters/php_phpcsfixer_test.py::php_phpcsfixer_test::test_success
===== 1 failed, 594 passed, 336 skipped, 48 warnings in 614.33s (0:10:14) ======
Pytest exited 1

Test automation failed, but runtime is correct for me.

@nvuillam
Copy link
Member

@llaville it seems that phpcs fixer does not accept to find non fixable errors

❌ Linted [PHP] files with [php-cs-fixer]: Found 1 error(s) - (0.16s) (expand for details)
  - Using [php-cs-fixer v3.56.1] https://megalinter.io/features-php-cs-fixer/descriptors/php_php_cs_fixer
  - MegaLinter key: [PHP_PHPCSFIXER]
  - Rules config: [.php-cs-fixer.dist.php]
  --Error detail:
  PHP CS Fixer 3.56.1 15 Keys Accelerate by Fabien Potencier, Dariusz Ruminski and contributors.
  PHP runtime: 8.3.7
  Loaded config default from "/action/lib/.automation/.php-cs-fixer.dist.php".
  Using cache file ".php-cs-fixer.cache".
  
  Found 0 of 5 files that can be fixed in 0.001 seconds, 14.000 MB memory used
  
  Files that were not fixed due to errors reported during linting before fixing:
     1) ./php_bad_2.php
     2) ./php_bad_1.php

If the test files do not fit, you can create another test directory and redirect tests to it using descriptor property test_folder :)

@llaville
Copy link
Collaborator Author

@nvuillam After trying your tip, and see that it won't fix anything (sorry to said that), I've spent my time to track down where it came from.
So I've look deeper into MegaLinter to see how test are run with pytest, and see that :

And PHP-CS-Fixer in in same context

After applying patch below

diff --git a/megalinter/utilstest.py b/megalinter/utilstest.py
index 011c01c34d..7d6e2ad5aa 100644
--- a/megalinter/utilstest.py
+++ b/megalinter/utilstest.py
@@ -186,7 +186,7 @@ def test_linter_success(linter, test_self):
             )
         else:
             test_self.assertRegex(output, rf"\[{linter_name}\] .*good.* - SUCCESS")
-    elif linter.descriptor_id != "SPELL":  # This log doesn't appear in SPELL linters
+    elif linter.descriptor_id != "SPELL" or linter.linter_name != "php-cs-fixer":  # This log doesn't appear in SPELL linters
         test_self.assertRegex(
             output,
             rf"Linted \[{linter.descriptor_id}\] files with \[{linter_name}\] successfully",

I've rebuild the PHP flavor image, and run test again, in official megalinter/.automation/test/php directory

docker run -e TEST_CASE_RUN=true -e TEST_KEYWORDS="php_phpcsfixer" -e OUTPUT_FORMAT=text -e OUTPUT_DETAIL=detailed -e MEGALINTER_VOLUME_ROOT="."  --rm -v "/var/run/docker.sock:/var/run/docker.sock:rw" -v .:/tmp/lint 84d5825f0e277f834d6b5db15ffa392eb10a50cd97848a6fcd69449f463f4bac

And got expected results :

============================== slowest durations ===============================
1.74s call     tests/test_megalinter/linters/php_phpcsfixer_test.py::php_phpcsfixer_test::test_failure
0.71s call     tests/test_megalinter/linters/php_phpcsfixer_test.py::php_phpcsfixer_test::test_success
0.11s call     tests/test_megalinter/linters/php_phpcsfixer_test.py::php_phpcsfixer_test::test_get_linter_help
0.10s call     tests/test_megalinter/linters/php_phpcsfixer_test.py::php_phpcsfixer_test::test_get_linter_version
0.02s call     tests/test_megalinter/linters/php_phpcsfixer_test.py::php_phpcsfixer_test::test_format_fix
0.02s call     tests/test_megalinter/linters/php_phpcsfixer_test.py::php_phpcsfixer_test::test_report_tap
0.02s call     tests/test_megalinter/linters/php_phpcsfixer_test.py::php_phpcsfixer_test::test_report_sarif

(14 durations < 0.005s hidden.  Use -vv to show these durations.)
=================== 4 passed, 3 skipped, 6 warnings in 6.63s ===================
Pytest exited 0
Successfully executed Pytest

So I think we should think about a better fix than this quick one (even if it works fine) ! WDTY ?

@bdovaz
Copy link
Collaborator

bdovaz commented May 24, 2024

@nvuillam can you reply to @llaville to unlock this PR? I am also interested in this linter.

@llaville
Copy link
Collaborator Author

@bdovaz There is no more hurry now. I'll be slow to respond in next days because I'll have a slow internet connection !

@nvuillam
Copy link
Member

I'm a little bit lost ^^
How can I help ? ^^

@llaville
Copy link
Collaborator Author

@nvuillam You can help, either :

Because PHP-CS-Fixer run ok except for unit tests !

@nvuillam
Copy link
Member

@llaville I'm not proud to say ok, but... ok :D
The issue appears when multiple linters have the same beginning of name... I didn't find a clean way to solve that yet, so go with the workaround as I plan to release a new minor version this week-end :)

@llaville
Copy link
Collaborator Author

Will have to wait I return to my home with a better internet connection. I'm very limited now :(

@nvuillam
Copy link
Member

@llaville do you think it would be ok this week-end when I may release a new version ? ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants