From 7155434a935aa80c576fd590cb14bdf653867c78 Mon Sep 17 00:00:00 2001 From: Lars Roettig Date: Sun, 31 Mar 2019 17:34:08 +0200 Subject: [PATCH 1/5] #21: Impelement rule --- .../TryProcessSystemResourcesSniff.php | 93 +++++++++++++++++++ .../TryProcessSystemResourcesUnitTest.inc | 45 +++++++++ .../TryProcessSystemResourcesUnitTest.php | 36 +++++++ Magento2/ruleset.xml | 4 + output.txt | 4 + 5 files changed, 182 insertions(+) create mode 100644 Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php create mode 100644 Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc create mode 100644 Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.php create mode 100644 output.txt diff --git a/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php b/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php new file mode 100644 index 00000000..90984475 --- /dev/null +++ b/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php @@ -0,0 +1,93 @@ +getTokens(); + $match = $tokens[$stackPtr]['content']; + + foreach ($this->functions as $function) { + if (strpos($match, $function) === false) { + continue; + } + + $isSystemResource = true; + break; + } + + if (false === $isSystemResource) { + // Probably no a system resource no check + return; + } + + $tryPosition = $phpcsFile->findPrevious(T_TRY, $stackPtr - 1); + + if ($tryPosition !== false) { + $tryTag = $tokens[$tryPosition]; + $start = $tryTag['scope_opener']; + $end = $tryTag['scope_closer']; + if ($stackPtr > $start && $stackPtr < $end) { + // element is warped by try no check required + return; + } + } + + $phpcsFile->addWarning( + $this->warningMessage, + $stackPtr, + $this->warningCode + ); + } +} diff --git a/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc b/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc new file mode 100644 index 00000000..2ada0371 --- /dev/null +++ b/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc @@ -0,0 +1,45 @@ + 1, + 38 => 1, + 40 => 1, + 42 => 1, + 44 => 1 + ]; + } +} diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index 8da1d608..0ba3a32e 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -125,6 +125,10 @@ 8 warning + + 7 + warning + 8 warning diff --git a/output.txt b/output.txt new file mode 100644 index 00000000..ebeabc99 --- /dev/null +++ b/output.txt @@ -0,0 +1,4 @@ +ERROR: The specified sniff code "Magento2.Exceptions" is invalid + +Run "phpcs --help" for usage information + From d49b390f4a6b6f64fa25d9945feff956d1abea9c Mon Sep 17 00:00:00 2001 From: Lars Roettig Date: Sun, 7 Apr 2019 15:24:55 +0200 Subject: [PATCH 2/5] #21 Review Fixes --- Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php | 3 +-- .../Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc | 4 ++-- output.txt | 4 ---- 3 files changed, 3 insertions(+), 8 deletions(-) delete mode 100644 output.txt diff --git a/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php b/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php index 90984475..92ccfa7f 100644 --- a/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php +++ b/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php @@ -3,7 +3,6 @@ * Copyright © Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento2\Sniffs\Exceptions; use function array_slice; @@ -20,7 +19,7 @@ class TryProcessSystemResourcesSniff implements Sniff * * @var string */ - protected $warningMessage = 'All resources SHOULD be properly released.'; + protected $warningMessage = 'The code MUST be wrapped with a try block if the method uses system resources .'; /** * Warning violation code. diff --git a/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc b/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc index 2ada0371..e0e077c7 100644 --- a/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc +++ b/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc @@ -6,7 +6,7 @@ use Exception; class StreamHandler { - public function handleExpection() + public function handleException() { try { $strChar = stream_get_contents(STDIN, 1); @@ -15,7 +15,7 @@ class StreamHandler } } - public function exetcute() + public function execute() { // Rule find: Try block detected when processing system resources $strChar = stream_get_contents(STDIN, 1); diff --git a/output.txt b/output.txt deleted file mode 100644 index ebeabc99..00000000 --- a/output.txt +++ /dev/null @@ -1,4 +0,0 @@ -ERROR: The specified sniff code "Magento2.Exceptions" is invalid - -Run "phpcs --help" for usage information - From 5f8ca0e3721d37cd833eaf6cf7c9f325285cc747 Mon Sep 17 00:00:00 2001 From: Lars Roettig Date: Thu, 20 Jun 2019 13:36:18 +0200 Subject: [PATCH 3/5] #21 Requested code review chnages --- .../TryProcessSystemResourcesSniff.php | 1 - .../TryProcessSystemResourcesUnitTest.inc | 16 ---------------- .../TryProcessSystemResourcesUnitTest.php | 9 ++++----- 3 files changed, 4 insertions(+), 22 deletions(-) diff --git a/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php b/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php index 92ccfa7f..2875a8e7 100644 --- a/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php +++ b/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php @@ -36,7 +36,6 @@ class TryProcessSystemResourcesSniff implements Sniff protected $functions = [ 'stream_', 'socket_', - 'curl_' ]; /** diff --git a/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc b/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc index e0e077c7..66f23840 100644 --- a/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc +++ b/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.inc @@ -14,22 +14,6 @@ class StreamHandler } } - - public function execute() - { - // Rule find: Try block detected when processing system resources - $strChar = stream_get_contents(STDIN, 1); - - try { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); - curl_setopt($ch, CURLOPT_HEADER, 0); - curl_exec($ch); - curl_close($ch); - } catch (Exception $exception) { - - } - } } function executeStream() diff --git a/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.php b/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.php index 66ec4c17..3496e00d 100644 --- a/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.php +++ b/Magento2/Tests/Exceptions/TryProcessSystemResourcesUnitTest.php @@ -26,11 +26,10 @@ protected function getErrorList() protected function getWarningList() { return [ - 21 => 1, - 38 => 1, - 40 => 1, - 42 => 1, - 44 => 1 + 22 => 1, + 24 => 1, + 26 => 1, + 28 => 1 ]; } } From 859e563d711eae865df14c0100cf2813a1e715a0 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Fri, 16 Aug 2019 16:26:23 -0500 Subject: [PATCH 4/5] magento/magento-coding-standard#21: [New Rule] No try block detected when processing system resources --- .../TryProcessSystemResourcesSniff.php | 52 +++++++------------ Magento2/ruleset.xml | 2 +- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php b/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php index 2875a8e7..70fcfabe 100644 --- a/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php +++ b/Magento2/Sniffs/Exceptions/TryProcessSystemResourcesSniff.php @@ -10,7 +10,7 @@ use PHP_CodeSniffer\Files\File; /** - * Detects no try block detected when processing system resources + * Detects missing try-catch block when processing system resources. */ class TryProcessSystemResourcesSniff implements Sniff { @@ -19,17 +19,17 @@ class TryProcessSystemResourcesSniff implements Sniff * * @var string */ - protected $warningMessage = 'The code MUST be wrapped with a try block if the method uses system resources .'; + protected $warningMessage = 'The code must be wrapped with a try block if the method uses system resources.'; /** * Warning violation code. * * @var string */ - protected $warningCode = ' TryProcessSystem'; + protected $warningCode = 'MissingTryCatch'; /** - * Searched functions. + * Search for functions that start with. * * @var array */ @@ -51,41 +51,29 @@ public function register() */ public function process(File $phpcsFile, $stackPtr) { - $isSystemResource = false; - $tokens = $phpcsFile->getTokens(); - $match = $tokens[$stackPtr]['content']; foreach ($this->functions as $function) { - if (strpos($match, $function) === false) { + if (strpos($tokens[$stackPtr]['content'], $function) !== 0) { continue; } + $tryPosition = $phpcsFile->findPrevious(T_TRY, $stackPtr - 1); - $isSystemResource = true; - break; - } - - if (false === $isSystemResource) { - // Probably no a system resource no check - return; - } - - $tryPosition = $phpcsFile->findPrevious(T_TRY, $stackPtr - 1); - - if ($tryPosition !== false) { - $tryTag = $tokens[$tryPosition]; - $start = $tryTag['scope_opener']; - $end = $tryTag['scope_closer']; - if ($stackPtr > $start && $stackPtr < $end) { - // element is warped by try no check required - return; + if ($tryPosition !== false) { + $tryTag = $tokens[$tryPosition]; + $start = $tryTag['scope_opener']; + $end = $tryTag['scope_closer']; + if ($stackPtr > $start && $stackPtr < $end) { + // element is warped by try no check required + return; + } } - } - $phpcsFile->addWarning( - $this->warningMessage, - $stackPtr, - $this->warningCode - ); + $phpcsFile->addWarning( + $this->warningMessage, + $stackPtr, + $this->warningCode + ); + } } } diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index ab2e812c..2af35a7c 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -154,7 +154,7 @@ warning - 7 + 8 warning From 8a7ff839729912732880c513d409e6a308df45b0 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Fri, 16 Aug 2019 16:28:12 -0500 Subject: [PATCH 5/5] magento/magento-coding-standard#21: [New Rule] No try block detected when processing system resources - added test excludes --- Magento2/ruleset.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Magento2/ruleset.xml b/Magento2/ruleset.xml index f9b583e3..df66ac69 100644 --- a/Magento2/ruleset.xml +++ b/Magento2/ruleset.xml @@ -156,6 +156,10 @@ 8 warning + */_files/* + */Fixtures/* + */Test/* + *Test.php 8