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

Open
wants to merge 1 commit into
base: 1.12.x
Choose a base branch
from
Open
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 @@ -23,6 +23,7 @@
use function in_array;
use function is_bool;
use function mb_substr;
use function strlen;
use function substr;

final class SubstrDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
Expand Down Expand Up @@ -74,19 +75,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 @@ -127,4 +138,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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPStan\Testing\TypeInferenceTestCase;

class LooseConstComparisonPhp7Test extends TypeInferenceTestCase
class NodeScopeResolverPhp7Test extends TypeInferenceTestCase
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the neon file is only

parameters:
	phpVersion: 70400 # PHP 7.4

we could use those TestCase for all the configured-phpVersion-related tests instead of creating a TestCase every time.

{

/**
Expand All @@ -15,6 +15,8 @@ public function dataFileAsserts(): iterable
// compares constants according to the php-version phpstan configuration,
// _NOT_ the current php runtime version
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-const-comparison-php7.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-13129-php7.php');
}

/**
Expand All @@ -33,7 +35,7 @@ public function testFileAsserts(
public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/looseConstComparisonPhp7.neon',
__DIR__ . '/nodeScopeResolverPhp7.neon',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPStan\Testing\TypeInferenceTestCase;

class LooseConstComparisonPhp8Test extends TypeInferenceTestCase
class NodeScopeResolverPhp8Test extends TypeInferenceTestCase
{

/**
Expand All @@ -15,6 +15,8 @@ public function dataFileAsserts(): iterable
// compares constants according to the php-version phpstan configuration,
// _NOT_ the current php runtime version
yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-const-comparison-php8.php');

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-13129-php8.php');
}

/**
Expand All @@ -33,7 +35,7 @@ public function testFileAsserts(
public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/looseConstComparisonPhp8.neon',
__DIR__ . '/nodeScopeResolverPhp8.neon',
];
}

Expand Down
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can add the lint comment which makes the file beeing analyzed in the proper version

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lint comment is for the runtime PHP version no ? (Like it's done for a lot of NodeScopeResolverPHPTest).

Here I want to allow Analysing this file on both PHP 7 and PHP 8, but telling PHPStan to consider he's in PHP 7.


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