annotate vendor/symfony/debug/FatalErrorHandler/UndefinedMethodFatalErrorHandler.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Debug\FatalErrorHandler;
Chris@0 13
Chris@0 14 use Symfony\Component\Debug\Exception\FatalErrorException;
Chris@0 15 use Symfony\Component\Debug\Exception\UndefinedMethodException;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * ErrorHandler for undefined methods.
Chris@0 19 *
Chris@0 20 * @author Grégoire Pineau <lyrixx@lyrixx.info>
Chris@0 21 */
Chris@0 22 class UndefinedMethodFatalErrorHandler implements FatalErrorHandlerInterface
Chris@0 23 {
Chris@0 24 /**
Chris@0 25 * {@inheritdoc}
Chris@0 26 */
Chris@0 27 public function handleError(array $error, FatalErrorException $exception)
Chris@0 28 {
Chris@0 29 preg_match('/^Call to undefined method (.*)::(.*)\(\)$/', $error['message'], $matches);
Chris@0 30 if (!$matches) {
Chris@0 31 return;
Chris@0 32 }
Chris@0 33
Chris@0 34 $className = $matches[1];
Chris@0 35 $methodName = $matches[2];
Chris@0 36
Chris@0 37 $message = sprintf('Attempted to call an undefined method named "%s" of class "%s".', $methodName, $className);
Chris@0 38
Chris@0 39 if (!class_exists($className) || null === $methods = get_class_methods($className)) {
Chris@0 40 // failed to get the class or its methods on which an unknown method was called (for example on an anonymous class)
Chris@0 41 return new UndefinedMethodException($message, $exception);
Chris@0 42 }
Chris@0 43
Chris@17 44 $candidates = [];
Chris@0 45 foreach ($methods as $definedMethodName) {
Chris@0 46 $lev = levenshtein($methodName, $definedMethodName);
Chris@17 47 if ($lev <= \strlen($methodName) / 3 || false !== strpos($definedMethodName, $methodName)) {
Chris@0 48 $candidates[] = $definedMethodName;
Chris@0 49 }
Chris@0 50 }
Chris@0 51
Chris@0 52 if ($candidates) {
Chris@0 53 sort($candidates);
Chris@0 54 $last = array_pop($candidates).'"?';
Chris@0 55 if ($candidates) {
Chris@0 56 $candidates = 'e.g. "'.implode('", "', $candidates).'" or "'.$last;
Chris@0 57 } else {
Chris@0 58 $candidates = '"'.$last;
Chris@0 59 }
Chris@0 60
Chris@0 61 $message .= "\nDid you mean to call ".$candidates;
Chris@0 62 }
Chris@0 63
Chris@0 64 return new UndefinedMethodException($message, $exception);
Chris@0 65 }
Chris@0 66 }