Skip to content

Commit 27fab22

Browse files
bug #40019 [ErrorHandler] Fix strpos error when trying to call a method without a name (Deuchnord)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [ErrorHandler] Fix strpos error when trying to call a method without a name | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | None (direct fix) | License | MIT | Doc PR | None <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too.) - Features and deprecations must be submitted against branch 5.x. --> When running the following code: ```php class Foo { // Some code here } $str = ''; // this should not happen, but for some reason, it did. $foo->{$str}(); ``` a fatal error occurs because the method name to execute is empty, but Symfony's error enhancer fails to parse it: ![Error screenshot](https://user-images.githubusercontent.com/7600265/106108704-ec019b80-6148-11eb-82bc-f7801e30fea4.png) In this PR, I propose a fix with a more clear error to inform the developer about what happened. Commits ------- 66be87b [ErrorHandler] Fix strpos error when trying to call a method without a name
2 parents 8533ea2 + 66be87b commit 27fab22

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

src/Symfony/Component/Debug/FatalErrorHandler/UndefinedMethodFatalErrorHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function handleError(array $error, FatalErrorException $exception)
4040

4141
$message = sprintf('Attempted to call an undefined method named "%s" of class "%s".', $methodName, $className);
4242

43-
if (!class_exists($className) || null === $methods = get_class_methods($className)) {
43+
if ('' === $methodName || !class_exists($className) || null === $methods = get_class_methods($className)) {
4444
// failed to get the class or its methods on which an unknown method was called (for example on an anonymous class)
4545
return new UndefinedMethodException($message, $exception);
4646
}

src/Symfony/Component/Debug/Tests/FatalErrorHandler/UndefinedMethodFatalErrorHandlerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public function provideUndefinedMethodData()
4848
],
4949
'Attempted to call an undefined method named "what" of class "SplObjectStorage".',
5050
],
51+
[
52+
[
53+
'type' => 1,
54+
'line' => 12,
55+
'file' => 'foo.php',
56+
'message' => 'Call to undefined method SplObjectStorage::()',
57+
],
58+
'Attempted to call an undefined method named "" of class "SplObjectStorage".',
59+
],
5160
[
5261
[
5362
'type' => 1,

src/Symfony/Component/ErrorHandler/ErrorEnhancer/UndefinedMethodErrorEnhancer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function enhance(\Throwable $error): ?\Throwable
3939

4040
$message = sprintf('Attempted to call an undefined method named "%s" of class "%s".', $methodName, $className);
4141

42-
if (!class_exists($className) || null === $methods = get_class_methods($className)) {
42+
if ('' === $methodName || !class_exists($className) || null === $methods = get_class_methods($className)) {
4343
// failed to get the class or its methods on which an unknown method was called (for example on an anonymous class)
4444
return new UndefinedMethodError($message, $error);
4545
}

src/Symfony/Component/ErrorHandler/Tests/ErrorEnhancer/UndefinedMethodErrorEnhancerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function provideUndefinedMethodData()
4040
'Call to undefined method SplObjectStorage::what()',
4141
'Attempted to call an undefined method named "what" of class "SplObjectStorage".',
4242
],
43+
[
44+
'Call to undefined method SplObjectStorage::()',
45+
'Attempted to call an undefined method named "" of class "SplObjectStorage".',
46+
],
4347
[
4448
'Call to undefined method SplObjectStorage::walid()',
4549
"Attempted to call an undefined method named \"walid\" of class \"SplObjectStorage\".\nDid you mean to call \"valid\"?",

0 commit comments

Comments
 (0)