Skip to content

MC-18816: Impelement an better check for ThrowCatchSniff #120

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

Merged
merged 4 commits into from
Aug 1, 2019
Merged
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
92 changes: 59 additions & 33 deletions Magento2/Sniffs/Exceptions/ThrowCatchSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\Exceptions;

use function array_slice;
Expand Down Expand Up @@ -33,7 +34,7 @@ class ThrowCatchSniff implements Sniff
*/
public function register()
{
return [T_FUNCTION, T_CLOSURE];
return [T_TRY];
}

/**
Expand All @@ -42,53 +43,78 @@ public function register()
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (!isset($tokens[$stackPtr]['scope_closer'])) {
// Probably an interface method no check
return;
}
$endOfStatement = $phpcsFile->findEndOfStatement($stackPtr);

$closeBrace = $tokens[$stackPtr]['scope_closer'];
$throwTags = [];
$catchTags = [];
$throwClassNames = [];
$searchForNextThrow = $stackPtr;

for ($i = $stackPtr; $i < $closeBrace; $i++) {
$token = $tokens[$i];
if ($token['code'] === T_CATCH) {
$catchTags[] = $token;
}
if ($token['code'] === T_THROW) {
$throwTags[] = $i;
// search for all throws
do {
$throwTag = $phpcsFile->findNext(T_THROW, $searchForNextThrow, $endOfStatement);

if ($throwTag === false) {
break;
}
}

if (count($catchTags) === 0 || count($throwTags) === 0) {
// No catch or throw found no check
return;
$throwClassNames[] = $this->getFullClassName($tokens, $throwTag + 1);

$searchForNextThrow = $throwTag + 1;
} while ($throwTag !== false);

if (empty($throwClassNames)) {
return; // is not relevant not throw in try found.
}

$catchClassNames = [];
$throwClassNames = [];

// find all relevant classes in catch
foreach ($catchTags as $catchTag) {
$start = $catchTag['parenthesis_opener'];
$end = $catchTag['parenthesis_closer'];

$match = $phpcsFile->findNext(T_STRING, $start, $end);
$catchClassNames[$match] = $tokens[$match]['content'];
}
// TRY statements need to check until the end of all CATCH statements.
do {
$nextToken = $phpcsFile->findNext(T_WHITESPACE, ($endOfStatement + 1), null, true);
if ($tokens[$nextToken]['code'] === T_CATCH) {
$endOfStatement = $tokens[$nextToken]['scope_closer'];
$catchClassNames[$nextToken] = $this->getFullClassName($tokens, $nextToken + 1);
} else {
break;
}
} while (isset($tokens[$nextToken]['scope_closer']) === true);

// find all relevant classes in throws
foreach ($throwTags as $throwTag) {
$match = $phpcsFile->findNext(T_STRING, $throwTag);
$throwClassNames[] = $tokens[$match]['content'];
if (empty($catchClassNames)) {
return; // is not relevant no catch found
}

$throwClassNames = array_flip($throwClassNames);
foreach ($catchClassNames as $match => $catchClassName) {
if (array_key_exists($catchClassName, $throwClassNames)) {
if (isset($throwClassNames[$catchClassName])) {
$phpcsFile->addWarning($this->warningMessage, $match, $this->warningCode);
}
}
}

/**
* Get the full class name with namespace.
*
* @param array $tokens
* @param int $startPos
* @return string
*/
private function getFullClassName(array $tokens, $startPos)
{
$fullName = "";
$endOfClassName = [T_SEMICOLON => 0, T_CLOSE_PARENTHESIS => 0];

$tokenCount = count($tokens);
for ($i = $startPos; $i <= $tokenCount; $i++) {
$type = $tokens[$i]['code'];

if ($type === T_STRING || $type === T_NS_SEPARATOR) {
$fullName .= $tokens[$i]['content'];
}

if (array_key_exists($type, $endOfClassName)) {
break; // line end each
}
}

return $fullName;
}
}
18 changes: 18 additions & 0 deletions Magento2/Tests/Exceptions/ThrowCatchUnitTest.1.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

// This file contains all false positive cases that were found with previous implementation
// Check Exceptions MUST NOT handled in same function should fin nothing there

/**
* @throws \Magento\Framework\Exception\LocalizedException
*/
function creatDir()
{
try {
// call internal code that throw an Exception
throw new \Magento\Framework\Exception\LocalizedException('Nice user friendly message');
}
catch (\Magento\Framework\Exception\FileSystemException $e) {
throw new \Magento\Framework\Exception\LocalizedException('Nice user friendly message');
}
}
8 changes: 7 additions & 1 deletion Magento2/Tests/Exceptions/ThrowCatchUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Tests\Exceptions;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
Expand All @@ -23,8 +24,13 @@ protected function getErrorList()
/**
* @inheritdoc
*/
protected function getWarningList()
protected function getWarningList($testFile = '')
{

if ($testFile === 'ThrowCatchUnitTest.1.inc') {
return [];
}

return [
41 => 1,
120 => 1,
Expand Down