Skip to content

Commit 74d13e3

Browse files
committed
[Debug] Added UndefinedMethodFatalErrorHandler
1 parent 6764f91 commit 74d13e3

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

src/Symfony/Component/Debug/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
2.5.0
5+
-----
6+
7+
* added UndefinedMethodFatalErrorHandler
8+
49
2.4.0
510
-----
611

src/Symfony/Component/Debug/ErrorHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Debug\Exception\FatalErrorException;
1717
use Symfony\Component\Debug\Exception\DummyException;
1818
use Symfony\Component\Debug\FatalErrorHandler\UndefinedFunctionFatalErrorHandler;
19+
use Symfony\Component\Debug\FatalErrorHandler\UndefinedMethodFatalErrorHandler;
1920
use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
2021
use Symfony\Component\Debug\FatalErrorHandler\FatalErrorHandlerInterface;
2122

@@ -224,6 +225,7 @@ protected function getFatalErrorHandlers()
224225
{
225226
return array(
226227
new UndefinedFunctionFatalErrorHandler(),
228+
new UndefinedMethodFatalErrorHandler(),
227229
new ClassNotFoundFatalErrorHandler(),
228230
);
229231
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Debug\Exception;
13+
14+
/**
15+
* Undefined Method Exception.
16+
*
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
class UndefinedMethodException extends FatalErrorException
20+
{
21+
public function __construct($message, \ErrorException $previous)
22+
{
23+
parent::__construct($message, $previous->getCode(), $previous->getSeverity(), $previous->getFile(), $previous->getLine(), $previous->getPrevious());
24+
}
25+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Debug\FatalErrorHandler;
13+
14+
use Symfony\Component\Debug\Exception\FatalErrorException;
15+
use Symfony\Component\Debug\Exception\UndefinedMethodException;
16+
17+
/**
18+
* ErrorHandler for undefined methods.
19+
*
20+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
21+
*/
22+
class UndefinedMethodFatalErrorHandler implements FatalErrorHandlerInterface
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function handleError(array $error, FatalErrorException $exception)
28+
{
29+
preg_match('/^Call to undefined method (.*)::(.*)\(\)$/', $error['message'], $matches);
30+
if (!$matches) {
31+
return;
32+
}
33+
34+
$className = $matches[1];
35+
$methodName = $matches[2];
36+
37+
$message = sprintf('Attempted to call method "%s" on class "%s" in %s line %d.', $methodName, $className, $error['file'], $error['line']);
38+
39+
$candidates = array();
40+
foreach (get_class_methods($className) as $definedMethodName) {
41+
$lev = levenshtein($methodName, $definedMethodName);
42+
if ($lev <= strlen($methodName) / 3 || false !== strpos($definedMethodName, $methodName)) {
43+
$candidates[] = $definedMethodName;
44+
}
45+
}
46+
47+
if ($candidates) {
48+
$message .= sprintf(' Did you mean to call: "%s"?', implode('", "', $candidates));
49+
}
50+
51+
return new UndefinedMethodException($message, $exception);
52+
}
53+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Debug\Tests\FatalErrorHandler;
13+
14+
use Symfony\Component\Debug\Exception\FatalErrorException;
15+
use Symfony\Component\Debug\FatalErrorHandler\UndefinedMethodFatalErrorHandler;
16+
17+
class UndefinedMethodFatalErrorHandlerTest extends \PHPUnit_Framework_TestCase
18+
{
19+
/**
20+
* @dataProvider provideUndefinedMethodData
21+
*/
22+
public function testUndefinedMethod($error, $translatedMessage)
23+
{
24+
$handler = new UndefinedMethodFatalErrorHandler();
25+
$exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
26+
27+
$this->assertInstanceof('Symfony\Component\Debug\Exception\UndefinedMethodException', $exception);
28+
$this->assertSame($translatedMessage, $exception->getMessage());
29+
$this->assertSame($error['type'], $exception->getSeverity());
30+
$this->assertSame($error['file'], $exception->getFile());
31+
$this->assertSame($error['line'], $exception->getLine());
32+
}
33+
34+
public function provideUndefinedMethodData()
35+
{
36+
return array(
37+
array(
38+
array(
39+
'type' => 1,
40+
'line' => 12,
41+
'file' => 'foo.php',
42+
'message' => 'Call to undefined method SplObjectStorage::what()',
43+
),
44+
'Attempted to call method "what" on class "SplObjectStorage" in foo.php line 12.',
45+
),
46+
array(
47+
array(
48+
'type' => 1,
49+
'line' => 12,
50+
'file' => 'foo.php',
51+
'message' => 'Call to undefined method SplObjectStorage::walid()',
52+
),
53+
'Attempted to call method "walid" on class "SplObjectStorage" in foo.php line 12. Did you mean to call: "valid"?',
54+
),
55+
array(
56+
array(
57+
'type' => 1,
58+
'line' => 12,
59+
'file' => 'foo.php',
60+
'message' => 'Call to undefined method SplObjectStorage::offsetFet()',
61+
),
62+
'Attempted to call method "offsetFet" on class "SplObjectStorage" in foo.php line 12. Did you mean to call: "offsetSet", "offsetUnset", "offsetGet"?',
63+
),
64+
);
65+
}
66+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy