-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PHPStan type extension for the replaceCallback callable (#33)
- Loading branch information
Showing
6 changed files
with
238 additions
and
2 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
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,67 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Composer\Pcre\PHPStan; | ||
|
||
use Composer\Pcre\Preg; | ||
use Composer\Pcre\Regex; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\Native\NativeParameterReflection; | ||
use PHPStan\Reflection\ParameterReflection; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\ClosureType; | ||
use PHPStan\Type\Php\RegexArrayShapeMatcher; | ||
use PHPStan\Type\StaticMethodParameterClosureTypeExtension; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
|
||
final class PregReplaceCallbackClosureTypeExtension implements StaticMethodParameterClosureTypeExtension | ||
{ | ||
/** | ||
* @var RegexArrayShapeMatcher | ||
*/ | ||
private $regexShapeMatcher; | ||
|
||
public function __construct(RegexArrayShapeMatcher $regexShapeMatcher) | ||
{ | ||
$this->regexShapeMatcher = $regexShapeMatcher; | ||
} | ||
|
||
public function isStaticMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool | ||
{ | ||
return in_array($methodReflection->getDeclaringClass()->getName(), [Preg::class, Regex::class], true) | ||
&& in_array($methodReflection->getName(), ['replaceCallback'], true) | ||
&& $parameter->getName() === 'replacement'; | ||
} | ||
|
||
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, ParameterReflection $parameter, Scope $scope): ?Type | ||
{ | ||
$args = $methodCall->getArgs(); | ||
$patternArg = $args[0] ?? null; | ||
$flagsArg = $args[5] ?? null; | ||
|
||
if ( | ||
$patternArg === null | ||
) { | ||
return null; | ||
} | ||
|
||
$flagsType = null; | ||
if ($flagsArg !== null) { | ||
$flagsType = $scope->getType($flagsArg->value); | ||
} | ||
|
||
$matchesType = $this->regexShapeMatcher->matchExpr($patternArg->value, $flagsType, TrinaryLogic::createYes(), $scope); | ||
if ($matchesType === null) { | ||
return null; | ||
} | ||
|
||
return new ClosureType( | ||
[ | ||
new NativeParameterReflection($parameter->getName(), $parameter->isOptional(), $matchesType, $parameter->passedByReference(), $parameter->isVariadic(), $parameter->getDefaultValue()), | ||
], | ||
new StringType() | ||
); | ||
} | ||
} |
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,80 @@ | ||
<?php // lint < 7.4 | ||
|
||
namespace PregMatchShapes; | ||
|
||
use Composer\Pcre\Preg; | ||
use Composer\Pcre\Regex; | ||
use function PHPStan\Testing\assertType; | ||
|
||
function (string $s): void { | ||
Preg::replaceCallback( | ||
$s, | ||
function ($matches) { | ||
assertType('array<int|string, string|null>', $matches); | ||
return ''; | ||
}, | ||
$s | ||
); | ||
|
||
Regex::replaceCallback( | ||
$s, | ||
function ($matches) { | ||
assertType('array<int|string, string|null>', $matches); | ||
return ''; | ||
}, | ||
$s | ||
); | ||
}; | ||
|
||
function (string $s): void { | ||
Preg::replaceCallback( | ||
'|<p>(\s*)\w|', | ||
function ($matches) { | ||
assertType('array{string, string}', $matches); | ||
return ''; | ||
}, | ||
$s | ||
); | ||
}; | ||
|
||
function (string $s): void { | ||
Preg::replaceCallback( | ||
'/(foo)?(bar)?(baz)?/', | ||
function ($matches) { | ||
assertType("array{0: string, 1?: ''|'foo', 2?: ''|'bar', 3?: 'baz'}", $matches); | ||
return ''; | ||
}, | ||
$s, | ||
-1, | ||
$count, | ||
PREG_UNMATCHED_AS_NULL | ||
); | ||
}; | ||
|
||
function (string $s): void { | ||
Preg::replaceCallback( | ||
'/(foo)?(bar)?(baz)?/', | ||
function ($matches) { | ||
assertType("array{0: array{string, int<-1, max>}, 1?: array{''|'foo', int<-1, max>}, 2?: array{''|'bar', int<-1, max>}, 3?: array{'baz', int<-1, max>}}", $matches); | ||
return ''; | ||
}, | ||
$s, | ||
-1, | ||
$count, | ||
PREG_OFFSET_CAPTURE | ||
); | ||
}; | ||
|
||
function (string $s): void { | ||
Preg::replaceCallback( | ||
'/(foo)?(bar)?(baz)?/', | ||
function ($matches) { | ||
assertType("array{0: array{string, int<-1, max>}, 1?: array{''|'foo', int<-1, max>}, 2?: array{''|'bar', int<-1, max>}, 3?: array{'baz', int<-1, max>}}", $matches); | ||
return ''; | ||
}, | ||
$s, | ||
-1, | ||
$count, | ||
PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL | ||
); | ||
}; |
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,80 @@ | ||
<?php // lint >= 7.4 | ||
|
||
namespace PregMatchShapes; | ||
|
||
use Composer\Pcre\Preg; | ||
use Composer\Pcre\Regex; | ||
use function PHPStan\Testing\assertType; | ||
|
||
function (string $s): void { | ||
Preg::replaceCallback( | ||
$s, | ||
function ($matches) { | ||
assertType('array<int|string, string|null>', $matches); | ||
return ''; | ||
}, | ||
$s | ||
); | ||
|
||
Regex::replaceCallback( | ||
$s, | ||
function ($matches) { | ||
assertType('array<int|string, string|null>', $matches); | ||
return ''; | ||
}, | ||
$s | ||
); | ||
}; | ||
|
||
function (string $s): void { | ||
Preg::replaceCallback( | ||
'|<p>(\s*)\w|', | ||
function ($matches) { | ||
assertType('array{string, string}', $matches); | ||
return ''; | ||
}, | ||
$s | ||
); | ||
}; | ||
|
||
function (string $s): void { | ||
Preg::replaceCallback( | ||
'/(foo)?(bar)?(baz)?/', | ||
function ($matches) { | ||
assertType("array{string, 'foo'|null, 'bar'|null, 'baz'|null}", $matches); | ||
return ''; | ||
}, | ||
$s, | ||
-1, | ||
$count, | ||
PREG_UNMATCHED_AS_NULL | ||
); | ||
}; | ||
|
||
function (string $s): void { | ||
Preg::replaceCallback( | ||
'/(foo)?(bar)?(baz)?/', | ||
function ($matches) { | ||
assertType("array{0: array{string, int<-1, max>}, 1?: array{''|'foo', int<-1, max>}, 2?: array{''|'bar', int<-1, max>}, 3?: array{'baz', int<-1, max>}}", $matches); | ||
return ''; | ||
}, | ||
$s, | ||
-1, | ||
$count, | ||
PREG_OFFSET_CAPTURE | ||
); | ||
}; | ||
|
||
function (string $s): void { | ||
Preg::replaceCallback( | ||
'/(foo)?(bar)?(baz)?/', | ||
function ($matches) { | ||
assertType("array{array{string|null, int<-1, max>}, array{'foo'|null, int<-1, max>}, array{'bar'|null, int<-1, max>}, array{'baz'|null, int<-1, max>}}", $matches); | ||
return ''; | ||
}, | ||
$s, | ||
-1, | ||
$count, | ||
PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL | ||
); | ||
}; |