You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.5 KiB
PHP

<?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();
}
}