|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Sniffs\Exceptions; |
| 8 | + |
| 9 | +use function array_slice; |
| 10 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 11 | +use PHP_CodeSniffer\Files\File; |
| 12 | + |
| 13 | +/** |
| 14 | + * Detects exceptions must not be handled in same function |
| 15 | + */ |
| 16 | +class ThrowCatchSniff implements Sniff |
| 17 | +{ |
| 18 | + /** |
| 19 | + * String representation of warning. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $warningMessage = 'Exceptions must not be handled in the same function where they are thrown.'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Warning violation code. |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $warningCode = 'ThrowCatch'; |
| 31 | + |
| 32 | + /** |
| 33 | + * @inheritDoc |
| 34 | + */ |
| 35 | + public function register() |
| 36 | + { |
| 37 | + return [T_FUNCTION, T_CLOSURE]; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritDoc |
| 42 | + */ |
| 43 | + public function process(File $phpcsFile, $stackPtr) |
| 44 | + { |
| 45 | + $tokens = $phpcsFile->getTokens(); |
| 46 | + if (!isset($tokens[$stackPtr]['scope_closer'])) { |
| 47 | + // Probably an interface method no check |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $closeBrace = $tokens[$stackPtr]['scope_closer']; |
| 52 | + $throwTags = []; |
| 53 | + $catchTags = []; |
| 54 | + |
| 55 | + for ($i = $stackPtr; $i < $closeBrace; $i++) { |
| 56 | + $token = $tokens[$i]; |
| 57 | + if ($token['code'] === T_CATCH) { |
| 58 | + $catchTags[] = $token; |
| 59 | + } |
| 60 | + if ($token['code'] === T_THROW) { |
| 61 | + $throwTags[] = $i; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (count($catchTags) === 0 || count($throwTags) === 0) { |
| 66 | + // No catch or throw found no check |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + $catchClassNames = []; |
| 71 | + $throwClassNames = []; |
| 72 | + |
| 73 | + // find all relevant classes in catch |
| 74 | + foreach ($catchTags as $catchTag) { |
| 75 | + $start = $catchTag['parenthesis_opener']; |
| 76 | + $end = $catchTag['parenthesis_closer']; |
| 77 | + |
| 78 | + $match = $phpcsFile->findNext(T_STRING, $start, $end); |
| 79 | + $catchClassNames[$match] = $tokens[$match]['content']; |
| 80 | + } |
| 81 | + |
| 82 | + // find all relevant classes in throws |
| 83 | + foreach ($throwTags as $throwTag) { |
| 84 | + $match = $phpcsFile->findNext(T_STRING, $throwTag); |
| 85 | + $throwClassNames[] = $tokens[$match]['content']; |
| 86 | + } |
| 87 | + |
| 88 | + $throwClassNames = array_flip($throwClassNames); |
| 89 | + foreach ($catchClassNames as $match => $catchClassName) { |
| 90 | + if (array_key_exists($catchClassName, $throwClassNames)) { |
| 91 | + $phpcsFile->addWarning($this->warningMessage, $match, $this->warningCode); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments