-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
additional magic for preg_match, fixes #46
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace TheCodingMachine\Safe\PHPStan\Type\Php; | ||
|
||
use PHPStan\Type\Php\RegexArrayShapeMatcher; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Analyser\SpecifiedTypes; | ||
use PHPStan\Analyser\TypeSpecifier; | ||
use PHPStan\Analyser\TypeSpecifierAwareExtension; | ||
use PHPStan\Analyser\TypeSpecifierContext; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
use function in_array; | ||
use function strtolower; | ||
|
||
final class PregMatchTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension | ||
{ | ||
|
||
private TypeSpecifier $typeSpecifier; | ||
|
||
public function __construct( | ||
private RegexArrayShapeMatcher $regexShapeMatcher, | ||
) | ||
{ | ||
} | ||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool | ||
{ | ||
return in_array(strtolower($functionReflection->getName()), ['safe\preg_match', 'safe\preg_match_all'], true) && !$context->null(); | ||
} | ||
|
||
public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes | ||
{ | ||
$args = $node->getArgs(); | ||
$patternArg = $args[0] ?? null; | ||
$matchesArg = $args[2] ?? null; | ||
$flagsArg = $args[3] ?? null; | ||
|
||
if ( | ||
$patternArg === null || $matchesArg === null | ||
) { | ||
return new SpecifiedTypes(); | ||
} | ||
|
||
$flagsType = null; | ||
if ($flagsArg !== null) { | ||
$flagsType = $scope->getType($flagsArg->value); | ||
} | ||
|
||
if ($functionReflection->getName() === 'Safe\preg_match') { | ||
$matchedType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope); | ||
} else { | ||
$matchedType = $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createFromBoolean($context->true()), $scope); | ||
} | ||
if ($matchedType === null) { | ||
return new SpecifiedTypes(); | ||
} | ||
|
||
$overwrite = false; | ||
if ($context->false()) { | ||
$overwrite = true; | ||
$context = $context->negate(); | ||
} | ||
|
||
$types = $this->typeSpecifier->create( | ||
$matchesArg->value, | ||
$matchedType, | ||
$context, | ||
$scope, | ||
)->setRootExpr($node); | ||
if ($overwrite) { | ||
$types = $types->setAlwaysOverwriteTypes(); | ||
} | ||
|
||
return $types; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters