Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infer correct type for
$input->getOptions()
call
- Loading branch information
1 parent
f4cb3b8
commit 70f8452
Showing
6 changed files
with
123 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Symfony; | ||
|
||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\BooleanType; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
class GetOptionTypeHelper | ||
{ | ||
|
||
public function getOptionType(Scope $scope, InputOption $option): Type | ||
{ | ||
if (!$option->acceptValue()) { | ||
return new BooleanType(); | ||
} | ||
|
||
$optType = TypeCombinator::union(new StringType(), new NullType()); | ||
if ($option->isValueRequired() && ($option->isArray() || $option->getDefault() !== null)) { | ||
$optType = TypeCombinator::removeNull($optType); | ||
} | ||
if ($option->isArray()) { | ||
$optType = new ArrayType(new IntegerType(), $optType); | ||
} | ||
|
||
return TypeCombinator::union($optType, $scope->getTypeFromValue($option->getDefault())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/Type/Symfony/InputInterfaceGetOptionsDynamicReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Symfony; | ||
|
||
use InvalidArgumentException; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Symfony\ConsoleApplicationResolver; | ||
use PHPStan\Type\Constant\ConstantArrayTypeBuilder; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use function count; | ||
|
||
final class InputInterfaceGetOptionsDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
/** @var ConsoleApplicationResolver */ | ||
private $consoleApplicationResolver; | ||
|
||
/** @var GetOptionTypeHelper */ | ||
private $getOptionTypeHelper; | ||
|
||
public function __construct(ConsoleApplicationResolver $consoleApplicationResolver, GetOptionTypeHelper $getOptionTypeHelper) | ||
{ | ||
$this->consoleApplicationResolver = $consoleApplicationResolver; | ||
$this->getOptionTypeHelper = $getOptionTypeHelper; | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return 'Symfony\Component\Console\Input\InputInterface'; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'getOptions'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type | ||
{ | ||
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType(); | ||
$classReflection = $scope->getClassReflection(); | ||
if ($classReflection === null) { | ||
return $defaultReturnType; | ||
} | ||
|
||
$optTypes = []; | ||
foreach ($this->consoleApplicationResolver->findCommands($classReflection) as $command) { | ||
try { | ||
$command->mergeApplicationDefinition(); | ||
$options = $command->getDefinition()->getOptions(); | ||
$builder = ConstantArrayTypeBuilder::createEmpty(); | ||
foreach ($options as $name => $option) { | ||
$optionType = $this->getOptionTypeHelper->getOptionType($scope, $option); | ||
$builder->setOffsetValueType(new ConstantStringType($name), $optionType); | ||
} | ||
|
||
$optTypes[] = $builder->getArray(); | ||
} catch (InvalidArgumentException $e) { | ||
// noop | ||
} | ||
} | ||
|
||
return count($optTypes) > 0 ? TypeCombinator::union(...$optTypes) : $defaultReturnType; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters