Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
7 changes: 6 additions & 1 deletion src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
use PHPStan\Type\VoidType;
use stdClass;
use Throwable;
use ValueError;
use function abs;
use function array_filter;
use function array_key_exists;
Expand Down Expand Up @@ -1750,7 +1751,11 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
&& !is_numeric($varValue)
&& function_exists('str_increment')
) {
$varValue = str_increment($varValue);
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels kinda weird because

  • On PHP 8.3 & 8.4 we return ErrorType while it's still valid code (even if deprecated)
  • On PHP 8.5+ we're still returning '1' for ''++ while it's invalid code

Should we introduce a method inside PHPVersion class ?

Then you could keep @++$varValue for PHPVersion <= 8.5

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was one of the variants I considered. the ++$varValue is consistent across all versions (it differs only by emitting a deprecation warning).

if we are fine with swalling the deprecation warning the impl could be simplified.

I think we wouldn't even need per version expectations

$varValue = str_increment($varValue);
} catch (ValueError) {
return new ErrorType();
}
} elseif (!is_bool($varValue)) {
++$varValue;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13481-before-php83.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php // lint < 8.3

namespace bug13481BeforePhp83;

use function PHPStan\Testing\assertType;

function bug13481() {
$s = 'ab c1';
assertType("*ERROR*", str_increment($s));

++$s;
assertType("'ab c2'", $s);
}

function bug13481b() {
$s = '%';
assertType("*ERROR*", str_increment($s));

++$s;
assertType("'%'", $s);
}
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13481-php83.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php // lint >= 8.3

namespace bug13481Php83;

use function PHPStan\Testing\assertType;
use function str_increment;

function bug13481() {
$s = 'ab c1';
assertType("*ERROR*", str_increment($s));

++$s;
assertType("*ERROR*", $s);
}

function bug13481b() {
$s = '%';
assertType("*ERROR*", str_increment($s));

++$s;
assertType("*ERROR*", $s);
}

Loading