Skip to content

Resolve substr based on configured PHP version #4061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2025
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
5 changes: 0 additions & 5 deletions build/baseline-8.0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,3 @@ parameters:
message: "#^Strict comparison using \\=\\=\\= between list<string> and false will always evaluate to false\\.$#"
count: 1
path: ../src/Type/Php/StrSplitFunctionReturnTypeExtension.php

-
message: "#^Call to function is_bool\\(\\) with string will always evaluate to false\\.$#"
count: 1
path: ../src/Type/Php/SubstrDynamicReturnTypeExtension.php
37 changes: 36 additions & 1 deletion src/Type/Php/SubstrDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function in_array;
use function is_bool;
use function mb_substr;
use function strlen;
use function substr;

#[AutowiredService]
Expand Down Expand Up @@ -76,19 +77,29 @@ public function getTypeFromFunctionCall(
if ($length !== null) {
if ($functionReflection->getName() === 'mb_substr') {
$substr = mb_substr($constantString->getValue(), $offset->getValue(), $length->getValue());
} elseif ($this->phpVersion->substrReturnFalseInsteadOfEmptyString()) {
$substr = $this->substrOrFalse($constantString->getValue(), $offset->getValue(), $length->getValue());
} else {
$substr = substr($constantString->getValue(), $offset->getValue(), $length->getValue());
}
} else {
if ($functionReflection->getName() === 'mb_substr') {
$substr = mb_substr($constantString->getValue(), $offset->getValue());
} elseif ($this->phpVersion->substrReturnFalseInsteadOfEmptyString()) {
// Simulate substr call on an older PHP version if the runtime one is too new.
$substr = $this->substrOrFalse($constantString->getValue(), $offset->getValue());
} else {
$substr = substr($constantString->getValue(), $offset->getValue());
}
}

if (is_bool($substr)) {
$results[] = new ConstantBooleanType($substr);
if ($this->phpVersion->substrReturnFalseInsteadOfEmptyString()) {
$results[] = new ConstantBooleanType($substr);
} else {
// Simulate substr call on a recent PHP version if the runtime one is too old.
$results[] = new ConstantStringType('');
}
} else {
$results[] = new ConstantStringType($substr);
}
Expand Down Expand Up @@ -129,4 +140,28 @@ public function getTypeFromFunctionCall(
return null;
}

private function substrOrFalse(string $string, int $offset, ?int $length = null): false|string
{
$strlen = strlen($string);

if ($offset > $strlen) {
return false;
}

if ($length !== null && $length < 0) {
if ($offset < 0 && -$length > $strlen) {
return false;
}
if ($offset >= 0 && -$length > $strlen - $offset) {
return false;
}
}

if ($length === null) {
return substr($string, $offset);
}

return substr($string, $offset, $length);
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/LooseConstComparisonPhp7Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testFileAsserts(
public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/looseConstComparisonPhp7.neon',
__DIR__ . '/nodeScopeResolverPhp7.neon',
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/LooseConstComparisonPhp8Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testFileAsserts(
public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/looseConstComparisonPhp8.neon',
__DIR__ . '/nodeScopeResolverPhp8.neon',
];
}

Expand Down
39 changes: 39 additions & 0 deletions tests/PHPStan/Analyser/SubstrPhp7Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Testing\TypeInferenceTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class SubstrPhp7Test extends TypeInferenceTestCase
{

/**
* @return iterable<array<string, mixed[]>>
*/
public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-13129-php7.php');
}

/**
* @param mixed ...$args
*/
#[DataProvider('dataFileAsserts')]
public function testFileAsserts(
string $assertType,
string $file,
...$args,
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/nodeScopeResolverPhp7.neon',
];
}

}
39 changes: 39 additions & 0 deletions tests/PHPStan/Analyser/SubstrPhp8Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types = 1);

namespace PHPStan\Analyser;

use PHPStan\Testing\TypeInferenceTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

class SubstrPhp8Test extends TypeInferenceTestCase
{

/**
* @return iterable<array<string, mixed[]>>
*/
public static function dataFileAsserts(): iterable
{
yield from self::gatherAssertTypes(__DIR__ . '/data/bug-13129-php8.php');
}

/**
* @param mixed ...$args
*/
#[DataProvider('dataFileAsserts')]
public function testFileAsserts(
string $assertType,
string $file,
...$args,
): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/nodeScopeResolverPhp8.neon',
];
}

}
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/bug-13129-php7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Bug13129PHP7;

use function PHPStan\Testing\assertType;

assertType("false", substr("test", 5));
assertType("false", substr("test", -1, -5));
assertType("false", substr("test", 1, -4));
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/data/bug-13129-php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Bug13129PHP8;

use function PHPStan\Testing\assertType;

assertType("''", substr("test", 5));
assertType("''", substr("test", -1, -5));
assertType("''", substr("test", 1, -4));
Loading