|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Nette\PHPStan\Tester; |
| 6 | + |
| 7 | +use PhpParser\Node\Arg; |
| 8 | +use PhpParser\Node\Expr; |
| 9 | +use PhpParser\Node\Expr\BinaryOp\Identical; |
| 10 | +use PhpParser\Node\Expr\BinaryOp\NotIdentical; |
| 11 | +use PhpParser\Node\Expr\BooleanNot; |
| 12 | +use PhpParser\Node\Expr\ConstFetch; |
| 13 | +use PhpParser\Node\Expr\FuncCall; |
| 14 | +use PhpParser\Node\Expr\Instanceof_; |
| 15 | +use PhpParser\Node\Expr\StaticCall; |
| 16 | +use PhpParser\Node\Name; |
| 17 | +use PHPStan\Analyser\Scope; |
| 18 | +use PHPStan\Analyser\SpecifiedTypes; |
| 19 | +use PHPStan\Analyser\TypeSpecifier; |
| 20 | +use PHPStan\Analyser\TypeSpecifierAwareExtension; |
| 21 | +use PHPStan\Analyser\TypeSpecifierContext; |
| 22 | +use PHPStan\Reflection\MethodReflection; |
| 23 | +use PHPStan\Type\StaticMethodTypeSpecifyingExtension; |
| 24 | +use Tester\Assert; |
| 25 | +use function count; |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * Narrows variable types after Tester\Assert assertion calls. |
| 30 | + */ |
| 31 | +class AssertTypeNarrowingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension |
| 32 | +{ |
| 33 | + private TypeSpecifier $typeSpecifier; |
| 34 | + |
| 35 | + |
| 36 | + public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void |
| 37 | + { |
| 38 | + $this->typeSpecifier = $typeSpecifier; |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + public function getClass(): string |
| 43 | + { |
| 44 | + return Assert::class; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + public function isStaticMethodSupported( |
| 49 | + MethodReflection $staticMethodReflection, |
| 50 | + StaticCall $node, |
| 51 | + TypeSpecifierContext $context, |
| 52 | + ): bool |
| 53 | + { |
| 54 | + $minArgs = match ($staticMethodReflection->getName()) { |
| 55 | + 'null', 'notNull', 'true', 'false', 'truthy', 'falsey' => 1, |
| 56 | + 'same', 'notSame', 'type' => 2, |
| 57 | + default => null, |
| 58 | + }; |
| 59 | + return $minArgs !== null && count($node->getArgs()) >= $minArgs; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + public function specifyTypes( |
| 64 | + MethodReflection $staticMethodReflection, |
| 65 | + StaticCall $node, |
| 66 | + Scope $scope, |
| 67 | + TypeSpecifierContext $context, |
| 68 | + ): SpecifiedTypes |
| 69 | + { |
| 70 | + $args = $node->getArgs(); |
| 71 | + $expression = match ($staticMethodReflection->getName()) { |
| 72 | + 'null' => new Identical($args[0]->value, new ConstFetch(new Name('null'))), |
| 73 | + 'notNull' => new NotIdentical($args[0]->value, new ConstFetch(new Name('null'))), |
| 74 | + 'true' => new Identical($args[0]->value, new ConstFetch(new Name('true'))), |
| 75 | + 'false' => new Identical($args[0]->value, new ConstFetch(new Name('false'))), |
| 76 | + 'truthy' => $args[0]->value, |
| 77 | + 'falsey' => new BooleanNot($args[0]->value), |
| 78 | + 'same' => new Identical($args[1]->value, $args[0]->value), |
| 79 | + 'notSame' => new NotIdentical($args[1]->value, $args[0]->value), |
| 80 | + 'type' => $this->createTypeExpression($scope, $args), |
| 81 | + default => null, |
| 82 | + }; |
| 83 | + |
| 84 | + if ($expression === null) { |
| 85 | + return new SpecifiedTypes([], []); |
| 86 | + } |
| 87 | + |
| 88 | + return $this->typeSpecifier->specifyTypesInCondition( |
| 89 | + $scope, |
| 90 | + $expression, |
| 91 | + TypeSpecifierContext::createTruthy(), |
| 92 | + )->setRootExpr($expression); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + /** |
| 97 | + * @param Arg[] $args |
| 98 | + */ |
| 99 | + private function createTypeExpression(Scope $scope, array $args): ?Expr |
| 100 | + { |
| 101 | + $typeType = $scope->getType($args[0]->value); |
| 102 | + $constantStrings = $typeType->getConstantStrings(); |
| 103 | + |
| 104 | + if (count($constantStrings) === 1) { |
| 105 | + $typeName = $constantStrings[0]->getValue(); |
| 106 | + |
| 107 | + $func = match ($typeName) { |
| 108 | + 'list', 'array' => 'is_array', |
| 109 | + 'bool' => 'is_bool', |
| 110 | + 'callable' => 'is_callable', |
| 111 | + 'float' => 'is_float', |
| 112 | + 'int', 'integer' => 'is_int', |
| 113 | + 'null' => 'is_null', |
| 114 | + 'object' => 'is_object', |
| 115 | + 'resource' => 'is_resource', |
| 116 | + 'scalar' => 'is_scalar', |
| 117 | + 'string' => 'is_string', |
| 118 | + default => null, |
| 119 | + }; |
| 120 | + |
| 121 | + return $func !== null |
| 122 | + ? new FuncCall(new Name($func), [$args[1]]) |
| 123 | + : new Instanceof_($args[1]->value, new Name($typeName)); |
| 124 | + } |
| 125 | + |
| 126 | + // object argument → instanceof using its class |
| 127 | + $classNames = $typeType->getObjectClassNames(); |
| 128 | + if (count($classNames) === 1) { |
| 129 | + return new Instanceof_($args[1]->value, new Name($classNames[0])); |
| 130 | + } |
| 131 | + |
| 132 | + return null; |
| 133 | + } |
| 134 | +} |
0 commit comments