diff --git a/phpstan/rules.neon b/phpstan/rules.neon index 8d81b0dd3..6dae5cfd4 100644 --- a/phpstan/rules.neon +++ b/phpstan/rules.neon @@ -8,3 +8,7 @@ services: class: Composer\PHPStan\ConfigReturnTypeExtension tags: - phpstan.broker.dynamicMethodReturnTypeExtension + - + class: Composer\PHPStan\RuleReasonDataReturnTypeExtension + tags: + - phpstan.broker.dynamicMethodReturnTypeExtension diff --git a/src/Composer/DependencyResolver/Problem.php b/src/Composer/DependencyResolver/Problem.php index 9ded041f4..a1379368a 100644 --- a/src/Composer/DependencyResolver/Problem.php +++ b/src/Composer/DependencyResolver/Problem.php @@ -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)); } } diff --git a/src/Composer/DependencyResolver/Rule.php b/src/Composer/DependencyResolver/Rule.php index dba29ed1b..1b2f1aa95 100644 --- a/src/Composer/DependencyResolver/Rule.php +++ b/src/Composer/DependencyResolver/Rule.php @@ -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)'; // } diff --git a/src/Composer/PHPStan/RuleReasonDataReturnTypeExtension.php b/src/Composer/PHPStan/RuleReasonDataReturnTypeExtension.php new file mode 100644 index 000000000..c68e43a14 --- /dev/null +++ b/src/Composer/PHPStan/RuleReasonDataReturnTypeExtension.php @@ -0,0 +1,78 @@ + + * Jordi Boggiano + * + * 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); + } +}