1
0
Fork 0

added phpstan ConfigReturnTypeExtension

pull/10635/head
Markus Staab 2022-03-18 09:26:31 +01:00 committed by Jordi Boggiano
parent b8b84c2c35
commit 0cd02f6257
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
2 changed files with 56 additions and 0 deletions

View File

@ -58,3 +58,9 @@ parameters:
- Composer\Composer::VERSION
- Composer\Composer::RELEASE_DATE
- Composer\Composer::SOURCE_VERSION
services:
-
class: Composer\PHPStan\ConfigReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

View File

@ -0,0 +1,50 @@
<?php
namespace Composer\PHPStan;
use Composer\Config;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
final class ConfigReturnTypeExtension implements DynamicMethodReturnTypeExtension {
public function getClass(): string
{
return Config::class;
}
public function isMethodSupported(MethodReflection $methodReflection): bool
{
return strtolower($methodReflection->getName()) === 'get';
}
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$args = $methodCall->getArgs();
if (count($args) < 1) {
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}
$keyType = $scope->getType($args[0]->value);
if ($keyType instanceof ConstantStringType) {
if ($keyType->getValue() == 'allow-plugins') {
return TypeCombinator::addNull(
new ArrayType(new StringType(), new BooleanType())
);
}
}
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
}
}