1 | <?php |
||
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 |
||
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 |
||
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 |
||
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: