Skip to content

Commit a5a1186

Browse files
committed
minor symfony#57758 [Console] Remove a redundant check in ErrorListener (seriquynh)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [Console] Remove a redundant check in `ErrorListener` | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | N/A | License | MIT If a class defines `__toString()` method, an object created from it will also be an instance of `\Stringable` interface. This case, the `$input` object is an implementation of `Symfony\Component\Console\Input\InputInterface` that has `__toString()` method. So, `$input instanceof \Stringable` check is always true. You may try this code sample for more details. ```php <?php require __DIR__.'/vendor/autoload.php'; var_dump(new \Symfony\Component\Console\Input\ArgvInput() instanceof \Stringable); // bool(true) var_dump(new \Symfony\Component\Console\Input\ArrayInput([]) instanceof \Stringable); // bool(true) var_dump(new \Symfony\Component\Console\Input\StringInput('') instanceof \Stringable); // bool(true) readonly class Person { public function __construct(private string $name) {} public function __toString(): string { return "Hello, I am {$this->name}."; } } $person = new Person("John Doe"); var_dump($person instanceof \Stringable); // bool(true) ``` Commits ------- bcb3e00 [Console] Remove a redundant check in `ErrorListener`
2 parents e27db25 + bcb3e00 commit a5a1186

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/Symfony/Component/Console/EventListener/ErrorListener.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,15 @@ public static function getSubscribedEvents(): array
7575
];
7676
}
7777

78-
private static function getInputString(ConsoleEvent $event): ?string
78+
private static function getInputString(ConsoleEvent $event): string
7979
{
8080
$commandName = $event->getCommand()?->getName();
81-
$input = $event->getInput();
81+
$inputString = (string) $event->getInput();
8282

83-
if ($input instanceof \Stringable) {
84-
if ($commandName) {
85-
return str_replace(["'$commandName'", "\"$commandName\""], $commandName, (string) $input);
86-
}
87-
88-
return (string) $input;
83+
if ($commandName) {
84+
return str_replace(["'$commandName'", "\"$commandName\""], $commandName, $inputString);
8985
}
9086

91-
return $commandName;
87+
return $inputString;
9288
}
9389
}

0 commit comments

Comments
 (0)