1 | <?php |
||
24 | class FluentSetterSniff extends MethodScopeSniff |
||
25 | { |
||
26 | use SuppressingTrait; |
||
27 | |||
28 | /** |
||
29 | * Every setter function MUST return $this if nothing else is returned. |
||
30 | */ |
||
31 | public const CODE_MUST_RETURN_THIS = 'MustReturnThis'; |
||
32 | |||
33 | /** |
||
34 | * Your method MUST contain a return. |
||
35 | */ |
||
36 | public const CODE_NO_RETURN_FOUND = 'NoReturnFound'; |
||
37 | |||
38 | /** |
||
39 | * Error message when the method does not return $this. |
||
40 | */ |
||
41 | private const ERROR_MUST_RETURN_THIS = 'The method "%s" must return $this'; |
||
42 | |||
43 | /** |
||
44 | * Error message when no return statement is found. |
||
45 | */ |
||
46 | private const ERROR_NO_RETURN_FOUND = 'Method "%s" has no return statement'; |
||
47 | |||
48 | /** |
||
49 | * Specifies how an identation looks like. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | public $identation = ' '; |
||
54 | |||
55 | /** |
||
56 | * The used file decorated for the interface. |
||
57 | * |
||
58 | * @var FileDecorator |
||
59 | */ |
||
60 | private $file; |
||
61 | |||
62 | /** |
||
63 | * The position of this node. |
||
64 | * |
||
65 | * @var int |
||
66 | */ |
||
67 | private $stackPos; |
||
68 | |||
69 | /** |
||
70 | * Registers an error if an empty return (return null; or return;) is given. |
||
71 | 4 | * |
|
72 | * @param File $file The sniffed file. |
||
73 | 4 | * @param int $functionPos The position of the function. |
|
74 | 4 | * @param int $returnPos The position of the return call. |
|
75 | * @param string $methodIdent The ident for the method to given in an error. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | private function checkAndRegisterEmptyReturnErrors( |
||
100 | 4 | ||
101 | 4 | /** |
|
102 | 4 | * Checks if there are fluent setter errors and registers errors if needed. |
|
103 | * |
||
104 | 4 | * @param File $phpcsFile The file for this sniff. |
|
105 | * @param int $functionPos The position of the used token. |
||
106 | 4 | * @param int $classPos The position of the class. |
|
107 | 1 | * |
|
108 | 1 | * @return void |
|
109 | 1 | */ |
|
110 | 1 | private function checkForFluentSetterErrors(File $phpcsFile, int $functionPos, int $classPos): void |
|
138 | |||
139 | 3 | /** |
|
140 | 1 | * Returns the used file decorated for the interface. |
|
141 | 1 | * |
|
142 | 1 | * @return FileDecorator |
|
143 | 1 | */ |
|
144 | 1 | public function getFile(): FileDecorator |
|
148 | 1 | ||
149 | /** |
||
150 | * Returns the position of this node. |
||
151 | 1 | * @return int |
|
152 | */ |
||
153 | 3 | public function getStackPos(): int |
|
157 | |||
158 | /** |
||
159 | * Processes the tokens that this test is listening for. |
||
160 | * |
||
161 | * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint |
||
162 | * |
||
163 | * @param File $file The file where this token was found. |
||
164 | * @param int $functionPos The position in the stack where this token was found. |
||
165 | * @param int $classPos The position in the tokens array that opened the scope that this test is listening for. |
||
166 | 4 | * |
|
167 | * @return void |
||
168 | 4 | */ |
|
169 | protected function processTokenWithinScope( |
||
183 | 1 | ||
184 | 1 | /** |
|
185 | * Checks if the given method name relates to a setter function of a property. |
||
186 | 1 | * |
|
187 | * @param int $classPosition The position of the class token. |
||
188 | 1 | * @param File $file The file of the sniff. |
|
189 | 1 | * @param int $methodPosition The position of the method token. |
|
190 | 1 | * |
|
191 | 1 | * @return bool Indicator if the given method is a setter function |
|
192 | 1 | */ |
|
193 | 1 | private function checkIfSetterFunction(int $classPosition, File $file, int $methodPosition): bool |
|
211 | 1 | ||
212 | 1 | /** |
|
213 | 1 | * Fixes if no return statement is found. |
|
214 | 1 | * |
|
215 | * @param File $phpcsFile The php cs file |
||
216 | 1 | * @param int $closingBracePtr Pointer to the closing curly brace of the function |
|
217 | 1 | * |
|
218 | * @return void |
||
219 | */ |
||
220 | private function fixNoReturnFound(File $phpcsFile, int $closingBracePtr): void |
||
233 | |||
234 | /** |
||
235 | * Fixes the return value of a function to $this. |
||
236 | * |
||
237 | * @param File $phpcsFile The php cs file |
||
238 | * @param int $returnPtr Pointer to the return token |
||
239 | * |
||
240 | * @return void |
||
241 | */ |
||
242 | private function fixMustReturnThis(File $phpcsFile, int $returnPtr): void |
||
257 | } |
||
258 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: