Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ conditionalTags:
phpstan.typeSpecifier.functionTypeSpecifyingExtension: %featureToggles.narrowPregMatches%
PHPStan\Type\Php\PregMatchParameterOutTypeExtension:
phpstan.functionParameterOutTypeExtension: %featureToggles.narrowPregMatches%
PHPStan\Type\Php\PregReplaceCallbackClosureTypeExtension:
phpstan.functionParameterClosureTypeExtension: %featureToggles.narrowPregMatches%

services:
-
Expand Down Expand Up @@ -1497,6 +1499,9 @@ services:
-
class: PHPStan\Type\Php\PregMatchParameterOutTypeExtension

-
class: PHPStan\Type\Php\PregReplaceCallbackClosureTypeExtension

-
class: PHPStan\Type\Php\RegexArrayShapeMatcher

Expand Down
60 changes: 60 additions & 0 deletions src/Type/Php/PregReplaceCallbackClosureTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ClosureType;
use PHPStan\Type\FunctionParameterClosureTypeExtension;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;

final class PregReplaceCallbackClosureTypeExtension implements FunctionParameterClosureTypeExtension
{

public function __construct(
private RegexArrayShapeMatcher $regexShapeMatcher,
)
{
}

public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool
{
return $functionReflection->getName() === 'preg_replace_callback' && $parameter->getName() === 'callback';
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, ParameterReflection $parameter, Scope $scope): ?Type
{
$args = $functionCall->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(),
);
}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/Analyser/nsrt/preg_replace_callback_shapes-php72.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace PregReplaceCallbackMatchShapes72;

use function PHPStan\Testing\assertType;

function (string $s): void {
preg_replace_callback(
$s,
function ($matches) {
assertType('array<int|string, string>', $matches);
return '';
},
$s
);
};

function (string $s): void {
preg_replace_callback(
'|<p>(\s*)\w|',
function ($matches) {
assertType('array{string, string}', $matches);
return '';
},
$s
);
};

// The flags parameter was added in PHP 7.4
47 changes: 47 additions & 0 deletions tests/PHPStan/Analyser/nsrt/preg_replace_callback_shapes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php // lint >= 7.4

namespace PregReplaceCallbackMatchShapes;

use function PHPStan\Testing\assertType;

function (string $s): void {
preg_replace_callback(
'/(foo)?(bar)?(baz)?/',
function ($matches) {
assertType('array{string, non-empty-string|null, non-empty-string|null, non-empty-string|null}', $matches);
return '';
},
$s,
-1,
$count,
PREG_UNMATCHED_AS_NULL
);
};

function (string $s): void {
preg_replace_callback(
'/(foo)?(bar)?(baz)?/',
function ($matches) {
assertType('array{0: array{string, int<0, max>}, 1?: array{non-empty-string, int<0, max>}, 2?: array{non-empty-string, int<0, max>}, 3?: array{non-empty-string, int<0, max>}}', $matches);
return '';
},
$s,
-1,
$count,
PREG_OFFSET_CAPTURE
);
};

function (string $s): void {
preg_replace_callback(
'/(foo)?(bar)?(baz)?/',
function ($matches) {
assertType('array{array{string|null, int<-1, max>}, array{non-empty-string|null, int<-1, max>}, array{non-empty-string|null, int<-1, max>}, array{non-empty-string|null, int<-1, max>}}', $matches);
return '';
},
$s,
-1,
$count,
PREG_OFFSET_CAPTURE|PREG_UNMATCHED_AS_NULL
);
};
Loading