From d6fc34ba73f44317fae3db3b3fb462d355223549 Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Thu, 8 Aug 2019 23:47:32 +0530 Subject: [PATCH 1/5] Ignore warning for empty around plugin --- Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php | 16 ++++++++++++++++ .../Tests/CodeAnalysis/EmptyBlockUnitTest.inc | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php b/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php index 2f5b5cc1..107b107d 100644 --- a/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php +++ b/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php @@ -5,6 +5,7 @@ */ namespace Magento2\Sniffs\CodeAnalysis; +use PHP_CodeSniffer\Files\File; use PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis\EmptyStatementSniff; /** @@ -25,4 +26,19 @@ public function register() ] ); } + /** + * @inheritDoc + */ + public function process(File $phpcsFile, $stackPtr) + { + $posOfFunction = $phpcsFile->findNext([T_FUNCTION], $stackPtr); + $functionName = $phpcsFile->getDeclarationName($posOfFunction); + // Skip for around function + if (strpos($functionName, 'around') !== false) { + return; + } + + parent::process($phpcsFile, $stackPtr); + + }//end process() } diff --git a/Magento2/Tests/CodeAnalysis/EmptyBlockUnitTest.inc b/Magento2/Tests/CodeAnalysis/EmptyBlockUnitTest.inc index 2a50d42b..e0aa59ed 100644 --- a/Magento2/Tests/CodeAnalysis/EmptyBlockUnitTest.inc +++ b/Magento2/Tests/CodeAnalysis/EmptyBlockUnitTest.inc @@ -74,3 +74,7 @@ if (true) {} elseif (false) {} function emptyFunction () { /*Empty function block*/ } function nonEmptyFunction () { return true; } + +function aroundEmptyFunction ($foo, $bar) { + +} \ No newline at end of file From d2bd683860f98a9359b93261628c3448603012ff Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Thu, 8 Aug 2019 23:50:23 +0530 Subject: [PATCH 2/5] Minor tweaks --- Magento2/Tests/CodeAnalysis/EmptyBlockUnitTest.inc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Magento2/Tests/CodeAnalysis/EmptyBlockUnitTest.inc b/Magento2/Tests/CodeAnalysis/EmptyBlockUnitTest.inc index e0aa59ed..6fb834d8 100644 --- a/Magento2/Tests/CodeAnalysis/EmptyBlockUnitTest.inc +++ b/Magento2/Tests/CodeAnalysis/EmptyBlockUnitTest.inc @@ -75,6 +75,4 @@ function emptyFunction () { /*Empty function block*/ } function nonEmptyFunction () { return true; } -function aroundEmptyFunction ($foo, $bar) { - -} \ No newline at end of file +function aroundEmptyFunction ($foo, $bar) { } From f9dc5f9fcc6c0cafaa99c1bf0f8cfb7a53cbd504 Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Fri, 9 Aug 2019 08:19:22 +0530 Subject: [PATCH 3/5] Feedback changes --- Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php b/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php index 107b107d..68a66eee 100644 --- a/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php +++ b/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php @@ -31,10 +31,13 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - $posOfFunction = $phpcsFile->findNext([T_FUNCTION], $stackPtr); - $functionName = $phpcsFile->getDeclarationName($posOfFunction); - // Skip for around function - if (strpos($functionName, 'around') !== false) { + $tokens = $phpcsFile->getTokens(); + $posOfString = $phpcsFile->findNext(T_STRING, $stackPtr); + $stringContent = $tokens[$posOfString]['content']; + /** Check if function starts with around and also checked if string length + * greater than 6 so that exact blank function name 'around()' give us warning + */ + if (substr($stringContent, 0, 6) === "around" && strlen($stringContent) > 6) { return; } From 917fe53992535e86d76509dd120df9723d47f65a Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Fri, 9 Aug 2019 08:31:13 +0530 Subject: [PATCH 4/5] Fixed static build tests --- Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php b/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php index 68a66eee..212fc8f9 100644 --- a/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php +++ b/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php @@ -42,6 +42,5 @@ public function process(File $phpcsFile, $stackPtr) } parent::process($phpcsFile, $stackPtr); - }//end process() } From 8ef3eeb12402ce42b5dae0ffb3b8b19a4998949f Mon Sep 17 00:00:00 2001 From: Shankar Konar Date: Fri, 9 Aug 2019 23:07:07 +0530 Subject: [PATCH 5/5] Feedback changes --- Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php b/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php index 212fc8f9..9e6ae160 100644 --- a/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php +++ b/Magento2/Sniffs/CodeAnalysis/EmptyBlockSniff.php @@ -32,12 +32,8 @@ public function register() public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $posOfString = $phpcsFile->findNext(T_STRING, $stackPtr); - $stringContent = $tokens[$posOfString]['content']; - /** Check if function starts with around and also checked if string length - * greater than 6 so that exact blank function name 'around()' give us warning - */ - if (substr($stringContent, 0, 6) === "around" && strlen($stringContent) > 6) { + if ($tokens[$stackPtr]['code'] === T_FUNCTION && + strpos($phpcsFile->getDeclarationName($stackPtr), 'around') === 0) { return; }