|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BestIt\Sniffs\Functions; |
|
6
|
|
|
|
|
7
|
|
|
use BestIt\CodeSniffer\Helper\TokenHelper; |
|
8
|
|
|
use BestIt\Sniffs\AbstractSniff; |
|
9
|
|
|
use BestIt\Sniffs\FunctionRegistrationTrait; |
|
10
|
|
|
use function array_filter; |
|
11
|
|
|
use function array_shift; |
|
12
|
|
|
use function array_walk; |
|
13
|
|
|
use const T_CLOSURE; |
|
14
|
|
|
use const T_FUNCTION; |
|
15
|
|
|
use const T_RETURN; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class MultipleReturnSniff. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Mika Bertels <[email protected]> |
|
21
|
|
|
* @package BestIt\Sniffs\Functions |
|
22
|
|
|
*/ |
|
23
|
|
|
class MultipleReturnSniff extends AbstractSniff |
|
24
|
|
|
{ |
|
25
|
|
|
use FunctionRegistrationTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* You SHOULD only use a return per method. |
|
29
|
|
|
*/ |
|
30
|
|
|
public const CODE_MULTIPLE_RETURNS_FOUND = 'MultipleReturnsFound'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Error message for multiple returns. |
|
34
|
|
|
*/ |
|
35
|
|
|
private const WARNING_MULTIPLE_RETURNS_FOUND = 'Multiple returns detected. Did you refactor your method? Please ' . |
|
36
|
|
|
'do not use an early return if your method/function still is cluttered.'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Only work on full fledged functions. |
|
40
|
|
|
* |
|
41
|
|
|
* @return bool True if there is a scope closer for this token. |
|
42
|
|
|
*/ |
|
43
|
|
|
protected function areRequirementsMet(): bool |
|
44
|
|
|
{ |
|
45
|
|
|
return (bool) @ $this->token['scope_closer']; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns the returns of this function. |
|
50
|
|
|
* |
|
51
|
|
|
* We check the "token level" to exclude the returns of nested closures. |
|
52
|
|
|
* |
|
53
|
|
|
* @return int[] The positions of the returns from the same function-scope. |
|
54
|
|
|
*/ |
|
55
|
|
|
private function loadReturnsOfThisFunction(): array |
|
56
|
|
|
{ |
|
57
|
|
|
$returnPositions = TokenHelper::findNextAll( |
|
58
|
|
|
$this->file, |
|
|
|
|
|
|
59
|
|
|
[T_RETURN], |
|
60
|
|
|
$this->stackPos + 1, |
|
61
|
|
|
$this->token['scope_closer'] |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
return array_filter($returnPositions, function (int $returnPos): bool { |
|
65
|
|
|
$possibleClosure = $this->file->findPrevious([T_CLOSURE, T_FUNCTION], $returnPos - 1, $this->stackPos); |
|
66
|
|
|
|
|
67
|
|
|
return $possibleClosure === $this->stackPos; |
|
68
|
|
|
}); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Iterates through the returns of this function and registers warnings if there is more then one relevant return. |
|
73
|
|
|
* |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function processToken(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$returnPositions = $this->loadReturnsOfThisFunction(); |
|
79
|
|
|
|
|
80
|
|
|
if (count($returnPositions) > 1) { |
|
81
|
|
|
array_shift($returnPositions); |
|
82
|
|
|
|
|
83
|
|
|
array_walk($returnPositions, function (int $returnPos): void { |
|
84
|
|
|
$this->file->addWarning( |
|
85
|
|
|
self::WARNING_MULTIPLE_RETURNS_FOUND, |
|
86
|
|
|
$returnPos, |
|
87
|
|
|
static::CODE_MULTIPLE_RETURNS_FOUND |
|
88
|
|
|
); |
|
89
|
|
|
}); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: