Skip to content

Commit a7d255d

Browse files
committed
matchesAll
1 parent 26ec2df commit a7d255d

4 files changed

Lines changed: 36 additions & 14 deletions

File tree

extension.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ services:
128128
tags:
129129
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
130130

131+
-
132+
class: PHPStan\Type\Nette\StringsMatchTypeSpecifiyingExtension
133+
tags:
134+
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension
135+
131136
-
132137
class: PHPStan\Type\Nette\StringsReplaceCallbackClosureTypeExtension
133138
tags:

src/Type/Nette/StringsMatchTypeSpecifiyingExtension.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,20 @@
33
namespace PHPStan\Type\Nette;
44

55
use Nette\Utils\Strings;
6-
use PhpParser\Node\Arg;
76
use PhpParser\Node\Expr\StaticCall;
87
use PHPStan\Analyser\Scope;
98
use PHPStan\Analyser\SpecifiedTypes;
109
use PHPStan\Analyser\TypeSpecifier;
10+
use PHPStan\Analyser\TypeSpecifierAwareExtension;
1111
use PHPStan\Analyser\TypeSpecifierContext;
1212
use PHPStan\Reflection\MethodReflection;
13-
use PHPStan\TrinaryLogic;
14-
use PHPStan\Type\Constant\ConstantBooleanType;
15-
use PHPStan\Type\Constant\ConstantIntegerType;
16-
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
17-
use PHPStan\Type\NullType;
1813
use PHPStan\Type\Php\RegexArrayShapeMatcher;
1914
use PHPStan\Type\StaticMethodTypeSpecifyingExtension;
20-
use PHPStan\Type\Type;
21-
use PHPStan\Type\TypeCombinator;
22-
use function array_key_exists;
23-
use const PREG_OFFSET_CAPTURE;
24-
use const PREG_UNMATCHED_AS_NULL;
15+
use function in_array;
2516

26-
class StringsMatchTypeSpecifiyingExtension implements StaticMethodTypeSpecifyingExtension
17+
class StringsMatchTypeSpecifiyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
2718
{
19+
2820
private RegexArrayShapeMatcher $regexArrayShapeMatcher;
2921

3022
private TypeSpecifier $typeSpecifier;
@@ -46,7 +38,7 @@ public function getClass(): string
4638

4739
public function isStaticMethodSupported(MethodReflection $staticMethodReflection, StaticCall $node, TypeSpecifierContext $context): bool
4840
{
49-
return $context->true() && $staticMethodReflection->getName() === 'match';
41+
return $context->true() && in_array($staticMethodReflection->getName(), ['match', 'matchAll'], true);
5042
}
5143

5244
public function specifyTypes(MethodReflection $staticMethodReflection, StaticCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
@@ -78,4 +70,5 @@ public function specifyTypes(MethodReflection $staticMethodReflection, StaticCal
7870

7971
return $subjectTypes;
8072
}
73+
8174
}

tests/Type/Nette/StringsMatchDynamicReturnTypeExtensionTest.php renamed to tests/Type/Nette/StringsMatchTypeInferenceExtensionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use PHPStan\Testing\TypeInferenceTestCase;
66

7-
class StringsMatchDynamicReturnTypeExtensionTest extends TypeInferenceTestCase
7+
class StringsMatchTypeInferenceExtensionTest extends TypeInferenceTestCase
88
{
99

1010
public function dataFileAsserts(): iterable
1111
{
1212
yield from self::gatherAssertTypes(__DIR__ . '/data/strings-match.php');
1313
yield from self::gatherAssertTypes(__DIR__ . '/data/strings-match-74.php');
14+
yield from self::gatherAssertTypes(__DIR__ . '/data/strings-match-subject.php');
1415
}
1516

1617
/**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace StringsMatchSubject;
4+
5+
use Nette\Utils\Strings;
6+
use function PHPStan\Testing\assertType;
7+
8+
function (string $s): void {
9+
if (Strings::match($s, '/foo/')) {
10+
assertType("non-falsy-string", $s);
11+
} else {
12+
assertType("string", $s);
13+
}
14+
assertType("string", $s);
15+
16+
$matches = Strings::matchAll($s, '/foo/');
17+
if (count($matches) !== 0) {
18+
assertType("non-falsy-string", $s);
19+
} else {
20+
assertType("string", $s);
21+
}
22+
assertType("string", $s);
23+
};

0 commit comments

Comments
 (0)