|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BestIt\Sniffs\Functions; |
|
6
|
|
|
|
|
7
|
|
|
use PHP_CodeSniffer\Files\File; |
|
8
|
|
|
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class MultipleReturnSniff. |
|
12
|
|
|
* |
|
13
|
|
|
* @author Mika Bertels <[email protected]> |
|
14
|
|
|
* @package BestIt\Sniffs\Functions |
|
15
|
|
|
*/ |
|
16
|
|
|
class MultipleReturnSniff extends AbstractScopeSniff |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Code for multiple returns. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string CODE_MULTIPLE_RETURNS_FOUND |
|
22
|
|
|
*/ |
|
23
|
|
|
public const CODE_MULTIPLE_RETURNS_FOUND = 'MultipleReturnsFound'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Error message for multiple returns. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string WARNING_MULTIPLE_RETURNS_FOUND |
|
29
|
|
|
*/ |
|
30
|
|
|
private const WARNING_MULTIPLE_RETURNS_FOUND = 'Multiple returns detected. Did you refactor your class?'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* MultipleReturnSniff constructor. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
parent::__construct([T_FUNCTION], [T_RETURN], false); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Processes the tokens that this test is listening for. |
|
42
|
|
|
* |
|
43
|
|
|
* @param File $phpcsFile |
|
44
|
|
|
* @param int $returnPos |
|
45
|
|
|
* @param int $functionPos |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function processTokenWithinScope(File $phpcsFile, $returnPos, $functionPos): void |
|
50
|
|
|
{ |
|
51
|
|
|
$multipleReturnsFound = $phpcsFile->findPrevious([T_RETURN], $returnPos - 1, $functionPos); |
|
52
|
|
|
|
|
53
|
|
|
if ($multipleReturnsFound && $multipleReturnsFound > -1) { |
|
54
|
|
|
$phpcsFile->addWarning( |
|
55
|
|
|
self::WARNING_MULTIPLE_RETURNS_FOUND, |
|
56
|
|
|
$returnPos, |
|
57
|
|
|
self::CODE_MULTIPLE_RETURNS_FOUND, |
|
58
|
|
|
$phpcsFile->getWarnings() |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Processes a token that is found outside the scope that this test is listening to. |
|
65
|
|
|
* |
|
66
|
|
|
* @param File $phpcsFile The file where this token was found. |
|
67
|
|
|
* @param int $stackPtr The position in the stack where this token was found. |
|
68
|
|
|
* |
|
69
|
|
|
* @return void|int Optionally returns a stack pointer. The sniff will not be |
|
70
|
|
|
* called again on the current file until the returned stack |
|
71
|
|
|
* pointer is reached. Return (count($tokens) + 1) to skip |
|
72
|
|
|
* the rest of the file. |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function processTokenOutsideScope(File $phpcsFile, $stackPtr) |
|
75
|
|
|
{ |
|
76
|
|
|
// Satisfy PHP MD and do nothing. |
|
77
|
|
|
unset($phpcsFile, $stackPtr); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|