From 5e7970bfab4ffaa4449e0822a1d4e49bfae95494 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 30 Aug 2023 13:02:31 +0200 Subject: [PATCH 1/7] Remove Neutron Standard - Added custom DisableCallUserFuncSniff (with tests) - Added custom DisableMagicSerializeSniff (with tests) - Added custom NoRootNamespaceFunctionsSniff (with tests) - Added Slevomat standard as dependency - Explicilty required PHPCSExtra and PHPCS Utils - Replaced some Netron sniffs with Generic/Squid/PHPCSExtra/Slevomat - Renamed updated WPCS standards - Improved QA --- .psalm/autoloader.php | 14 --- Inpsyde/PhpcsHelpers.php | 3 +- .../CodeQuality/DisableCallUserFuncSniff.php | 49 ++++++++ .../DisableMagicSerializeSniff.php | 74 +++++++++++ .../NoRootNamespaceFunctionsSniff.php | 71 +++++++++++ Inpsyde/ruleset.xml | 80 +++++++----- composer.json | 7 +- phpcs.xml | 116 +----------------- psalm.xml | 2 +- tests/autoload.php | 17 +++ tests/bootstrap.php | 4 +- tests/fixtures/disable-call-user-func.php | 22 ++++ tests/fixtures/disallow-magic-serialize.php | 28 +++++ .../no-root-namespace-functions-multi.php | 31 +++++ .../no-root-namespace-functions-single.php | 14 +++ 15 files changed, 365 insertions(+), 167 deletions(-) delete mode 100644 .psalm/autoloader.php create mode 100644 Inpsyde/Sniffs/CodeQuality/DisableCallUserFuncSniff.php create mode 100644 Inpsyde/Sniffs/CodeQuality/DisableMagicSerializeSniff.php create mode 100644 Inpsyde/Sniffs/CodeQuality/NoRootNamespaceFunctionsSniff.php create mode 100644 tests/autoload.php create mode 100644 tests/fixtures/disable-call-user-func.php create mode 100644 tests/fixtures/disallow-magic-serialize.php create mode 100644 tests/fixtures/no-root-namespace-functions-multi.php create mode 100644 tests/fixtures/no-root-namespace-functions-single.php diff --git a/.psalm/autoloader.php b/.psalm/autoloader.php deleted file mode 100644 index 86ace84..0000000 --- a/.psalm/autoloader.php +++ /dev/null @@ -1,14 +0,0 @@ -> + * + * phpcs:disable Inpsyde.CodeQuality.NoAccessors + */ + public function getGroups(): array + { + // phpcs:enable Inpsyde.CodeQuality.NoAccessors + return [ + 'call_user_func' => [ + 'type' => 'error', + 'message' => 'Usage of %s() is forbidden.', + 'functions' => [ + 'call_user_func', + 'call_user_func_array', + 'suca', + ], + ], + ]; + } +} diff --git a/Inpsyde/Sniffs/CodeQuality/DisableMagicSerializeSniff.php b/Inpsyde/Sniffs/CodeQuality/DisableMagicSerializeSniff.php new file mode 100644 index 0000000..2d8180a --- /dev/null +++ b/Inpsyde/Sniffs/CodeQuality/DisableMagicSerializeSniff.php @@ -0,0 +1,74 @@ + */ + public array $disabledFunctions = [ + '__serialize', + '__sleep', + '__unserialize', + '__wakeup', + ]; + + /** + * @return list + */ + public function register(): array + { + return [T_FUNCTION]; + } + + /** + * @param File $phpcsFile + * @param int $stackPtr + * @return void + * + * phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration + */ + public function process(File $phpcsFile, $stackPtr) + { + // phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration + if (!Scopes::isOOMethod($phpcsFile, $stackPtr)) { + return; + } + + $name = FunctionDeclarations::getName($phpcsFile, $stackPtr); + if (in_array($name, $this->disabledFunctions, true)) { + $phpcsFile->addError( + sprintf( + 'The method "%s" is forbidden, please use Serializable interface.', + $name + ), + $stackPtr, + 'Found' + ); + } + } +} diff --git a/Inpsyde/Sniffs/CodeQuality/NoRootNamespaceFunctionsSniff.php b/Inpsyde/Sniffs/CodeQuality/NoRootNamespaceFunctionsSniff.php new file mode 100644 index 0000000..3c8ad58 --- /dev/null +++ b/Inpsyde/Sniffs/CodeQuality/NoRootNamespaceFunctionsSniff.php @@ -0,0 +1,71 @@ + + */ + public function register(): array + { + return [T_FUNCTION]; + } + + /** + * @param File $phpcsFile + * @param int $stackPtr + * @return void + * + * phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration + */ + public function process(File $phpcsFile, $stackPtr): void + { + // phpcs:enable Inpsyde.CodeQuality.ArgumentTypeDeclaration + if (Scopes::isOOMethod($phpcsFile, $stackPtr)) { + return; + } + + $namespace = Namespaces::determineNamespace($phpcsFile, $stackPtr); + if ($namespace !== '') { + return; + } + $name = FunctionDeclarations::getName($phpcsFile, $stackPtr); + if (!$name) { + return; + } + + $message = sprintf('The function "%s" is in root namespace.', $name); + + $phpcsFile->addError($message, $stackPtr, 'Found'); + } +} diff --git a/Inpsyde/ruleset.xml b/Inpsyde/ruleset.xml index c70db7a..5123cb8 100644 --- a/Inpsyde/ruleset.xml +++ b/Inpsyde/ruleset.xml @@ -1,5 +1,5 @@ - + PHP 7+ coding standards for Inpsyde WordPress projects. @@ -13,45 +13,22 @@ - - - warning - - - warning - - - warning - - - warning - - - warning - - - warning - - - warning - - - - - + + + + + + @@ -81,7 +58,6 @@ - @@ -131,10 +107,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + @@ -144,6 +148,13 @@ + + + + + + + @@ -156,6 +167,13 @@ + + + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/psalm.xml b/psalm.xml index 53e46c1..7399676 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,7 +1,7 @@ Date: Wed, 30 Aug 2023 13:12:26 +0200 Subject: [PATCH 2/7] Fix CS --- Inpsyde/ruleset.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Inpsyde/ruleset.xml b/Inpsyde/ruleset.xml index 5123cb8..d51b281 100644 --- a/Inpsyde/ruleset.xml +++ b/Inpsyde/ruleset.xml @@ -153,8 +153,8 @@ - - + + From 7a52274ffdf59c502941306eec0f8afa9772f709 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 30 Aug 2023 13:14:44 +0200 Subject: [PATCH 3/7] Run QA on ruleset changes --- .github/workflows/quality-assurance-php.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/quality-assurance-php.yml b/.github/workflows/quality-assurance-php.yml index cee505f..d5dfc5e 100644 --- a/.github/workflows/quality-assurance-php.yml +++ b/.github/workflows/quality-assurance-php.yml @@ -8,6 +8,7 @@ on: - '**phpcs.xml.dist' - '**phpunit.xml.dist' - '**psalm.xml' + - '**ruleset.xml' workflow_dispatch: inputs: jobs: From aa133bf9a1ae9f13e9bf43d44452e8df10090f54 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 30 Aug 2023 15:16:57 +0200 Subject: [PATCH 4/7] Move dependabot.yml to .gitattributes See #67 --- dependabot.yml => .github/dependabot.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename dependabot.yml => .github/dependabot.yml (100%) diff --git a/dependabot.yml b/.github/dependabot.yml similarity index 100% rename from dependabot.yml rename to .github/dependabot.yml From 27685e382056f398d3d95bb66142f5b4253fe529 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 30 Aug 2023 23:43:15 +0200 Subject: [PATCH 5/7] Remove debug leftover --- Inpsyde/Sniffs/CodeQuality/DisableCallUserFuncSniff.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Inpsyde/Sniffs/CodeQuality/DisableCallUserFuncSniff.php b/Inpsyde/Sniffs/CodeQuality/DisableCallUserFuncSniff.php index 5220d02..9f6428c 100644 --- a/Inpsyde/Sniffs/CodeQuality/DisableCallUserFuncSniff.php +++ b/Inpsyde/Sniffs/CodeQuality/DisableCallUserFuncSniff.php @@ -41,7 +41,6 @@ public function getGroups(): array 'functions' => [ 'call_user_func', 'call_user_func_array', - 'suca', ], ], ]; From a385aee01e8a6d8870fcb625627db70fbef034c0 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Wed, 30 Aug 2023 23:44:30 +0200 Subject: [PATCH 6/7] Fix typo in fixture Co-authored-by: Thorsten Frommen Signed-off-by: Giuseppe Mazzapica --- tests/fixtures/disable-call-user-func.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fixtures/disable-call-user-func.php b/tests/fixtures/disable-call-user-func.php index db6e97a..7f2374a 100644 --- a/tests/fixtures/disable-call-user-func.php +++ b/tests/fixtures/disable-call-user-func.php @@ -15,7 +15,7 @@ function test() { class Foo { - pribate function test() { + private function test() { // @phpcsErrorOnNextLine return call_user_func_array('strtolower', ['foo']); } From eb1a128afb68e161929b701ec1da38c33c2f8f9c Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Thu, 31 Aug 2023 11:09:07 +0200 Subject: [PATCH 7/7] Add more tests for DisableMagicSerialize --- tests/fixtures/disallow-magic-serialize.php | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/fixtures/disallow-magic-serialize.php b/tests/fixtures/disallow-magic-serialize.php index 27938f2..cf7f2ea 100644 --- a/tests/fixtures/disallow-magic-serialize.php +++ b/tests/fixtures/disallow-magic-serialize.php @@ -25,4 +25,26 @@ public function sleep(): array { return []; } + + // @phpcsErrorOnNextLine + public function __wakeup(): array + { + return []; + } + + public function wakeup(): array + { + return []; + } + + // @phpcsErrorOnNextLine + public function __unserialize(): array + { + return []; + } + + public function unserialize(): array + { + return []; + } }