-
Notifications
You must be signed in to change notification settings - Fork 22
Type hints for Safe\preg_match, fixes #40 #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,57 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
/* | ||
Blatantly copy-pasted from PHPStan's source code but with isFunctionSupported changed | ||
|
||
https://github.com/phpstan/phpstan-src/blob/e664bed7b62e2a58d571fb631ddf47030914a2b5/src/Type/Php/PregMatchParameterOutTypeExtension.php | ||
*/ | ||
namespace TheCodingMachine\Safe\PHPStan\Type\Php; | ||
|
||
use PHPStan\Type\Php\RegexArrayShapeMatcher; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\ParameterReflection; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\FunctionParameterOutTypeExtension; | ||
use PHPStan\Type\Type; | ||
use function in_array; | ||
|
||
final class PregMatchParameterOutTypeExtension implements FunctionParameterOutTypeExtension | ||
{ | ||
|
||
public function __construct( | ||
private RegexArrayShapeMatcher $regexShapeMatcher, | ||
) { | ||
} | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool | ||
{ | ||
return in_array($functionReflection->getName(), ['Safe\preg_match', 'Safe\preg_match_all'], true) | ||
// the parameter is named different, depending on PHP version. | ||
&& in_array($parameter->getName(), ['subpatterns', 'matches'], true); | ||
} | ||
|
||
public function getParameterOutTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, ParameterReflection $parameter, Scope $scope): ?Type | ||
{ | ||
$args = $funcCall->getArgs(); | ||
$patternArg = $args[0] ?? null; | ||
$matchesArg = $args[2] ?? null; | ||
$flagsArg = $args[3] ?? null; | ||
|
||
if ($patternArg === null || $matchesArg === null | ||
) { | ||
return null; | ||
} | ||
|
||
$flagsType = null; | ||
if ($flagsArg !== null) { | ||
$flagsType = $scope->getType($flagsArg->value); | ||
} | ||
|
||
if ($functionReflection->getName() === 'Safe\preg_match') { | ||
return $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createMaybe(), $scope); | ||
} | ||
return $this->regexShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createMaybe(), $scope); | ||
} | ||
} |
This file contains hidden or 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,32 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace TheCodingMachine\Safe\PHPStan\Type\Php; | ||
|
||
use PHPStan\Testing\TypeInferenceTestCase; | ||
|
||
class PregMatchParameterOutTypeExtensionTest extends TypeInferenceTestCase | ||
{ | ||
/** | ||
* @return iterable<mixed> | ||
*/ | ||
public static function dataFileAsserts(): iterable | ||
{ | ||
yield from self::gatherAssertTypes(__DIR__ . '/data/preg.php'); | ||
} | ||
|
||
/** | ||
* @dataProvider dataFileAsserts | ||
*/ | ||
public function testFileAsserts( | ||
string $assertType, | ||
string $file, | ||
mixed ...$args | ||
): void { | ||
$this->assertFileAsserts($assertType, $file, ...$args); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [__DIR__ . '/../../../phpstan-safe-rule.neon']; | ||
} | ||
} |
This file contains hidden or 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,15 @@ | ||
<?php | ||
|
||
namespace TheCodingMachine\Safe\PHPStan\Type\Php\data; | ||
|
||
// Checking that preg_match and Safe\preg_match are equivalent | ||
$pattern = '/H(.)ll(o) (World)?/'; | ||
$string = 'Hello World'; | ||
$type = "array{0?: string, 1?: non-empty-string, 2?: 'o', 3?: 'World'}"; | ||
|
||
// @phpstan-ignore-next-line - use of unsafe is intentional | ||
\preg_match($pattern, $string, $matches); | ||
\PHPStan\Testing\assertType($type, $matches); | ||
|
||
\Safe\preg_match($pattern, $string, $matches); | ||
\PHPStan\Testing\assertType($type, $matches); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be fixed by php-cs-fixer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like
tests/
aren't covered by the formatter - I'll manually fix for now, and then cover tests in a follow-up