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

AC-1895: Avoid having duplicated issues in magento-coding-standard #362

Conversation

svera
Copy link
Contributor

@svera svera commented Jan 14, 2022

There are a couple of sniffs duplicated between PHPCS and PHP Compatibility. I've picked the ones with better coverage after doing some tests.

Excluded Kept Description
Generic.PHP.DeprecatedFunctions PHPCompatibility.FunctionUse.RemovedFunctions Excluded one only catches deprecations and, due to it using get_defined_functions() to get deprecated methods, it doesn't include functions coming from extensions or deprecations from different PHP versions than the one which is running the sniff.
PHPCompatibility.Syntax.ForbiddenCallTimePassByReference Generic.Functions.CallTimePassByReference Both are equivalent, there is no special advantage of one over the other; in fact, call-time pass-by-reference is forbidden since PHP 5.4 so we can remove both checks.

@@ -16,11 +16,6 @@
<exclude-pattern>*\.phtml$</exclude-pattern>
<exclude-pattern>*\.xml$</exclude-pattern>
</rule>
<rule ref="Generic.PHP.DeprecatedFunctions">
Copy link
Contributor Author

@svera svera Jan 14, 2022

Choose a reason for hiding this comment

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

This only catches deprecations and, due to it using get_defined_functions()to get deprecated methods, its coverage is not as good as the solution from PHPCompatibility because it doesn't include functions coming from extensions or deprecations from different PHP versions than the one which is running the sniff.

Copy link
Member

@sivaschenko sivaschenko left a comment

Choose a reason for hiding this comment

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

@svera can you please add information describing which sniff was excluded/removed in favour to which sniff, what are the difference between them and pros/cons. This should help to understand this decision better. Thank you!

@svera
Copy link
Contributor Author

svera commented Jan 18, 2022

@sivaschenko adressed in PR description

@svera svera requested a review from sivaschenko January 18, 2022 15:57
@sivaschenko
Copy link
Member

Grouped excluded PHPCompatibility.Syntax.ForbiddenCallTimePassByReference and set correct error 10 severity for PHPCompatibility.FunctionUse.RemovedFunctions

72be356

@sivaschenko
Copy link
Member

@magento import pr to magento-commerce/magento-coding-standard

@m2-github-services
Copy link
Contributor

@sivaschenko the Pull Request is successfully imported.

@tuyennn
Copy link
Member

tuyennn commented Apr 19, 2022

@sivaschenko any idea while I still got

phpcs: ERROR: Referenced sniff "PHPCompatibility.FunctionUse.RemovedFunctions" does not exist 

On my IDE
Edit: tag version v21

@rafafortes
Copy link

@tuyennn I'm also having this issue. Did you manage to resolve this?

@sivaschenko
Copy link
Member

@tuyennn @rafafortes I believe you need to register PHPCompatibility standard:

For example from magento project root vendor/bin/phpcs --config-set installed_paths ../../..,../../phpcompatibility/php-compatibility/PHPCompatibility (or adjust the paths according to working directory)

That is handled for Magento by post-install-cmd and post-update-cmd in composer.json, but may not be automatically handled for other specific installations

@rafafortes
Copy link

@sivaschenko it resolved the issue through the CLI but PHPStorm still complains about it, that's odd.

@Vinai
Copy link

Vinai commented May 11, 2022

I'm running into the same issue @rafafortes.
PHPCompatibility is properly registered in vendor/squizlabs/php_codesniffer/CodeSniffer.conf:

<?php
 $phpCodeSnifferConfig = array (
  'installed_paths' => '../../magento/magento-coding-standard,../../phpcompatibility/php-compatibility',
)
?>

I've checked that PHPStorm is indeed running the sniff from the Magento base directory as the current working directory (just like when running it from the command line).

Do you have any other ideas what might be causing the difference between running phpcs from the CLI and from PHPStorm?

EDIT: I opened a new issue about this: #390

@rafafortes
Copy link

@Vinai I've tried things like adding more installed_paths and different phpcs binaries (from OS, Docker containers) and none of this resolved the issue so far. The PHPStorm debug messages don't help also so I'm running out of ideas

@tuyennn
Copy link
Member

tuyennn commented May 13, 2022

@rafafortes here how I resolved the annoying PHPStorm errors with new magento codding standard.

1 . From project I remove the current require-dev magento codding standard. We aim to use the external magento codding standard outside project because the one from vendor was always deprecated, since we cannot keep the one from vendor run along with external up-to-date magento codding standard source

composer remove magento/magento-coding-standard
  1. Uncheck Installed standard path from PHPStorm and from the root of current project do
 vendor/bin/phpcs --config-set installed_paths  your_local_external_path_to/magento-coding-standard,../../phpcompatibility/php-compatibility

For example

 vendor/bin/phpcs --config-set installed_paths ~/Projects/magento-coding-standard,../../phpcompatibility/php-compatibility

@Vinai
Copy link

Vinai commented May 16, 2022

@rafafortes To debug I've added a require 'debug.php'; to vendor/squizlabs/php_codesniffer/CodeSniffer.conf.
Even though the file ends with .conf, it's loaded with include or require.
Then I added a debug.php and started to add debug statements and breakpoints.

@rafafortes
Copy link

Thanks for the tip, @Vinai. I'm able to debug it when running phpcs from CLI only. Did you manage to trigger the debug when inspecting code from PHPStorm UI?
Screenshot_20220523_112621

@Vinai
Copy link

Vinai commented May 24, 2022

I don't remember. Will have to check again. Using a PHP interpreter config with XDEBUG and starting the debug listener in PHPStorm should work, but I am not sure.
If that doesn't work, using var_dump'n'die and debug_backtrace is always an oldschool option I guess :)

@rafafortes
Copy link

Thank you @Vinai . The old and good var_dump helped me out!

I realized the $resolvedInstalledPaths array was missing the path: '/opt/project/vendor/phpcompatibility/php-compatibility/PHPCompatibility';

As a quick fix I ended up adding the missing path manually to the array as follows:

// vendor/squizlabs/php_codesniffer/src/Util/Standards.php
$resolvedInstalledPaths[] = '/opt/project/vendor/phpcompatibility/php-compatibility/PHPCompatibility';
return $resolvedInstalledPaths;

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

Successfully merging this pull request may close these issues.

7 participants