From f2bb5ec52fbc518f68799ee0cefda662ab22c66d Mon Sep 17 00:00:00 2001 From: Lars Roettig Date: Sun, 3 Mar 2019 23:23:10 +0100 Subject: [PATCH] #49 Impl. LocalizedException SHOULD only be thrown in View --- .../Sniffs/Exceptions/LocalizedThrowSniff.php | 78 +++++++++++++++++++ .../Exceptions/LocalizedThrowUnitTest.1.inc | 28 +++++++ .../Exceptions/LocalizedThrowUnitTest.2.inc | 22 ++++++ .../Exceptions/LocalizedThrowUnitTest.3.inc | 20 +++++ .../Exceptions/LocalizedThrowUnitTest.4.inc | 14 ++++ .../Exceptions/LocalizedThrowUnitTest.5.inc | 13 ++++ .../Exceptions/LocalizedThrowUnitTest.php | 38 +++++++++ Magento/ruleset.xml | 4 + 8 files changed, 217 insertions(+) create mode 100644 Magento/Sniffs/Exceptions/LocalizedThrowSniff.php create mode 100644 Magento/Tests/Exceptions/LocalizedThrowUnitTest.1.inc create mode 100644 Magento/Tests/Exceptions/LocalizedThrowUnitTest.2.inc create mode 100644 Magento/Tests/Exceptions/LocalizedThrowUnitTest.3.inc create mode 100644 Magento/Tests/Exceptions/LocalizedThrowUnitTest.4.inc create mode 100644 Magento/Tests/Exceptions/LocalizedThrowUnitTest.5.inc create mode 100644 Magento/Tests/Exceptions/LocalizedThrowUnitTest.php diff --git a/Magento/Sniffs/Exceptions/LocalizedThrowSniff.php b/Magento/Sniffs/Exceptions/LocalizedThrowSniff.php new file mode 100644 index 00000000..416a1b4e --- /dev/null +++ b/Magento/Sniffs/Exceptions/LocalizedThrowSniff.php @@ -0,0 +1,78 @@ +findNext(T_OPEN_PARENTHESIS, $stackPtr); + $exceptionType = $phpcsFile->getTokensAsString($stackPtr, $lineEnd); + + // throw is not a LocalizedException nothing to do + if (strpos($exceptionType, 'LocalizedException') === false) { + return; + } + + $nameSpaceTag = $phpcsFile->findPrevious(T_NAMESPACE, $stackPtr, 0); + if ($nameSpaceTag === false) { + // no namespace nothing to do + return; + } + + $lineEnd = $phpcsFile->findNext(T_SEMICOLON, $nameSpaceTag); + $nameSpaceString = $phpcsFile->getTokensAsString($nameSpaceTag, $lineEnd); + + $isPresentationLayer = false; + foreach ($this->presentationLayerKeys as $layerKey) { + if (strpos($nameSpaceString, $layerKey) !== false) { + $isPresentationLayer = true; + break; + } + } + + if (!$isPresentationLayer) { + $phpcsFile->addWarning( + $this->warningMessage, + $stackPtr, + $this->warningCode + ); + } + } +} diff --git a/Magento/Tests/Exceptions/LocalizedThrowUnitTest.1.inc b/Magento/Tests/Exceptions/LocalizedThrowUnitTest.1.inc new file mode 100644 index 00000000..61dc361d --- /dev/null +++ b/Magento/Tests/Exceptions/LocalizedThrowUnitTest.1.inc @@ -0,0 +1,28 @@ + 1 + ]; + } + + return []; + } +} diff --git a/Magento/ruleset.xml b/Magento/ruleset.xml index 40336962..7a635703 100644 --- a/Magento/ruleset.xml +++ b/Magento/ruleset.xml @@ -195,6 +195,10 @@ 7 warning + + 7 + warning + 7 warning