Skip to content

Commit

Permalink
[console] fix required argument value analysis (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
seferov committed Jan 18, 2023
1 parent 80670da commit 9756ee3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/Handler/ConsoleHandler.php
Expand Up @@ -31,11 +31,11 @@ class ConsoleHandler implements AfterMethodCallAnalysisInterface
/**
* @var Union[]
*/
private static $arguments = [];
private static array $arguments = [];
/**
* @var Union[]
*/
private static $options = [];
private static array $options = [];

/**
* {@inheritdoc}
Expand Down Expand Up @@ -157,11 +157,8 @@ private static function analyseArgument(array $args, StatementsSource $statement
}

$defaultParam = $normalizedParams['default'];
if ($defaultParam) {
if ($defaultParam && (!$defaultParam->value instanceof Expr\ConstFetch || 'null' !== $defaultParam->value->name->parts[0])) {
$returnTypes->removeType('null');
if ($defaultParam->value instanceof Expr\ConstFetch && 'null' === $defaultParam->value->name->parts[0]) {
$returnTypes->addType(new TNull());
}
}

self::$arguments[$identifier] = $returnTypes;
Expand Down
5 changes: 5 additions & 0 deletions tests/acceptance/acceptance/console/ConsoleArgument.feature
Expand Up @@ -168,6 +168,7 @@ Feature: ConsoleArgument
->addArgument('arg5', InputArgument::OPTIONAL, '', 'default value')
->addArgument('arg6', InputArgument::OPTIONAL)
->addArgument('arg7', InputArgument::OPTIONAL, '', null)
->addArgument('arg8', InputArgument::REQUIRED, '', null)
;
}
Expand All @@ -194,6 +195,9 @@ Feature: ConsoleArgument
/** @psalm-trace $arg7 */
$arg7 = $input->getArgument('arg7');
/** @psalm-trace $arg8 */
$arg8 = $input->getArgument('arg8');
return 0;
}
}
Expand All @@ -208,6 +212,7 @@ Feature: ConsoleArgument
| Trace | $arg5: string |
| Trace | $arg6: null\|string |
| Trace | $arg7: null\|string |
| Trace | $arg8: string |
And I see no other errors

Scenario Outline: Asserting array arguments return types have inferred
Expand Down

0 comments on commit 9756ee3

Please sign in to comment.