1 | <?php |
||
57 | class Antibot implements LoggerAwareInterface |
||
58 | { |
||
59 | /** |
||
60 | * Session persistent, unique token |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $unique; |
||
65 | /** |
||
66 | * Antibot prefix |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $prefix; |
||
71 | /** |
||
72 | * Unique signature |
||
73 | * |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $signature; |
||
77 | /** |
||
78 | * Parameter prefix |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $parameterPrefix; |
||
83 | /** |
||
84 | * GET & POST data |
||
85 | * |
||
86 | * @var null|string[] |
||
87 | */ |
||
88 | protected $data = null; |
||
89 | /** |
||
90 | * Validators |
||
91 | * |
||
92 | * @var ValidatorInterface[] |
||
93 | */ |
||
94 | protected $validators = []; |
||
95 | /** |
||
96 | * Immutable instance |
||
97 | * |
||
98 | * @var bool |
||
99 | */ |
||
100 | protected $immutable = false; |
||
101 | /** |
||
102 | * Logger |
||
103 | * |
||
104 | * @var LoggerInterface |
||
105 | */ |
||
106 | protected $logger = null; |
||
107 | /** |
||
108 | * Default antibot prefix |
||
109 | * |
||
110 | * @var string |
||
111 | */ |
||
112 | const DEFAULT_PREFIX = 'antibot'; |
||
113 | |||
114 | /** |
||
115 | * Antibot constructor |
||
116 | * |
||
117 | * @param string $unique Session-persistent, unique key |
||
118 | * @param string $prefix Prefix |
||
119 | */ |
||
120 | 10 | public function __construct(string $unique, string $prefix = self::DEFAULT_PREFIX) |
|
126 | |||
127 | /** |
||
128 | * Return the session persistent, unique token |
||
129 | * |
||
130 | * @return string Session persistent, unique token |
||
131 | */ |
||
132 | 4 | public function getUnique(): string |
|
136 | |||
137 | /** |
||
138 | * Return the prefix |
||
139 | * |
||
140 | * @return string Prefix |
||
141 | */ |
||
142 | 1 | public function getPrefix(): string |
|
146 | |||
147 | /** |
||
148 | * Return the submitted Antibot data |
||
149 | * |
||
150 | * @return string[] Antibot data |
||
151 | */ |
||
152 | 4 | public function getData(): ?array |
|
158 | |||
159 | /** |
||
160 | * Return the parameter prefix |
||
161 | * |
||
162 | * @return string Parameter prefix |
||
163 | * @throws RuntimeException If Antibot needs to be initialized |
||
164 | */ |
||
165 | 4 | public function getParameterPrefix(): string |
|
171 | |||
172 | /** |
||
173 | * Validate a request |
||
174 | * |
||
175 | * @param ServerRequestInterface $request Request |
||
176 | * |
||
177 | * @return ValidationResult Validation result |
||
178 | */ |
||
179 | 4 | public function validate(ServerRequestInterface $request): ValidationResult |
|
217 | |||
218 | /** |
||
219 | * Return the Antibot armor |
||
220 | * |
||
221 | * @param ServerRequestInterface $request Request |
||
222 | * @param bool $raw Return input elements |
||
223 | * |
||
224 | * @return string|array Antibot armor |
||
225 | */ |
||
226 | 4 | public function armor(ServerRequestInterface $request, bool $raw = false) |
|
242 | |||
243 | /** |
||
244 | * Compare and sort validators |
||
245 | * |
||
246 | * @param ValidatorInterface $validator1 Validator 1 |
||
247 | * @param ValidatorInterface $validator2 Validator 2 |
||
248 | * |
||
249 | * @return int Sorting |
||
250 | */ |
||
251 | protected function sortValidators(ValidatorInterface $validator1, ValidatorInterface $validator2): int |
||
261 | |||
262 | /** |
||
263 | * Pre-validation initialization |
||
264 | * |
||
265 | * @param ServerRequestInterface $request Request |
||
266 | */ |
||
267 | 5 | protected function initialize(ServerRequestInterface $request): void |
|
275 | |||
276 | /** |
||
277 | * Calculate the unique signature |
||
278 | * |
||
279 | * @return string Signature |
||
280 | */ |
||
281 | 5 | protected function calculateSignature(): string |
|
287 | |||
288 | /** |
||
289 | * Extract the antibot data from GET and POST parameters |
||
290 | * |
||
291 | * @param ServerRequestInterface $request Request |
||
292 | */ |
||
293 | 5 | protected function extractData(ServerRequestInterface $request): void |
|
301 | |||
302 | /** |
||
303 | * Check whether this Antibot instance is immutable |
||
304 | * |
||
305 | * @throws RuntimeException If the Antibot instance is immutable |
||
306 | */ |
||
307 | 5 | protected function checkImmutable(): void |
|
316 | |||
317 | /** |
||
318 | * Check whether this Antibot instance is already initialized |
||
319 | * |
||
320 | * @throws RuntimeException If the Antibot instance still needs to be initialized |
||
321 | */ |
||
322 | 4 | protected function checkInitialized(): void |
|
332 | |||
333 | /** |
||
334 | * Return the logger |
||
335 | * |
||
336 | * @return LoggerInterface Logger |
||
337 | */ |
||
338 | 4 | public function getLogger(): LoggerInterface |
|
342 | |||
343 | /** |
||
344 | * Sets a logger instance on the object |
||
345 | * |
||
346 | * @param LoggerInterface $logger Logger |
||
347 | * |
||
348 | * @return void |
||
349 | */ |
||
350 | 10 | public function setLogger(LoggerInterface $logger): void |
|
354 | } |
||
355 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.