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
21 changes: 12 additions & 9 deletions src/Type/Php/StrPadFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
Expand Down Expand Up @@ -44,15 +45,17 @@ public function getTypeFromFunctionCall(
$accessoryTypes[] = new AccessoryNonEmptyStringType();
}

if ($inputType->isLiteralString()->yes()) {
if (count($args) < 3) {
$accessoryTypes[] = new AccessoryLiteralStringType();
} else {
$padStringType = $scope->getType($args[2]->value);
if ($padStringType->isLiteralString()->yes()) {
$accessoryTypes[] = new AccessoryLiteralStringType();
}
}
if (count($args) < 3) {
$padStringType = null;
} else {
$padStringType = $scope->getType($args[2]->value);
}

if ($inputType->isLiteralString()->yes() && ($padStringType === null || $padStringType->isLiteralString()->yes())) {
$accessoryTypes[] = new AccessoryLiteralStringType();
}
if ($inputType->isLowercaseString()->yes() && ($padStringType === null || $padStringType->isLowercaseString()->yes())) {
$accessoryTypes[] = new AccessoryLowercaseStringType();
}

if (count($accessoryTypes) > 0) {
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Php/StrRepeatFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Accessory\AccessoryLiteralStringType;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
Expand Down Expand Up @@ -93,6 +94,10 @@ public function getTypeFromFunctionCall(
}
}

if ($inputType->isLowercaseString()->yes()) {
$accessoryTypes[] = new AccessoryLowercaseStringType();
}

if (count($accessoryTypes) > 0) {
$accessoryTypes[] = new StringType();
return new IntersectionType($accessoryTypes);
Expand Down
16 changes: 8 additions & 8 deletions tests/PHPStan/Analyser/nsrt/literal-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function doFoo($literalString, string $string, $numericString)
"'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'",
str_repeat('a', 99)
);
assertType('literal-string&non-falsy-string', str_repeat('a', 100));
assertType('literal-string&non-empty-string&numeric-string', str_repeat('0', 100)); // could be non-falsy-string
assertType('literal-string&non-falsy-string&numeric-string', str_repeat('1', 100));
assertType('literal-string&lowercase-string&non-falsy-string', str_repeat('a', 100));
assertType('literal-string&lowercase-string&non-empty-string&numeric-string', str_repeat('0', 100)); // could be non-falsy-string
assertType('literal-string&lowercase-string&non-falsy-string&numeric-string', str_repeat('1', 100));
// Repeating a numeric type multiple times can lead to a non-numeric type: 3v4l.org/aRBdZ
assertType('non-empty-string', str_repeat($numericString, 100));

Expand All @@ -51,13 +51,13 @@ public function doFoo($literalString, string $string, $numericString)
assertType("non-empty-string", str_repeat($numericString, 2));
assertType("literal-string", str_repeat($literalString, 1));
$x = rand(1,2);
assertType("literal-string&non-falsy-string", str_repeat(' 1 ', $x));
assertType("literal-string&non-falsy-string", str_repeat('+1', $x));
assertType("literal-string&non-falsy-string", str_repeat('1e9', $x));
assertType("literal-string&non-falsy-string&numeric-string", str_repeat('19', $x));
assertType("literal-string&lowercase-string&non-falsy-string", str_repeat(' 1 ', $x));
assertType("literal-string&lowercase-string&non-falsy-string", str_repeat('+1', $x));
assertType("literal-string&lowercase-string&non-falsy-string", str_repeat('1e9', $x));
assertType("literal-string&lowercase-string&non-falsy-string&numeric-string", str_repeat('19', $x));

$x = rand(0,2);
assertType("literal-string", str_repeat('19', $x));
assertType("literal-string&lowercase-string", str_repeat('19', $x));

$x = rand(-10,-1);
assertType("*NEVER*", str_repeat('19', $x));
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/nsrt/lowercase-string-pad.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace LowercaseStringStrPad;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @param lowercase-string $lowercase
*/
public function doRepeat(string $string, string $lowercase): void
{
assertType('non-empty-string', str_pad($string, 5));
assertType('non-empty-string', str_pad($string, 5, $lowercase));
assertType('non-empty-string', str_pad($string, 5, $string));
assertType('lowercase-string&non-empty-string', str_pad($lowercase, 5));
assertType('lowercase-string&non-empty-string', str_pad($lowercase, 5, $lowercase));
assertType('non-empty-string', str_pad($lowercase, 5, $string));
}

}
19 changes: 19 additions & 0 deletions tests/PHPStan/Analyser/nsrt/lowercase-string-repeat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace LowercaseStringStrRepeat;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @param lowercase-string $lowercase
*/
public function doRepeat(string $string, string $lowercase): void
{
assertType('lowercase-string', str_repeat($lowercase, 5));
assertType('string', str_repeat($string, 5));
}

}
Loading