Skip to content

Commit 28983de

Browse files
authored
[console] fix false positive - console argument value when using ternary (#267)
1 parent 0a7b057 commit 28983de

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Handler/ConsoleHandler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ private static function normalizeParams(array $params, array $args): array
278278
/**
279279
* @param mixed $mode
280280
*/
281-
private static function getModeValue($mode): int
281+
private static function getModeValue($mode): ?int
282282
{
283283
if ($mode instanceof Expr\BinaryOp\BitwiseOr) {
284284
return self::getModeValue($mode->left) | self::getModeValue($mode->right);
@@ -298,6 +298,10 @@ private static function getModeValue($mode): int
298298
return $value;
299299
}
300300

301+
if ($mode instanceof Expr\Ternary) {
302+
return null;
303+
}
304+
301305
throw new InvalidConsoleModeException();
302306
}
303307

tests/acceptance/acceptance/console/ConsoleArgument.feature

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,35 @@ Feature: ConsoleArgument
241241
| ->addArgument('arg1', InputArgument::IS_ARRAY) |
242242
| ->addArgument('arg1', InputArgument::IS_ARRAY \| InputArgument::REQUIRED) |
243243
| ->addArgument('arg1', InputArgument::IS_ARRAY \| InputArgument::OPTIONAL) |
244+
245+
Scenario: Assert using ternary operator as argument mode does not raise false positive
246+
Given I have the following code
247+
"""
248+
class MyCommand extends Command
249+
{
250+
private bool $foo;
251+
252+
public function __construct(bool $foo)
253+
{
254+
$this->foo = $foo;
255+
}
256+
257+
public function configure(): void
258+
{
259+
$this->addArgument('arg', $this->foo ? InputArgument::REQUIRED : InputArgument::OPTIONAL);
260+
}
261+
262+
public function execute(InputInterface $input, OutputInterface $output): int
263+
{
264+
/** @psalm-trace $arg */
265+
$arg = $input->getArgument('arg');
266+
267+
return 0;
268+
}
269+
}
270+
"""
271+
When I run Psalm
272+
Then I see these errors
273+
| Type | Message |
274+
| Trace | $arg: null\|string |
275+
And I see no other errors

0 commit comments

Comments
 (0)