1
0
Fork 0

Add RuleReasonDataReturnTypeExtension to resolve ReasonData types where possible in PHPStan

pull/11004/head
Jordi Boggiano 2022-08-17 14:59:24 +02:00
parent 131da999ac
commit d48acda485
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
4 changed files with 140 additions and 72 deletions

View File

@ -8,3 +8,7 @@ services:
class: Composer\PHPStan\ConfigReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: Composer\PHPStan\RuleReasonDataReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

View File

@ -91,13 +91,8 @@ class Problem
$packageName = $reasonData['packageName'];
$constraint = $reasonData['constraint'];
if (isset($constraint)) {
$packages = $pool->whatProvides($packageName, $constraint);
} else {
$packages = [];
}
if (empty($packages)) {
$packages = $pool->whatProvides($packageName, $constraint);
if (count($packages) === 0) {
return "\n ".implode(self::getMissingPackageReason($repositorySet, $request, $pool, $isVerbose, $packageName, $constraint));
}
}

View File

@ -100,18 +100,13 @@ abstract class Rule
public function getRequiredPackage(): ?string
{
$reason = $this->getReason();
if ($reason === self::RULE_ROOT_REQUIRE) {
return $this->reasonData['packageName'];
}
if ($reason === self::RULE_FIXED) {
return $this->reasonData['package']->getName();
}
if ($reason === self::RULE_PACKAGE_REQUIRES) {
return $this->reasonData->getTarget();
switch ($this->getReason()) {
case self::RULE_ROOT_REQUIRE:
return $this->getReasonData()['packageName'];
case self::RULE_FIXED:
return $this->getReasonData()['package']->getName();
case self::RULE_PACKAGE_REQUIRES:
return $this->getReasonData()->getTarget();
}
return null;
@ -155,16 +150,16 @@ abstract class Rule
public function isCausedByLock(RepositorySet $repositorySet, Request $request, Pool $pool): bool
{
if ($this->getReason() === self::RULE_PACKAGE_REQUIRES) {
if (PlatformRepository::isPlatformPackage($this->reasonData->getTarget())) {
if (PlatformRepository::isPlatformPackage($this->getReasonData()->getTarget())) {
return false;
}
if ($request->getLockedRepository()) {
foreach ($request->getLockedRepository()->getPackages() as $package) {
if ($package->getName() === $this->reasonData->getTarget()) {
if ($package->getName() === $this->getReasonData()->getTarget()) {
if ($pool->isUnacceptableFixedOrLockedPackage($package)) {
return true;
}
if (!$this->reasonData->getConstraint()->matches(new Constraint('=', $package->getVersion()))) {
if (!$this->getReasonData()->getConstraint()->matches(new Constraint('=', $package->getVersion()))) {
return true;
}
// required package was locked but has been unlocked and still matches
@ -178,16 +173,16 @@ abstract class Rule
}
if ($this->getReason() === self::RULE_ROOT_REQUIRE) {
if (PlatformRepository::isPlatformPackage($this->reasonData['packageName'])) {
if (PlatformRepository::isPlatformPackage($this->getReasonData()['packageName'])) {
return false;
}
if ($request->getLockedRepository()) {
foreach ($request->getLockedRepository()->getPackages() as $package) {
if ($package->getName() === $this->reasonData['packageName']) {
if ($package->getName() === $this->getReasonData()['packageName']) {
if ($pool->isUnacceptableFixedOrLockedPackage($package)) {
return true;
}
if (!$this->reasonData['constraint']->matches(new Constraint('=', $package->getVersion()))) {
if (!$this->getReasonData()['constraint']->matches(new Constraint('=', $package->getVersion()))) {
return true;
}
break;
@ -211,11 +206,10 @@ abstract class Rule
$package1 = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[0]));
$package2 = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[1]));
if ($reasonData = $this->getReasonData()) {
// swap literals if they are not in the right order with package2 being the conflicter
if ($reasonData->getSource() === $package1->getName()) {
[$package2, $package1] = [$package1, $package2];
}
$reasonData = $this->getReasonData();
// swap literals if they are not in the right order with package2 being the conflicter
if ($reasonData->getSource() === $package1->getName()) {
[$package2, $package1] = [$package1, $package2];
}
return $package2;
@ -241,12 +235,13 @@ abstract class Rule
switch ($this->getReason()) {
case self::RULE_ROOT_REQUIRE:
$packageName = $this->reasonData['packageName'];
$constraint = $this->reasonData['constraint'];
$reasonData = $this->getReasonData();
$packageName = $reasonData['packageName'];
$constraint = $reasonData['constraint'];
$packages = $pool->whatProvides($packageName, $constraint);
if (!$packages) {
return 'No package found to satisfy root composer.json require '.$packageName.($constraint ? ' '.$constraint->getPrettyString() : '');
return 'No package found to satisfy root composer.json require '.$packageName.' '.$constraint->getPrettyString();
}
$packagesNonAlias = array_values(array_filter($packages, static function ($p): bool {
@ -259,10 +254,10 @@ abstract class Rule
}
}
return 'Root composer.json requires '.$packageName.($constraint ? ' '.$constraint->getPrettyString() : '').' -> satisfiable by '.$this->formatPackagesUnique($pool, $packages, $isVerbose, $constraint).'.';
return 'Root composer.json requires '.$packageName.' '.$constraint->getPrettyString().' -> satisfiable by '.$this->formatPackagesUnique($pool, $packages, $isVerbose, $constraint).'.';
case self::RULE_FIXED:
$package = $this->deduplicateDefaultBranchAlias($this->reasonData['package']);
$package = $this->deduplicateDefaultBranchAlias($this->getReasonData()['package']);
if ($request->isLockedPackage($package)) {
return $package->getPrettyName().' is locked to version '.$package->getPrettyVersion().' and an update of this package was not requested.';
@ -275,38 +270,36 @@ abstract class Rule
$package2 = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($literals[1]));
$conflictTarget = $package1->getPrettyString();
if ($reasonData = $this->getReasonData()) {
assert($reasonData instanceof Link);
$reasonData = $this->getReasonData();
// swap literals if they are not in the right order with package2 being the conflicter
if ($reasonData->getSource() === $package1->getName()) {
[$package2, $package1] = [$package1, $package2];
$conflictTarget = $package1->getPrettyName().' '.$reasonData->getPrettyConstraint();
// swap literals if they are not in the right order with package2 being the conflicter
if ($reasonData->getSource() === $package1->getName()) {
[$package2, $package1] = [$package1, $package2];
$conflictTarget = $package1->getPrettyName().' '.$reasonData->getPrettyConstraint();
}
// if the conflict is not directly against the package but something it provides/replaces,
// we try to find that link to display a better message
if ($reasonData->getTarget() !== $package1->getName()) {
$provideType = null;
$provided = null;
foreach ($package1->getProvides() as $provide) {
if ($provide->getTarget() === $reasonData->getTarget()) {
$provideType = 'provides';
$provided = $provide->getPrettyConstraint();
break;
}
}
// if the conflict is not directly against the package but something it provides/replaces,
// we try to find that link to display a better message
if ($reasonData->getTarget() !== $package1->getName()) {
$provideType = null;
$provided = null;
foreach ($package1->getProvides() as $provide) {
if ($provide->getTarget() === $reasonData->getTarget()) {
$provideType = 'provides';
$provided = $provide->getPrettyConstraint();
break;
}
}
foreach ($package1->getReplaces() as $replace) {
if ($replace->getTarget() === $reasonData->getTarget()) {
$provideType = 'replaces';
$provided = $replace->getPrettyConstraint();
break;
}
}
if (null !== $provideType) {
$conflictTarget = $reasonData->getTarget().' '.$reasonData->getPrettyConstraint().' ('.$package1->getPrettyString().' '.$provideType.' '.$reasonData->getTarget().' '.$provided.')';
foreach ($package1->getReplaces() as $replace) {
if ($replace->getTarget() === $reasonData->getTarget()) {
$provideType = 'replaces';
$provided = $replace->getPrettyConstraint();
break;
}
}
if (null !== $provideType) {
$conflictTarget = $reasonData->getTarget().' '.$reasonData->getPrettyConstraint().' ('.$package1->getPrettyString().' '.$provideType.' '.$reasonData->getTarget().' '.$provided.')';
}
}
return $package2->getPrettyString().' conflicts with '.$conflictTarget.'.';
@ -314,8 +307,7 @@ abstract class Rule
case self::RULE_PACKAGE_REQUIRES:
$sourceLiteral = array_shift($literals);
$sourcePackage = $this->deduplicateDefaultBranchAlias($pool->literalToPackage($sourceLiteral));
/** @var Link */
$reasonData = $this->reasonData;
$reasonData = $this->getReasonData();
$requires = [];
foreach ($literals as $literal) {
@ -324,11 +316,11 @@ abstract class Rule
$text = $reasonData->getPrettyString($sourcePackage);
if ($requires) {
$text .= ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $requires, $isVerbose, $this->reasonData->getConstraint()) . '.';
$text .= ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $requires, $isVerbose, $reasonData->getConstraint()) . '.';
} else {
$targetName = $reasonData->getTarget();
$reason = Problem::getMissingPackageReason($repositorySet, $request, $pool, $isVerbose, $targetName, $this->reasonData->getConstraint());
$reason = Problem::getMissingPackageReason($repositorySet, $request, $pool, $isVerbose, $targetName, $reasonData->getConstraint());
return $text . ' -> ' . $reason[1];
}
@ -341,8 +333,7 @@ abstract class Rule
$package = $pool->literalToPackage($literal);
$packageNames[$package->getName()] = true;
}
/** @var string $replacedName */
$replacedName = $this->reasonData;
$replacedName = $this->getReasonData();
if (count($packageNames) > 1) {
$reason = null;
@ -382,9 +373,9 @@ abstract class Rule
return 'You can only install one version of a package, so only one of these can be installed: ' . $this->formatPackagesUnique($pool, $literals, $isVerbose, null, true) . '.';
case self::RULE_LEARNED:
/** @TODO currently still generates way too much output to be helpful, and in some cases can even lead to endless recursion */
// if (isset($learnedPool[$this->reasonData])) {
// echo $this->reasonData."\n";
// $learnedString = ', learned rules:' . Problem::formatDeduplicatedRules($learnedPool[$this->reasonData], ' ', $repositorySet, $request, $pool, $isVerbose, $installedMap, $learnedPool);
// if (isset($learnedPool[$this->getReasonData()])) {
// echo $this->getReasonData()."\n";
// $learnedString = ', learned rules:' . Problem::formatDeduplicatedRules($learnedPool[$this->getReasonData()], ' ', $repositorySet, $request, $pool, $isVerbose, $installedMap, $learnedPool);
// } else {
// $learnedString = ' (reasoning unavailable)';
// }

View File

@ -0,0 +1,78 @@
<?php declare(strict_types=1);
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\PHPStan;
use Composer\Config;
use Composer\DependencyResolver\Rule;
use Composer\Json\JsonFile;
use Composer\Package\BasePackage;
use Composer\Package\Link;
use Composer\Semver\Constraint\ConstraintInterface;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use PhpParser\Node\Identifier;
final class RuleReasonDataReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return Rule::class;
}
public function isMethodSupported(MethodReflection $methodReflection): bool
{
return strtolower($methodReflection->getName()) === 'getreasondata';
}
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
$reasonType = $scope->getType(new MethodCall($methodCall->var, new Identifier('getReason')));
$types = [
Rule::RULE_ROOT_REQUIRE => new ConstantArrayType([new ConstantStringType('packageName'), new ConstantStringType('constraint')], [new StringType, new ObjectType(ConstraintInterface::class)]),
Rule::RULE_FIXED => new ConstantArrayType([new ConstantStringType('package')], [new ObjectType(BasePackage::class)]),
Rule::RULE_PACKAGE_CONFLICT => new ObjectType(Link::class),
Rule::RULE_PACKAGE_REQUIRES => new ObjectType(Link::class),
Rule::RULE_PACKAGE_SAME_NAME => TypeCombinator::intersect(new StringType, new AccessoryNonEmptyStringType()),
Rule::RULE_LEARNED => new IntegerType(),
Rule::RULE_PACKAGE_ALIAS => new ObjectType(BasePackage::class),
Rule::RULE_PACKAGE_INVERSE_ALIAS => new ObjectType(BasePackage::class),
];
foreach ($types as $const => $type) {
if ((new ConstantIntegerType($const))->isSuperTypeOf($reasonType)->yes()) {
return $type;
}
}
return TypeCombinator::union(...$types);
}
}