-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Debug] Developer friendly Class Not Found and Undefined Function errors #8553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6671945
Developer friendly Class Not Found and Undefined Function errors.
simensen a0b1585
[Debug] fixed CS
fabpot 208ca5f
[Debug] made guessing of possible class names more flexible
fabpot 53ab284
[Debug] made Debug find FQCN automatically based on well-known autolo…
fabpot cefa1b5
[Debug] moved special fatal error handlers to their own classes
fabpot 968764b
[Debug] refactored unit tests
fabpot 80e19e2
[Debug] added some missing phpdocs
fabpot bde67f0
fixed an error message
fabpot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
2.4.0 | ||
----- | ||
|
||
* improved error messages for not found classes and functions | ||
|
||
2.3.0 | ||
----- | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Debug/Exception/ClassNotFoundException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Debug\Exception; | ||
|
||
/** | ||
* Class (or Trait or Interface) Not Found Exception. | ||
* | ||
* @author Konstanton Myakshin <[email protected]> | ||
*/ | ||
class ClassNotFoundException extends FatalErrorException | ||
{ | ||
public function __construct($message, \ErrorException $previous) | ||
{ | ||
parent::__construct( | ||
$message, | ||
$previous->getCode(), | ||
$previous->getSeverity(), | ||
$previous->getFile(), | ||
$previous->getLine(), | ||
$previous->getPrevious() | ||
); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Debug/Exception/UndefinedFunctionException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Debug\Exception; | ||
|
||
/** | ||
* Undefined Function Exception. | ||
* | ||
* @author Konstanton Myakshin <[email protected]> | ||
*/ | ||
class UndefinedFunctionException extends FatalErrorException | ||
{ | ||
public function __construct($message, \ErrorException $previous) | ||
{ | ||
parent::__construct( | ||
$message, | ||
$previous->getCode(), | ||
$previous->getSeverity(), | ||
$previous->getFile(), | ||
$previous->getLine(), | ||
$previous->getPrevious() | ||
); | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
src/Symfony/Component/Debug/FatalErrorHandler/ClassNotFoundFatalErrorHandler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Debug\FatalErrorHandler; | ||
|
||
use Symfony\Component\Debug\Exception\ClassNotFoundException; | ||
use Symfony\Component\Debug\Exception\FatalErrorException; | ||
use Composer\Autoload\ClassLoader as ComposerClassLoader; | ||
use Symfony\Component\ClassLoader as SymfonyClassLoader; | ||
use Symfony\Component\ClassLoader\DebugClassLoader; | ||
|
||
/** | ||
* ErrorHandler for classes that do not exist. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
class ClassNotFoundFatalErrorHandler implements FatalErrorHandlerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleError(array $error, FatalErrorException $exception) | ||
{ | ||
$messageLen = strlen($error['message']); | ||
$notFoundSuffix = '" not found'; | ||
$notFoundSuffixLen = strlen($notFoundSuffix); | ||
if ($notFoundSuffixLen > $messageLen) { | ||
return; | ||
} | ||
|
||
if (0 !== substr_compare($error['message'], $notFoundSuffix, -$notFoundSuffixLen)) { | ||
return; | ||
} | ||
|
||
foreach (array('class', 'interface', 'trait') as $typeName) { | ||
$prefix = ucfirst($typeName).' "'; | ||
$prefixLen = strlen($prefix); | ||
if (0 !== strpos($error['message'], $prefix)) { | ||
continue; | ||
} | ||
|
||
$fullyQualifiedClassName = substr($error['message'], $prefixLen, -$notFoundSuffixLen); | ||
if (false !== $namespaceSeparatorIndex = strrpos($fullyQualifiedClassName, '\\')) { | ||
$className = substr($fullyQualifiedClassName, $namespaceSeparatorIndex + 1); | ||
$namespacePrefix = substr($fullyQualifiedClassName, 0, $namespaceSeparatorIndex); | ||
$message = sprintf( | ||
'Attempted to load %s "%s" from namespace "%s" in %s line %d. Do you need to "use" it from another namespace?', | ||
$typeName, | ||
$className, | ||
$namespacePrefix, | ||
$error['file'], | ||
$error['line'] | ||
); | ||
} else { | ||
$className = $fullyQualifiedClassName; | ||
$message = sprintf( | ||
'Attempted to load %s "%s" from the global namespace in %s line %d. Did you forget a use statement for this %s?', | ||
$typeName, | ||
$className, | ||
$error['file'], | ||
$error['line'], | ||
$typeName | ||
); | ||
} | ||
|
||
if ($classes = $this->getClassCandidates($className)) { | ||
$message .= sprintf(' Perhaps you need to add a use statement for one of the following: %s.', implode(', ', $classes)); | ||
} | ||
|
||
return new ClassNotFoundException($message, $exception); | ||
} | ||
} | ||
|
||
/** | ||
* Tries to guess the full namespace for a given class name. | ||
* | ||
* By default, it looks for PSR-0 classes registered via a Symfony or a Composer | ||
* autoloader (that should cover all common cases). | ||
* | ||
* @param string $class A class name (without its namespace) | ||
* | ||
* @return array An array of possible fully qualified class names | ||
*/ | ||
private function getClassCandidates($class) | ||
{ | ||
if (!is_array($functions = spl_autoload_functions())) { | ||
return array(); | ||
} | ||
|
||
// find Symfony and Composer autoloaders | ||
$classes = array(); | ||
foreach ($functions as $function) { | ||
if (!is_array($function)) { | ||
continue; | ||
} | ||
|
||
// get class loaders wrapped by DebugClassLoader | ||
if ($function[0] instanceof DebugClassLoader && method_exists($function[0], 'getClassLoader')) { | ||
$function[0] = $function[0]->getClassLoader(); | ||
} | ||
|
||
if ($function[0] instanceof ComposerClassLoader || $function[0] instanceof SymfonyClassLoader) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if the autoloader is a cached one (ApcClassLoader & co) ? |
||
foreach ($function[0]->getPrefixes() as $paths) { | ||
foreach ($paths as $path) { | ||
$classes = array_merge($classes, $this->findClassInPath($function[0], $path, $class)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $classes; | ||
} | ||
|
||
private function findClassInPath($loader, $path, $class) | ||
{ | ||
if (!$path = realpath($path)) { | ||
continue; | ||
} | ||
|
||
$classes = array(); | ||
$filename = $class.'.php'; | ||
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) { | ||
if ($filename == $file->getFileName() && $class = $this->convertFileToClass($loader, $path, $file->getPathName())) { | ||
$classes[] = $class; | ||
} | ||
} | ||
|
||
return $classes; | ||
} | ||
|
||
private function convertFileToClass($loader, $path, $file) | ||
{ | ||
// We cannot use the autoloader here as most of them use require; but if the class | ||
// is not found, the new autoloader call will require the file again leading to a | ||
// "cannot redeclare class" error. | ||
require_once $file; | ||
|
||
$file = str_replace(array($path.'/', '.php'), array('', ''), $file); | ||
|
||
// is it a namespaced class? | ||
$class = str_replace('/', '\\', $file); | ||
if (class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false))) { | ||
return $class; | ||
} | ||
|
||
// is it a PEAR-like class name instead? | ||
$class = str_replace('/', '_', $file); | ||
if (class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false))) { | ||
return $class; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Debug/FatalErrorHandler/FatalErrorHandlerInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Debug\FatalErrorHandler; | ||
|
||
use Symfony\Component\Debug\Exception\FatalErrorException; | ||
|
||
/** | ||
* Attempts to convert fatal errors to exceptions. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
interface FatalErrorHandlerInterface | ||
{ | ||
/** | ||
* Attempts to convert an error into an exception. | ||
* | ||
* @param array $error An array as returned by error_get_last() | ||
* @param FatalErrorException $exception A FatalErrorException instance | ||
* | ||
* @return FatalErrorException|null A FatalErrorException instance if the class is able to convert the error, null otherwise | ||
*/ | ||
public function handleError(array $error, FatalErrorException $exception); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rename the class to
ClassNotFoundHandler
to be shorter and avoid repeating the namespace. What do you think ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way it is named now is how we do it elsewhere, so this is consistent.