-
Notifications
You must be signed in to change notification settings - Fork 160
#49: LocalizedException SHOULD only be thrown in View #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Sniffs\Exceptions; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
|
||
/** | ||
* Detects possible direct throws of Exceptions. | ||
*/ | ||
class LocalizedThrowSniff implements Sniff | ||
{ | ||
|
||
protected $presentationLayerKeys = ['Controller', 'Block', 'ViewModel']; | ||
|
||
/** | ||
* String representation of warning. | ||
*/ | ||
protected $warningMessage = 'LocalizedException SHOULD only be thrown in the Presentation layer.'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like tech guidelines must be updated to allow LocalizedException in Service Layer as well. See #5.17 in https://devdocs.magento.com/guides/v2.3/coding-standards/technical-guidelines.html Please do not merge this PR until tech guidelines are updated. |
||
|
||
/** | ||
* Warning violation code. | ||
* | ||
* @var string | ||
*/ | ||
protected $warningCode = 'LocalizedThrow'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function register() | ||
{ | ||
return [T_THROW]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
$lineEnd = $phpcsFile->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 | ||
); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Catalog\Controller\Product; | ||
|
||
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface; | ||
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface; | ||
use Magento\Framework\App\Action\Context; | ||
use Magento\Framework\View\Result\PageFactory; | ||
use Magento\Catalog\Controller\Product as ProductAction; | ||
|
||
/** | ||
* View a product on storefront. Needs to be accessible by POST because of the store switching. | ||
*/ | ||
class View extends ProductAction implements HttpGetActionInterface, HttpPostActionInterface | ||
{ | ||
/** | ||
* | ||
*/ | ||
public function execute() | ||
{ | ||
throw new LocalizedException(__('%1 doesn\'t exists')); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
/** | ||
* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Catalog\Model\Test\SubFolder; | ||
|
||
/** | ||
* Class Product | ||
*/ | ||
class Product | ||
{ | ||
public function execute() | ||
{ | ||
// rule should find this | ||
throw new LocalizedException(__('Cant find %1 doesn\'t exists')); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/** | ||
* | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Catalog\Block\Test\SubFolder; | ||
|
||
|
||
/** | ||
* Class Product | ||
*/ | ||
class Product | ||
{ | ||
public function execute() | ||
{ | ||
throw new LocalizedException(__('Cant find %1 doesn\'t exists')); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
/** | ||
* Class Product | ||
*/ | ||
class TestViewModel | ||
{ | ||
public function execute() | ||
{ | ||
throw new \Magento\Framework\Exception\LocalizedException(__('Oops')); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Magento\Catalog\Model\Test\SubFolder; | ||
|
||
class Foo | ||
{ | ||
public function execute() | ||
{ | ||
throw new \InvalidArgumentException(__('Oops')); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Tests\Exceptions; | ||
|
||
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; | ||
|
||
/** | ||
* Class NamespaceUnitTest | ||
*/ | ||
class LocalizedThrowUnitTest extends AbstractSniffUnitTest | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getErrorList() | ||
{ | ||
return [ | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getWarningList($testFile = '') | ||
{ | ||
if ($testFile === 'LocalizedThrowUnitTest.2.inc') { | ||
return [ | ||
18 => 1 | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,6 +203,10 @@ | |
<severity>7</severity> | ||
<type>warning</type> | ||
</rule> | ||
<rule ref="Magento.Exceptions.LocalizedThrow"> | ||
<severity>7</severity> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change the severity to
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for nit-picking, but could you please add rules in alphabetical order? So they will be ordered by severity then by name. |
||
<type>warning</type> | ||
</rule> | ||
<rule ref="Magento.PHP.LiteralNamespaces"> | ||
<severity>7</severity> | ||
<type>warning</type> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update docs