-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Debug] Added UndefinedMethodFatalErrorHandler #9779
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
2.5.0 | ||
----- | ||
|
||
* added UndefinedMethodFatalErrorHandler | ||
|
||
2.4.0 | ||
----- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 Method Exception. | ||
* | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class UndefinedMethodException extends FatalErrorException | ||
{ | ||
public function __construct($message, \ErrorException $previous) | ||
{ | ||
parent::__construct($message, $previous->getCode(), $previous->getSeverity(), $previous->getFile(), $previous->getLine(), $previous->getPrevious()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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; | ||
use Symfony\Component\Debug\Exception\UndefinedMethodException; | ||
|
||
/** | ||
* ErrorHandler for undefined methods. | ||
* | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class UndefinedMethodFatalErrorHandler implements FatalErrorHandlerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleError(array $error, FatalErrorException $exception) | ||
{ | ||
preg_match('/^Call to undefined method (.*)::(.*)\(\)$/', $error['message'], $matches); | ||
if (!$matches) { | ||
return; | ||
} | ||
|
||
$className = $matches[1]; | ||
$methodName = $matches[2]; | ||
|
||
$message = sprintf('Attempted to call method "%s" on class "%s" in %s line %d.', $methodName, $className, $error['file'], $error['line']); | ||
|
||
$candidates = array(); | ||
foreach (get_class_methods($className) as $definedMethodName) { | ||
$lev = levenshtein($methodName, $definedMethodName); | ||
if ($lev <= strlen($methodName) / 3 || false !== strpos($definedMethodName, $methodName)) { | ||
$candidates[] = $definedMethodName; | ||
} | ||
} | ||
|
||
if ($candidates) { | ||
$message .= sprintf(' Did you mean to call: "%s"?', implode('", "', $candidates)); | ||
} | ||
|
||
return new UndefinedMethodException($message, $exception); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Debug\Tests\FatalErrorHandler; | ||
|
||
use Symfony\Component\Debug\Exception\FatalErrorException; | ||
use Symfony\Component\Debug\FatalErrorHandler\UndefinedMethodFatalErrorHandler; | ||
|
||
class UndefinedMethodFatalErrorHandlerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider provideUndefinedMethodData | ||
*/ | ||
public function testUndefinedMethod($error, $translatedMessage) | ||
{ | ||
$handler = new UndefinedMethodFatalErrorHandler(); | ||
$exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line'])); | ||
|
||
$this->assertInstanceof('Symfony\Component\Debug\Exception\UndefinedMethodException', $exception); | ||
$this->assertSame($translatedMessage, $exception->getMessage()); | ||
$this->assertSame($error['type'], $exception->getSeverity()); | ||
$this->assertSame($error['file'], $exception->getFile()); | ||
$this->assertSame($error['line'], $exception->getLine()); | ||
} | ||
|
||
public function provideUndefinedMethodData() | ||
{ | ||
return array( | ||
array( | ||
array( | ||
'type' => 1, | ||
'line' => 12, | ||
'file' => 'foo.php', | ||
'message' => 'Call to undefined method SplObjectStorage::what()', | ||
), | ||
'Attempted to call method "what" on class "SplObjectStorage" in foo.php line 12.', | ||
), | ||
array( | ||
array( | ||
'type' => 1, | ||
'line' => 12, | ||
'file' => 'foo.php', | ||
'message' => 'Call to undefined method SplObjectStorage::walid()', | ||
), | ||
'Attempted to call method "walid" on class "SplObjectStorage" in foo.php line 12. Did you mean to call: "valid"?', | ||
), | ||
array( | ||
array( | ||
'type' => 1, | ||
'line' => 12, | ||
'file' => 'foo.php', | ||
'message' => 'Call to undefined method SplObjectStorage::offsetFet()', | ||
), | ||
'Attempted to call method "offsetFet" on class "SplObjectStorage" in foo.php line 12. Did you mean to call: "offsetSet", "offsetUnset", "offsetGet"?', | ||
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. it seems a clean PR however i was wondering two things:
Finally, even though this does not need maybe to be documented, and i don't know if we have documentation for the debug component updated, i wonder how the approach to document this would look like. It was nice meeting you man 👶 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.
(nice to meet you too ;)) |
||
), | ||
); | ||
} | ||
} |
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.
Shouldn't you do the strpos check first and call levenshtein only in case strpos didnt succeeded?