Skip to content

#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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Magento/Sniffs/Exceptions/LocalizedThrowSniff.php
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.
Copy link
Contributor

@paliarush paliarush Mar 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update docs

*/
class LocalizedThrowSniff implements Sniff
{

protected $presentationLayerKeys = ['Controller', 'Block', 'ViewModel'];

/**
* String representation of warning.
*/
protected $warningMessage = 'LocalizedException SHOULD only be thrown in the Presentation layer.';
Copy link
Contributor

Choose a reason for hiding this comment

The 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
);
}
}
}
28 changes: 28 additions & 0 deletions Magento/Tests/Exceptions/LocalizedThrowUnitTest.1.inc
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'));
}
}
22 changes: 22 additions & 0 deletions Magento/Tests/Exceptions/LocalizedThrowUnitTest.2.inc
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'));
}
}


20 changes: 20 additions & 0 deletions Magento/Tests/Exceptions/LocalizedThrowUnitTest.3.inc
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'));
}
}
14 changes: 14 additions & 0 deletions Magento/Tests/Exceptions/LocalizedThrowUnitTest.4.inc
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'));
}
}


13 changes: 13 additions & 0 deletions Magento/Tests/Exceptions/LocalizedThrowUnitTest.5.inc
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'));
}
}


38 changes: 38 additions & 0 deletions Magento/Tests/Exceptions/LocalizedThrowUnitTest.php
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 [];
}
}
4 changes: 4 additions & 0 deletions Magento/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@
<severity>7</severity>
<type>warning</type>
</rule>
<rule ref="Magento.Exceptions.LocalizedThrow">
<severity>7</severity>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the severity to 8. This rule looks like:

Magento specific code issue.

Copy link
Contributor

Choose a reason for hiding this comment

The 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>
Expand Down