Skip to content
Closed
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
8 changes: 8 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,11 @@ services:
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\PregGrepDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\ReflectionClassIsSubclassOfTypeSpecifyingExtension
tags:
Expand Down Expand Up @@ -1724,6 +1729,9 @@ services:
-
class: PHPStan\Type\Constant\OversizedArrayBuilder

-
class: PHPStan\Rules\Regexp\PatternValidator

exceptionTypeResolver:
class: PHPStan\Rules\Exceptions\ExceptionTypeResolver
factory: @PHPStan\Rules\Exceptions\DefaultExceptionTypeResolver
Expand Down
22 changes: 22 additions & 0 deletions src/Rules/Regexp/PatternValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Regexp;

use Nette\Utils\RegexpException;
use Nette\Utils\Strings;

final class PatternValidator
{

public function validatePattern(string $pattern): ?string
{
try {
Strings::match('', $pattern);
} catch (RegexpException $e) {
return $e->getMessage();
}

return null;
}

}
57 changes: 57 additions & 0 deletions src/Type/Php/PregGrepDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\ParametersAcceptorSelector;
use PHPStan\Rules\Regexp\PatternValidator;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;

class PregGrepDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function __construct(private PatternValidator $patternValidator)
{
}

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'preg_grep';
}

public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): ?Type
{
$args = $functionCall->getArgs();
if (count($args) < 2) {
return null;
}

$patternType = $scope->getType($args[0]->value);
$constantStrings = $patternType->getConstantStrings();
if (count($constantStrings) === 0) {
return null;
}

foreach ($constantStrings as $constantString) {
if ($this->patternValidator->validatePattern($constantString->getValue()) !== null) {
return null;
}
}

return TypeCombinator::remove(
ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(),
new ConstantBooleanType(false),
);
}

}
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9293.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/nullsafe-vs-scalar.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8517.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/preg_grep.php');
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Analyser/data/preg_grep.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace PregGrep;

use function PHPStan\Testing\assertType;

function doFoo(array $arr, $mixed, $flags) {
assertType('array|false', preg_grep($mixed, $arr));
assertType('array|false', preg_grep($mixed, $arr, $flags));
assertType('array', preg_grep('/^[0-9]+$/', $arr));
assertType('array', preg_grep('/^[0-9]+$/', $arr, $flags));

// invalid pattern
assertType('array|false', preg_grep('/((/', $arr));
}