-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e985a9
commit d31922c
Showing
5 changed files
with
97 additions
and
0 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
71 changes: 71 additions & 0 deletions
71
src/Type/Symfony/CommandGetHelperDynamicReturnTypeExtension.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 PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Symfony\ConsoleApplicationResolver; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\TypeUtils; | ||
use Throwable; | ||
use function count; | ||
use function get_class; | ||
|
||
final class CommandGetHelperDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
/** @var \PHPStan\Symfony\ConsoleApplicationResolver */ | ||
private $consoleApplicationResolver; | ||
|
||
public function __construct(ConsoleApplicationResolver $consoleApplicationResolver) | ||
{ | ||
$this->consoleApplicationResolver = $consoleApplicationResolver; | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return 'Symfony\Component\Console\Command\Command'; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'getHelper'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type | ||
{ | ||
$defaultReturnType = new ObjectType('Symfony\Component\Console\Helper\HelperInterface'); | ||
|
||
if (!isset($methodCall->getArgs()[0])) { | ||
return $defaultReturnType; | ||
} | ||
|
||
$classReflection = $scope->getClassReflection(); | ||
if ($classReflection === null) { | ||
return $defaultReturnType; | ||
} | ||
|
||
$argStrings = TypeUtils::getConstantStrings($scope->getType($methodCall->getArgs()[0]->value)); | ||
if (count($argStrings) !== 1) { | ||
return $defaultReturnType; | ||
} | ||
$argName = $argStrings[0]->getValue(); | ||
|
||
$returnTypes = []; | ||
foreach ($this->consoleApplicationResolver->findCommands($classReflection) as $command) { | ||
try { | ||
$command->mergeApplicationDefinition(); | ||
$returnTypes[] = new ObjectType(get_class($command->getHelper($argName))); | ||
} catch (Throwable $e) { | ||
// no-op | ||
} | ||
} | ||
|
||
return count($returnTypes) > 0 ? TypeCombinator::union(...$returnTypes) : $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Command; | ||
|
||
class Command | ||
{ | ||
/** | ||
* @return \Symfony\Component\Console\Helper\HelperInterface | ||
*/ | ||
public function getHelper(string $name); | ||
} |
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,7 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Console\Helper; | ||
|
||
interface HelperInterface | ||
{ | ||
} |
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