|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Composer\Pcre\PHPStan; |
| 4 | + |
| 5 | +use Composer\Pcre\Preg; |
| 6 | +use PhpParser\Node\Expr\StaticCall; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Analyser\SpecifiedTypes; |
| 9 | +use PHPStan\Analyser\TypeSpecifier; |
| 10 | +use PHPStan\Analyser\TypeSpecifierAwareExtension; |
| 11 | +use PHPStan\Analyser\TypeSpecifierContext; |
| 12 | +use PHPStan\Reflection\MethodReflection; |
| 13 | +use PHPStan\TrinaryLogic; |
| 14 | +use PHPStan\Type\Php\RegexArrayShapeMatcher; |
| 15 | +use PHPStan\Type\StaticMethodTypeSpecifyingExtension; |
| 16 | + |
| 17 | +final class PregMatchTypeSpecifyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var TypeSpecifier |
| 21 | + */ |
| 22 | + private $typeSpecifier; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var RegexArrayShapeMatcher |
| 26 | + */ |
| 27 | + private $regexShapeMatcher; |
| 28 | + |
| 29 | + public function __construct(RegexArrayShapeMatcher $regexShapeMatcher) |
| 30 | + { |
| 31 | + $this->regexShapeMatcher = $regexShapeMatcher; |
| 32 | + } |
| 33 | + |
| 34 | + public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void |
| 35 | + { |
| 36 | + $this->typeSpecifier = $typeSpecifier; |
| 37 | + } |
| 38 | + |
| 39 | + public function getClass(): string |
| 40 | + { |
| 41 | + return Preg::class; |
| 42 | + } |
| 43 | + |
| 44 | + public function isStaticMethodSupported(MethodReflection $methodReflection, StaticCall $node, TypeSpecifierContext $context): bool |
| 45 | + { |
| 46 | + return $methodReflection->getName() === 'match' && !$context->null(); |
| 47 | + } |
| 48 | + |
| 49 | + public function specifyTypes(MethodReflection $methodReflection, StaticCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes |
| 50 | + { |
| 51 | + $args = $node->getArgs(); |
| 52 | + $patternArg = $args[0] ?? null; |
| 53 | + $matchesArg = $args[2] ?? null; |
| 54 | + $flagsArg = $args[3] ?? null; |
| 55 | + |
| 56 | + if ( |
| 57 | + $patternArg === null || $matchesArg === null |
| 58 | + ) { |
| 59 | + return new SpecifiedTypes(); |
| 60 | + } |
| 61 | + |
| 62 | + $flagsType = PregMatchFlags::getType($flagsArg, $scope); |
| 63 | + if ($flagsType === null) { |
| 64 | + return new SpecifiedTypes(); |
| 65 | + } |
| 66 | + $patternType = $scope->getType($patternArg->value); |
| 67 | + |
| 68 | + $matchedType = $this->regexShapeMatcher->matchType($patternType, $flagsType, TrinaryLogic::createFromBoolean($context->true())); |
| 69 | + if ($matchedType === null) { |
| 70 | + return new SpecifiedTypes(); |
| 71 | + } |
| 72 | + |
| 73 | + $overwrite = false; |
| 74 | + if ($context->false()) { |
| 75 | + $overwrite = true; |
| 76 | + $context = $context->negate(); |
| 77 | + } |
| 78 | + |
| 79 | + return $this->typeSpecifier->create( |
| 80 | + $matchesArg->value, |
| 81 | + $matchedType, |
| 82 | + $context, |
| 83 | + $overwrite, |
| 84 | + $scope, |
| 85 | + $node |
| 86 | + ); |
| 87 | + } |
| 88 | +} |
0 commit comments