|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Generics; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\DependencyInjection\AutowiredParameter; |
| 8 | +use PHPStan\DependencyInjection\RegisteredRule; |
| 9 | +use PHPStan\Node\InClassNode; |
| 10 | +use PHPStan\Reflection\ClassReflection; |
| 11 | +use PHPStan\Reflection\ConsistentTemplatesHelper; |
| 12 | +use PHPStan\Rules\Rule; |
| 13 | +use PHPStan\Rules\RuleErrorBuilder; |
| 14 | +use PHPStan\Type\Generic\TemplateType; |
| 15 | +use PHPStan\Type\Generic\TemplateTypeHelper; |
| 16 | +use PHPStan\Type\Generic\TemplateTypeVariance; |
| 17 | +use PHPStan\Type\Generic\TemplateTypeVarianceMap; |
| 18 | +use PHPStan\Type\VerbosityLevel; |
| 19 | +use function array_keys; |
| 20 | +use function array_values; |
| 21 | +use function count; |
| 22 | +use function sprintf; |
| 23 | +use function strtolower; |
| 24 | +use function ucfirst; |
| 25 | + |
| 26 | +/** |
| 27 | + * A class below one with `@phpstan-consistent-templates` declares the same template |
| 28 | + * types, passes them on in the same order and bounds them at most as tightly. |
| 29 | + * |
| 30 | + * @implements Rule<InClassNode> |
| 31 | + */ |
| 32 | +#[RegisteredRule(level: 2)] |
| 33 | +final class ConsistentTemplatesRule implements Rule |
| 34 | +{ |
| 35 | + |
| 36 | + public function __construct( |
| 37 | + private ConsistentTemplatesHelper $consistentTemplatesHelper, |
| 38 | + #[AutowiredParameter(ref: '%featureToggles.checkConsistentTemplates%')] |
| 39 | + private bool $checkConsistentTemplates, |
| 40 | + ) |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + public function getNodeType(): string |
| 45 | + { |
| 46 | + return InClassNode::class; |
| 47 | + } |
| 48 | + |
| 49 | + public function processNode(Node $node, Scope $scope): array |
| 50 | + { |
| 51 | + if (!$this->checkConsistentTemplates) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + $classReflection = $node->getClassReflection(); |
| 56 | + if ($classReflection->isAnonymous()) { |
| 57 | + return []; |
| 58 | + } |
| 59 | + |
| 60 | + $ancestors = array_values($classReflection->getImmediateInterfaces()); |
| 61 | + $parent = $classReflection->getParentClass(); |
| 62 | + if ($parent !== null) { |
| 63 | + $ancestors = [$parent, ...$ancestors]; |
| 64 | + } |
| 65 | + |
| 66 | + $errors = []; |
| 67 | + foreach ($ancestors as $ancestor) { |
| 68 | + $consistentClass = $this->consistentTemplatesHelper->findConsistentTemplatesClass($ancestor); |
| 69 | + if ($consistentClass === null) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + foreach ($this->check($classReflection, $ancestor, $consistentClass) as $message) { |
| 74 | + $errors[] = RuleErrorBuilder::message($message) |
| 75 | + ->identifier('generics.inconsistentTemplates') |
| 76 | + ->build(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return $errors; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return list<string> |
| 85 | + */ |
| 86 | + private function check(ClassReflection $classReflection, ClassReflection $ancestor, ClassReflection $consistentClass): array |
| 87 | + { |
| 88 | + $classDescription = sprintf('%s %s', strtolower($classReflection->getClassTypeDescription()), $classReflection->getDisplayName(false)); |
| 89 | + $ancestorDescription = sprintf('%s %s with @phpstan-consistent-templates', strtolower($ancestor->getClassTypeDescription()), $ancestor->getDisplayName(false)); |
| 90 | + if ($consistentClass->getName() !== $ancestor->getName()) { |
| 91 | + $ancestorDescription .= sprintf(' from %s %s', strtolower($consistentClass->getClassTypeDescription()), $consistentClass->getDisplayName(false)); |
| 92 | + } |
| 93 | + |
| 94 | + $ownNames = array_keys($classReflection->getTemplateTags()); |
| 95 | + $ancestorNames = array_keys($ancestor->getTemplateTags()); |
| 96 | + if (count($ownNames) !== count($ancestorNames)) { |
| 97 | + return [sprintf( |
| 98 | + '%s declares %d template %s, but %s declares %d.', |
| 99 | + ucfirst($classDescription), |
| 100 | + count($ownNames), |
| 101 | + count($ownNames) === 1 ? 'type' : 'types', |
| 102 | + $ancestorDescription, |
| 103 | + count($ancestorNames), |
| 104 | + )]; |
| 105 | + } |
| 106 | + |
| 107 | + $passed = $classReflection->getAncestorWithClassName($ancestor->getName()); |
| 108 | + if ($passed === null) { |
| 109 | + return []; |
| 110 | + } |
| 111 | + |
| 112 | + $passedTypes = $passed->getActiveTemplateTypeMap(); |
| 113 | + $ownTypes = $classReflection->getTemplateTypeMap(); |
| 114 | + $ancestorTypes = $ancestor->getTemplateTypeMap(); |
| 115 | + $messages = []; |
| 116 | + foreach ($ancestorNames as $i => $ancestorName) { |
| 117 | + $ownName = $ownNames[$i]; |
| 118 | + $passedType = $passedTypes->getType($ancestorName); |
| 119 | + if ( |
| 120 | + !$passedType instanceof TemplateType |
| 121 | + || $passedType->getName() !== $ownName |
| 122 | + || $passedType->getScope()->getClassName() !== $classReflection->getName() |
| 123 | + || $passedType->getScope()->getFunctionName() !== null |
| 124 | + ) { |
| 125 | + return [sprintf( |
| 126 | + '%s does not pass its template types to %s in the order it declares them.', |
| 127 | + ucfirst($classDescription), |
| 128 | + $ancestorDescription, |
| 129 | + )]; |
| 130 | + } |
| 131 | + |
| 132 | + $ownType = $ownTypes->getType($ownName); |
| 133 | + $ancestorType = $ancestorTypes->getType($ancestorName); |
| 134 | + if (!$ownType instanceof TemplateType || !$ancestorType instanceof TemplateType) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + // the ancestor's bound in terms of the own template types |
| 139 | + $ancestorBound = TemplateTypeHelper::resolveTemplateTypes( |
| 140 | + $ancestorType->getBound(), |
| 141 | + $passedTypes, |
| 142 | + TemplateTypeVarianceMap::createEmpty(), |
| 143 | + TemplateTypeVariance::createInvariant(), |
| 144 | + ); |
| 145 | + if ($ownType->getBound()->isSuperTypeOf($ancestorBound)->yes()) { |
| 146 | + continue; |
| 147 | + } |
| 148 | + |
| 149 | + $messages[] = sprintf( |
| 150 | + 'Bound type %s of template type %s of %s is narrower than bound type %s of template type %s of %s.', |
| 151 | + $ownType->getBound()->describe(VerbosityLevel::typeOnly()), |
| 152 | + $ownName, |
| 153 | + $classDescription, |
| 154 | + $ancestorBound->describe(VerbosityLevel::typeOnly()), |
| 155 | + $ancestorName, |
| 156 | + $ancestorDescription, |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + return $messages; |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments