|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace TheCodingMachine\Safe\PHPStan\Type\Php; |
| 4 | + |
| 5 | +use PHPStan\Type\Php\RegexArrayShapeMatcher; |
| 6 | +use PhpParser\Node\Expr\FuncCall; |
| 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\FunctionReflection; |
| 13 | +use PHPStan\TrinaryLogic; |
| 14 | +use PHPStan\Type\FunctionTypeSpecifyingExtension; |
| 15 | +use function in_array; |
| 16 | +use function strtolower; |
| 17 | + |
| 18 | +final class PregMatchTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension |
| 19 | +{ |
| 20 | + |
| 21 | + private TypeSpecifier $typeSpecifier; |
| 22 | + |
| 23 | + public function __construct( |
| 24 | + private RegexArrayShapeMatcher $regexShapeMatcher, |
| 25 | + ) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void |
| 30 | + { |
| 31 | + $this->typeSpecifier = $typeSpecifier; |
| 32 | + } |
| 33 | + |
| 34 | + public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool |
| 35 | + { |
| 36 | + return in_array(strtolower($functionReflection->getName()), ['safe\preg_match', 'safe\preg_match_all'], true) && !$context->null(); |
| 37 | + } |
| 38 | + |
| 39 | + public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes |
| 40 | + { |
| 41 | + $args = $node->getArgs(); |
| 42 | + $patternArg = $args[0] ?? null; |
| 43 | + $matchesArg = $args[2] ?? null; |
| 44 | + $flagsArg = $args[3] ?? null; |
| 45 | + |
| 46 | + if ( |
| 47 | + $patternArg === null || $matchesArg === null |
| 48 | + ) { |
| 49 | + return new SpecifiedTypes(); |
| 50 | + } |
| 51 | + |
| 52 | + $flagsType = null; |
| 53 | + if ($flagsArg !== null) { |
| 54 | + $flagsType = $scope->getType($flagsArg->value); |
| 55 | + } |
| 56 | + |
| 57 | + if ($functionReflection->getName() === 'Safe\preg_match') { |
| 58 | + $matchedType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope); |
| 59 | + } else { |
| 60 | + $matchedType = $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope); |
| 61 | + } |
| 62 | + if ($matchedType === null) { |
| 63 | + return new SpecifiedTypes(); |
| 64 | + } |
| 65 | + |
| 66 | + $overwrite = false; |
| 67 | + if ($context->false()) { |
| 68 | + $overwrite = true; |
| 69 | + $context = $context->negate(); |
| 70 | + } |
| 71 | + |
| 72 | + $types = $this->typeSpecifier->create( |
| 73 | + $matchesArg->value, |
| 74 | + $matchedType, |
| 75 | + $context, |
| 76 | + $scope, |
| 77 | + )->setRootExpr($node); |
| 78 | + if ($overwrite) { |
| 79 | + $types = $types->setAlwaysOverwriteTypes(); |
| 80 | + } |
| 81 | + |
| 82 | + return $types; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments