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 6c659ec2..382ea5fc 100644 --- a/Magento/ruleset.xml +++ b/Magento/ruleset.xml @@ -203,6 +203,10 @@ 7 warning + + 7 + warning + 7 warning