-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Implement array shapes for Preg::match
$matches by-ref parameter
#24
Closed
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d68dd8a
Implement array shapes for `Preg::match` $matches by-ref parameter
staabm 5f698b7
added more tests
staabm a83519c
reflect flags behaviour
staabm 79219b3
utilize phpstan feature flag
staabm b88c4b9
declare conflict with phpstan < 1.11.6
staabm 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 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,14 @@ | ||
# composer/pcre PHPStan extensions | ||
# | ||
# These can be reused by third party packages by including 'vendor/composer/pcre/extension.neon' | ||
# in your phpstan config | ||
|
||
services: | ||
- | ||
class: Composer\Pcre\PHPStan\PregMatchParameterOutTypeExtension | ||
tags: | ||
- phpstan.staticMethodParameterOutTypeExtension | ||
- | ||
class: Composer\Pcre\PHPStan\PregMatchTypeSpecifyingExtension | ||
tags: | ||
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension |
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,63 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Composer\Pcre\PHPStan; | ||
|
||
use Composer\Pcre\Preg; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParameterReflection; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\MethodParameterOutTypeExtension; | ||
use PHPStan\Type\Php\RegexArrayShapeMatcher; | ||
use PHPStan\Type\StaticMethodParameterOutTypeExtension; | ||
use PHPStan\Type\Type; | ||
use function in_array; | ||
use function strtolower; | ||
|
||
final class PregMatchParameterOutTypeExtension implements StaticMethodParameterOutTypeExtension | ||
{ | ||
|
||
private RegexArrayShapeMatcher $regexShapeMatcher; | ||
public function __construct( | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
RegexArrayShapeMatcher $regexShapeMatcher | ||
) | ||
{ | ||
$this->regexShapeMatcher = $regexShapeMatcher; | ||
} | ||
|
||
public function isStaticMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool | ||
{ | ||
return | ||
$methodReflection->getDeclaringClass()->getName() === Preg::class | ||
&& $methodReflection->getName() === 'match' | ||
&& $parameter->getName() === 'matches' | ||
; | ||
} | ||
|
||
public function getParameterOutTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, ParameterReflection $parameter, Scope $scope): ?Type | ||
{ | ||
$args = $methodCall->getArgs(); | ||
$patternArg = $args[0] ?? null; | ||
$matchesArg = $args[2] ?? null; | ||
$flagsArg = $args[3] ?? null; | ||
|
||
if ( | ||
$patternArg === null || $matchesArg === null | ||
) { | ||
return null; | ||
} | ||
|
||
$patternType = $scope->getType($patternArg->value); | ||
$flagsType = null; | ||
if ($flagsArg !== null) { | ||
$flagsType = $scope->getType($flagsArg->value); | ||
} | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return $this->regexShapeMatcher->matchType($patternType, $flagsType, TrinaryLogic::createMaybe()); | ||
} | ||
|
||
} |
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,93 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Composer\Pcre\PHPStan; | ||
|
||
use Composer\Pcre\Preg; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
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\Reflection\MethodReflection; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
use PHPStan\Type\MethodTypeSpecifyingExtension; | ||
use PHPStan\Type\Php\RegexArrayShapeMatcher; | ||
use PHPStan\Type\StaticMethodTypeSpecifyingExtension; | ||
use function in_array; | ||
use function strtolower; | ||
|
||
final class PregMatchTypeSpecifyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension | ||
{ | ||
|
||
private TypeSpecifier $typeSpecifier; | ||
|
||
private RegexArrayShapeMatcher $regexShapeMatcher; | ||
|
||
public function __construct( | ||
RegexArrayShapeMatcher $regexShapeMatcher | ||
) | ||
{ | ||
$this->regexShapeMatcher = $regexShapeMatcher; | ||
} | ||
|
||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
public function getClass(): string { | ||
return Preg::class; | ||
} | ||
|
||
public function isStaticMethodSupported(MethodReflection $methodReflection, StaticCall $node, TypeSpecifierContext $context) : bool | ||
{ | ||
return $methodReflection->getName() === 'match' && !$context->null(); | ||
} | ||
|
||
public function specifyTypes(MethodReflection $methodReflection, StaticCall $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(); | ||
} | ||
|
||
$patternType = $scope->getType($patternArg->value); | ||
$flagsType = null; | ||
if ($flagsArg !== null) { | ||
$flagsType = $scope->getType($flagsArg->value); | ||
} | ||
|
||
$matchedType = $this->regexShapeMatcher->matchType($patternType, $flagsType, TrinaryLogic::createFromBoolean($context->true())); | ||
if ($matchedType === null) { | ||
return new SpecifiedTypes(); | ||
} | ||
|
||
$overwrite = false; | ||
if ($context->false()) { | ||
$overwrite = true; | ||
$context = $context->negate(); | ||
} | ||
|
||
return $this->typeSpecifier->create( | ||
$matchesArg->value, | ||
$matchedType, | ||
$context, | ||
$overwrite, | ||
$scope, | ||
$node, | ||
); | ||
} | ||
|
||
} |
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,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of composer/pcre. | ||
* | ||
* (c) Composer <https://github.com/composer> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Composer\Pcre\PHPStanTests; | ||
|
||
use PHPStan\Testing\TypeInferenceTestCase; | ||
|
||
class TypeInferenceTest extends TypeInferenceTestCase | ||
{ | ||
public function dataFileAsserts(): iterable | ||
{ | ||
yield from $this->gatherAssertTypesFromDirectory(__DIR__ . '/nsrt'); | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} | ||
|
||
/** | ||
* @dataProvider dataFileAsserts | ||
* @param mixed ...$args | ||
*/ | ||
public function testFileAsserts( | ||
string $assertType, | ||
string $file, | ||
...$args | ||
): void | ||
{ | ||
$this->assertFileAsserts($assertType, $file, ...$args); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/../../extension.neon', | ||
]; | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace PregMatchShapes; | ||
|
||
use Composer\Pcre\Preg; | ||
use function PHPStan\Testing\assertType; | ||
|
||
function doMatch(string $s): void | ||
{ | ||
if (Preg::match('/Price: /i', $s, $matches)) { | ||
assertType('array{string}', $matches); | ||
} | ||
assertType('array{}|array{string}', $matches); | ||
|
||
if (Preg::match('/Price: (£|€)\d+/', $s, $matches)) { | ||
assertType('array{string, string}', $matches); | ||
} else { | ||
assertType('array{}', $matches); | ||
} | ||
assertType('array{}|array{string, string}', $matches); | ||
} |
Oops, something went wrong.
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.
I was not able to run the phpstan
TypeInferenceTestCase
with the simple bridge.is the bridge vital for this repo, or can we use plain phpunit?