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
10 changes: 10 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,13 @@ services:
class: PHPStan\Type\Nette\StringsReplaceCallbackClosureTypeExtension
tags:
- phpstan.staticMethodParameterClosureTypeExtension

-
class: PHPStan\Type\Nette\StringsLengthTypeSpecifiyingExtension
tags:
- phpstan.typeSpecifier.staticMethodTypeSpecifyingExtension

-
class: PHPStan\Type\Nette\StringsLengthDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
43 changes: 43 additions & 0 deletions src/Type/Nette/StringsLengthDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Nette;

use Nette\Utils\Strings;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\Type;

class StringsLengthDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
{

public function getClass(): string
{
return Strings::class;
}

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'length';
}

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
{
$args = $methodCall->getArgs();
$stringArg = $args[0] ?? null;

if ($stringArg === null) {
return null;
}

$type = $scope->getType($stringArg->value);
if ($type->isNonEmptyString()->yes()) {
return IntegerRangeType::fromInterval(1, null);
}

return null;
}

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

namespace PHPStan\Type\Nette;

use Nette\Utils\Strings;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\StaticMethodTypeSpecifyingExtension;
use PHPStan\Type\StringType;

class StringsLengthTypeSpecifiyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
{

private TypeSpecifier $typeSpecifier;

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}

public function getClass(): string
{
return Strings::class;
}

public function isStaticMethodSupported(MethodReflection $staticMethodReflection, StaticCall $node, TypeSpecifierContext $context): bool
{
return $context->true() && $staticMethodReflection->getName() === 'length';
}

public function specifyTypes(MethodReflection $staticMethodReflection, StaticCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$args = $node->getArgs();
$stringArg = $args[0] ?? null;

if ($stringArg === null) {
return new SpecifiedTypes();
}

$type = $scope->getType($stringArg->value);
if (!$type->isString()->yes()) {
return new SpecifiedTypes();
}

return $this->typeSpecifier->create(
$stringArg->value,
new IntersectionType([new StringType(), new AccessoryNonEmptyStringType()]),
$context,
$scope,
);
}

}
21 changes: 21 additions & 0 deletions strings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace StringsTypesNarrowing;

use Nette\Utils\Strings;
use function PHPStan\Testing\assertType;

function doFoo(string $string) {
assertType('string', $string);
if (Strings::length($string)) {
assertType('non-empty-string', $string);
} else {
assertType('string', $string);
}
assertType('string', $string);

if (Strings::length($string) === 0) {
assertType('string', $string);
}
assertType('string', $string);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use function class_exists;
use function version_compare;

class MultiplierTest extends TypeInferenceTestCase
class TypeInferenceTest extends TypeInferenceTestCase
{

public function dataFileAsserts(): iterable
Expand All @@ -26,6 +26,8 @@ public function dataFileAsserts(): iterable
} else {
yield from self::gatherAssertTypes(__DIR__ . '/data/multiplier.php');
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/strings-length.php');
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/Type/Nette/data/strings-length.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace StringsTypesNarrowing;

use Nette\Utils\Strings;
use function PHPStan\Testing\assertType;

function doFoo(string $string) {
assertType('string', $string);
if (Strings::length($string)) {
assertType('non-empty-string', $string);
assertType('int<1, max>', Strings::length($string));
} else {
assertType('string', $string);
assertType('0', Strings::length($string));
}
assertType('string', $string);
assertType('int', Strings::length($string));

if (Strings::length($string) === 0) {
assertType('string', $string);
}
assertType('string', $string);
}

/**
* @param non-empty-string $nonES
*/
function doBar(string $nonES) {
assertType('int<1, max>', Strings::length($nonES));
}
Loading