Mercurial > hg > isophonics-drupal-site
comparison vendor/psy/psysh/src/CodeCleaner/ValidClassNamePass.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | c2387f117808 |
children |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
41 private $conditionalScopes = 0; | 41 private $conditionalScopes = 0; |
42 private $atLeastPhp55; | 42 private $atLeastPhp55; |
43 | 43 |
44 public function __construct() | 44 public function __construct() |
45 { | 45 { |
46 $this->atLeastPhp55 = version_compare(PHP_VERSION, '5.5', '>='); | 46 $this->atLeastPhp55 = \version_compare(PHP_VERSION, '5.5', '>='); |
47 } | 47 } |
48 | 48 |
49 /** | 49 /** |
50 * Validate class, interface and trait definitions. | 50 * Validate class, interface and trait definitions. |
51 * | 51 * |
162 * @param ClassConstFetch $stmt | 162 * @param ClassConstFetch $stmt |
163 */ | 163 */ |
164 protected function validateClassConstFetchExpression(ClassConstFetch $stmt) | 164 protected function validateClassConstFetchExpression(ClassConstFetch $stmt) |
165 { | 165 { |
166 // there is no need to check exists for ::class const for php 5.5 or newer | 166 // there is no need to check exists for ::class const for php 5.5 or newer |
167 if (strtolower($stmt->name) === 'class' && $this->atLeastPhp55) { | 167 if (\strtolower($stmt->name) === 'class' && $this->atLeastPhp55) { |
168 return; | 168 return; |
169 } | 169 } |
170 | 170 |
171 // if class name is an expression, give it a pass for now | 171 // if class name is an expression, give it a pass for now |
172 if (!$stmt->class instanceof Expr) { | 172 if (!$stmt->class instanceof Expr) { |
208 } elseif ($this->traitExists($name)) { | 208 } elseif ($this->traitExists($name)) { |
209 $errorType = self::TRAIT_TYPE; | 209 $errorType = self::TRAIT_TYPE; |
210 } | 210 } |
211 | 211 |
212 if ($errorType !== null) { | 212 if ($errorType !== null) { |
213 throw $this->createError(sprintf('%s named %s already exists', ucfirst($errorType), $name), $stmt); | 213 throw $this->createError(\sprintf('%s named %s already exists', \ucfirst($errorType), $name), $stmt); |
214 } | 214 } |
215 | 215 |
216 // Store creation for the rest of this code snippet so we can find local | 216 // Store creation for the rest of this code snippet so we can find local |
217 // issue too | 217 // issue too |
218 $this->currentScope[strtolower($name)] = $scopeType; | 218 $this->currentScope[\strtolower($name)] = $scopeType; |
219 } | 219 } |
220 | 220 |
221 /** | 221 /** |
222 * Ensure that a referenced class exists. | 222 * Ensure that a referenced class exists. |
223 * | 223 * |
227 * @param Stmt $stmt | 227 * @param Stmt $stmt |
228 */ | 228 */ |
229 protected function ensureClassExists($name, $stmt) | 229 protected function ensureClassExists($name, $stmt) |
230 { | 230 { |
231 if (!$this->classExists($name)) { | 231 if (!$this->classExists($name)) { |
232 throw $this->createError(sprintf('Class \'%s\' not found', $name), $stmt); | 232 throw $this->createError(\sprintf('Class \'%s\' not found', $name), $stmt); |
233 } | 233 } |
234 } | 234 } |
235 | 235 |
236 /** | 236 /** |
237 * Ensure that a referenced class _or interface_ exists. | 237 * Ensure that a referenced class _or interface_ exists. |
242 * @param Stmt $stmt | 242 * @param Stmt $stmt |
243 */ | 243 */ |
244 protected function ensureClassOrInterfaceExists($name, $stmt) | 244 protected function ensureClassOrInterfaceExists($name, $stmt) |
245 { | 245 { |
246 if (!$this->classExists($name) && !$this->interfaceExists($name)) { | 246 if (!$this->classExists($name) && !$this->interfaceExists($name)) { |
247 throw $this->createError(sprintf('Class \'%s\' not found', $name), $stmt); | 247 throw $this->createError(\sprintf('Class \'%s\' not found', $name), $stmt); |
248 } | |
249 } | |
250 | |
251 /** | |
252 * Ensure that a referenced class _or trait_ exists. | |
253 * | |
254 * @throws FatalErrorException | |
255 * | |
256 * @param string $name | |
257 * @param Stmt $stmt | |
258 */ | |
259 protected function ensureClassOrTraitExists($name, $stmt) | |
260 { | |
261 if (!$this->classExists($name) && !$this->traitExists($name)) { | |
262 throw $this->createError(\sprintf('Class \'%s\' not found', $name), $stmt); | |
248 } | 263 } |
249 } | 264 } |
250 | 265 |
251 /** | 266 /** |
252 * Ensure that a statically called method exists. | 267 * Ensure that a statically called method exists. |
257 * @param string $name | 272 * @param string $name |
258 * @param Stmt $stmt | 273 * @param Stmt $stmt |
259 */ | 274 */ |
260 protected function ensureMethodExists($class, $name, $stmt) | 275 protected function ensureMethodExists($class, $name, $stmt) |
261 { | 276 { |
262 $this->ensureClassExists($class, $stmt); | 277 $this->ensureClassOrTraitExists($class, $stmt); |
263 | 278 |
264 // let's pretend all calls to self, parent and static are valid | 279 // let's pretend all calls to self, parent and static are valid |
265 if (in_array(strtolower($class), ['self', 'parent', 'static'])) { | 280 if (\in_array(\strtolower($class), ['self', 'parent', 'static'])) { |
266 return; | 281 return; |
267 } | 282 } |
268 | 283 |
269 // ... and all calls to classes defined right now | 284 // ... and all calls to classes defined right now |
270 if ($this->findInScope($class) === self::CLASS_TYPE) { | 285 if ($this->findInScope($class) === self::CLASS_TYPE) { |
274 // if method name is an expression, give it a pass for now | 289 // if method name is an expression, give it a pass for now |
275 if ($name instanceof Expr) { | 290 if ($name instanceof Expr) { |
276 return; | 291 return; |
277 } | 292 } |
278 | 293 |
279 if (!method_exists($class, $name) && !method_exists($class, '__callStatic')) { | 294 if (!\method_exists($class, $name) && !\method_exists($class, '__callStatic')) { |
280 throw $this->createError(sprintf('Call to undefined method %s::%s()', $class, $name), $stmt); | 295 throw $this->createError(\sprintf('Call to undefined method %s::%s()', $class, $name), $stmt); |
281 } | 296 } |
282 } | 297 } |
283 | 298 |
284 /** | 299 /** |
285 * Ensure that a referenced interface exists. | 300 * Ensure that a referenced interface exists. |
293 { | 308 { |
294 foreach ($interfaces as $interface) { | 309 foreach ($interfaces as $interface) { |
295 /** @var string $name */ | 310 /** @var string $name */ |
296 $name = $this->getFullyQualifiedName($interface); | 311 $name = $this->getFullyQualifiedName($interface); |
297 if (!$this->interfaceExists($name)) { | 312 if (!$this->interfaceExists($name)) { |
298 throw $this->createError(sprintf('Interface \'%s\' not found', $name), $stmt); | 313 throw $this->createError(\sprintf('Interface \'%s\' not found', $name), $stmt); |
299 } | 314 } |
300 } | 315 } |
301 } | 316 } |
302 | 317 |
303 /** | 318 /** |
333 protected function classExists($name) | 348 protected function classExists($name) |
334 { | 349 { |
335 // Give `self`, `static` and `parent` a pass. This will actually let | 350 // Give `self`, `static` and `parent` a pass. This will actually let |
336 // some errors through, since we're not checking whether the keyword is | 351 // some errors through, since we're not checking whether the keyword is |
337 // being used in a class scope. | 352 // being used in a class scope. |
338 if (in_array(strtolower($name), ['self', 'static', 'parent'])) { | 353 if (\in_array(\strtolower($name), ['self', 'static', 'parent'])) { |
339 return true; | 354 return true; |
340 } | 355 } |
341 | 356 |
342 return class_exists($name) || $this->findInScope($name) === self::CLASS_TYPE; | 357 return \class_exists($name) || $this->findInScope($name) === self::CLASS_TYPE; |
343 } | 358 } |
344 | 359 |
345 /** | 360 /** |
346 * Check whether an interface exists, or has been defined in the current code snippet. | 361 * Check whether an interface exists, or has been defined in the current code snippet. |
347 * | 362 * |
349 * | 364 * |
350 * @return bool | 365 * @return bool |
351 */ | 366 */ |
352 protected function interfaceExists($name) | 367 protected function interfaceExists($name) |
353 { | 368 { |
354 return interface_exists($name) || $this->findInScope($name) === self::INTERFACE_TYPE; | 369 return \interface_exists($name) || $this->findInScope($name) === self::INTERFACE_TYPE; |
355 } | 370 } |
356 | 371 |
357 /** | 372 /** |
358 * Check whether a trait exists, or has been defined in the current code snippet. | 373 * Check whether a trait exists, or has been defined in the current code snippet. |
359 * | 374 * |
361 * | 376 * |
362 * @return bool | 377 * @return bool |
363 */ | 378 */ |
364 protected function traitExists($name) | 379 protected function traitExists($name) |
365 { | 380 { |
366 return trait_exists($name) || $this->findInScope($name) === self::TRAIT_TYPE; | 381 return \trait_exists($name) || $this->findInScope($name) === self::TRAIT_TYPE; |
367 } | 382 } |
368 | 383 |
369 /** | 384 /** |
370 * Find a symbol in the current code snippet scope. | 385 * Find a symbol in the current code snippet scope. |
371 * | 386 * |
373 * | 388 * |
374 * @return string|null | 389 * @return string|null |
375 */ | 390 */ |
376 protected function findInScope($name) | 391 protected function findInScope($name) |
377 { | 392 { |
378 $name = strtolower($name); | 393 $name = \strtolower($name); |
379 if (isset($this->currentScope[$name])) { | 394 if (isset($this->currentScope[$name])) { |
380 return $this->currentScope[$name]; | 395 return $this->currentScope[$name]; |
381 } | 396 } |
382 } | 397 } |
383 | 398 |