From d5c1bb65a80fd83faee53be70c10f5e3cf9247e6 Mon Sep 17 00:00:00 2001 From: Lars Roettig Date: Tue, 19 Feb 2019 23:51:07 +0100 Subject: [PATCH 1/3] 24: [New Rule] Static methods SHOULD NOT be used --- .../Sniffs/Functions/StaticFunctionSniff.php | 61 +++++++++++++++++++ .../Functions/StaticFunctionUnitTest.inc | 26 ++++++++ .../Functions/StaticFunctionUnitTest.php | 34 +++++++++++ Magento/ruleset.xml | 3 + 4 files changed, 124 insertions(+) create mode 100644 Magento/Sniffs/Functions/StaticFunctionSniff.php create mode 100644 Magento/Tests/Functions/StaticFunctionUnitTest.inc create mode 100644 Magento/Tests/Functions/StaticFunctionUnitTest.php diff --git a/Magento/Sniffs/Functions/StaticFunctionSniff.php b/Magento/Sniffs/Functions/StaticFunctionSniff.php new file mode 100644 index 00000000..cf61c299 --- /dev/null +++ b/Magento/Sniffs/Functions/StaticFunctionSniff.php @@ -0,0 +1,61 @@ +findNext(T_FUNCTION, $stackPtr) + 1; + $tokens = array_slice($phpcsFile->getTokens(), $stackPtr, $posOfFunction - $stackPtr); + + $allowedTypes = [T_STATIC => true, T_WHITESPACE => true, T_FUNCTION => true]; + + foreach ($tokens as $token) { + + $code = $token['code']; + if (!array_key_exists($code, $allowedTypes)) { + break; + } + + if ($code === T_FUNCTION) { + $phpcsFile->addWarning($this->warningMessage, $posOfFunction, $this->warningCode); + } + } + } +} diff --git a/Magento/Tests/Functions/StaticFunctionUnitTest.inc b/Magento/Tests/Functions/StaticFunctionUnitTest.inc new file mode 100644 index 00000000..d07cc6fc --- /dev/null +++ b/Magento/Tests/Functions/StaticFunctionUnitTest.inc @@ -0,0 +1,26 @@ + 1, + 17 => 1 + ]; + } +} diff --git a/Magento/ruleset.xml b/Magento/ruleset.xml index b260bad4..e85c2930 100644 --- a/Magento/ruleset.xml +++ b/Magento/ruleset.xml @@ -72,6 +72,9 @@ 8 + + 6 + From e453814c3ef331cc30c1ca136061bef16edd1a76 Mon Sep 17 00:00:00 2001 From: Lars Roettig Date: Wed, 20 Feb 2019 16:38:17 +0100 Subject: [PATCH 2/3] #24 Review and static fixes --- Magento/Sniffs/Functions/StaticFunctionSniff.php | 2 +- Magento/Tests/Functions/StaticFunctionUnitTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Magento/Sniffs/Functions/StaticFunctionSniff.php b/Magento/Sniffs/Functions/StaticFunctionSniff.php index cf61c299..0ec96c6d 100644 --- a/Magento/Sniffs/Functions/StaticFunctionSniff.php +++ b/Magento/Sniffs/Functions/StaticFunctionSniff.php @@ -19,7 +19,7 @@ class StaticFunctionSniff implements Sniff * * @var string */ - protected $warningMessage = 'The use of `static` methods is discouraged'; + protected $warningMessage = 'Static method cannot be intercepted and its use is discouraged.'; /** * Warning violation code. diff --git a/Magento/Tests/Functions/StaticFunctionUnitTest.php b/Magento/Tests/Functions/StaticFunctionUnitTest.php index 39f735aa..ca2763c4 100644 --- a/Magento/Tests/Functions/StaticFunctionUnitTest.php +++ b/Magento/Tests/Functions/StaticFunctionUnitTest.php @@ -3,7 +3,6 @@ * Copyright © Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Tests\Functions; use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; From 59c0d4ffee7978ef7496116c32d51a219781e833 Mon Sep 17 00:00:00 2001 From: Lars Roettig Date: Wed, 20 Feb 2019 16:40:34 +0100 Subject: [PATCH 3/3] #24 Review and static fixes --- Magento/Sniffs/Functions/StaticFunctionSniff.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/Magento/Sniffs/Functions/StaticFunctionSniff.php b/Magento/Sniffs/Functions/StaticFunctionSniff.php index 0ec96c6d..2a6e537f 100644 --- a/Magento/Sniffs/Functions/StaticFunctionSniff.php +++ b/Magento/Sniffs/Functions/StaticFunctionSniff.php @@ -45,9 +45,7 @@ public function process(File $phpcsFile, $stackPtr) $tokens = array_slice($phpcsFile->getTokens(), $stackPtr, $posOfFunction - $stackPtr); $allowedTypes = [T_STATIC => true, T_WHITESPACE => true, T_FUNCTION => true]; - foreach ($tokens as $token) { - $code = $token['code']; if (!array_key_exists($code, $allowedTypes)) { break;