Skip to content

Commit

Permalink
widen the scope to str_starts_with, str_ends_with
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab committed Mar 13, 2022
1 parent 91fd591 commit 46a7c35
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ services:
- phpstan.dynamicStaticMethodThrowTypeExtension

-
class: PHPStan\Type\Php\StrContainsTypeSpecifyingExtension
class: PHPStan\Type\Php\StrContainingTypeSpecifyingExtension
tags:
- phpstan.typeSpecifier.functionTypeSpecifyingExtension

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StringType;
use function count;
use function in_array;
use function strtolower;

final class StrContainsTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
final class StrContainingTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension
{

/** @var string[] */
private array $strContainingFunctions = [
'str_contains',
'str_starts_with',
'str_ends_with',
];

private TypeSpecifier $typeSpecifier;

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
Expand All @@ -30,7 +38,7 @@ public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void

public function isFunctionSupported(FunctionReflection $functionReflection, FuncCall $node, TypeSpecifierContext $context): bool
{
return strtolower($functionReflection->getName()) === 'str_contains'
return in_array(strtolower($functionReflection->getName()), $this->strContainingFunctions, true)
&& $context->true();
}

Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/data/non-empty-string-str-contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ public function sayHello(string $s, string $s2, $nonES, $numS, $literalS, $nonEA
assertType('int', $i);
}
}

public function variants(string $s) {
if (str_starts_with($s, ':')) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (str_ends_with($s, ':')) {
assertType('non-empty-string', $s);
}
assertType('string', $s);
}
}

0 comments on commit 46a7c35

Please sign in to comment.