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