Skip to content

Commit

Permalink
[PHP 7.4] Fix literator separator string/int missmatch in PHPStan type (
Browse files Browse the repository at this point in the history
#2913)

* add fixture failing scope

* fix printing of raw value and literal separator
  • Loading branch information
TomasVotruba committed Sep 5, 2022
1 parent f230e54 commit a638a67
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
\PhpParser\Node::getAttribute(),
0,
\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE,
\Rector\NodeTypeResolver\Node\AttributeKey::REPRINT_RAW_VALUE,
\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::NEXT_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::PREVIOUS_NODE,
Expand All @@ -67,6 +68,7 @@
\PhpParser\Node::setAttribute(),
0,
\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE,
\Rector\NodeTypeResolver\Node\AttributeKey::REPRINT_RAW_VALUE,
\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::NEXT_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::PREVIOUS_NODE,
Expand Down
6 changes: 6 additions & 0 deletions packages/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,10 @@ final class AttributeKey
* @var string
*/
public const PHP_ATTRIBUTE_NAME = 'php_attribute_name';

/**
* Helper attribute to reprint raw value of int/float/string
* @var string
*/
public const REPRINT_RAW_VALUE = 'reprint_raw_value';
}
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,9 @@ parameters:
message: '#Instead of "DateTime" class/interface use "Nette\\Utils\\DateTime"#'
path: src/Application/VersionResolver.php

# complex multiprinter
-
message: '#Class cognitive complexity is 55, keep it under 50#'
message: '#Class cognitive complexity is \d+, keep it under 50#'
path: src/PhpParser/Printer/BetterStandardPrinter.php #41

# validate class-string input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Rector\Tests\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector\Fixture;

class Fixture
final class BigFloats
{
public function run()
{
$int = 1000;
$int2 = 1000000;
$float = 1000.0;
$float2 = 1000000.0;
$float3 = 1000500.001;
Expand All @@ -20,12 +18,10 @@ class Fixture

namespace Rector\Tests\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector\Fixture;

class Fixture
final class BigFloats
{
public function run()
{
$int = 1000;
$int2 = 1_000_000;
$float = 1000.0;
$float2 = 1_000_000.0;
$float3 = 1_000_500.001;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector\Fixture;

final class BigIntegers
{
public function run()
{
$int2 = 1000000;
}
}

?>
-----
<?php

namespace Rector\Tests\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector\Fixture;

final class BigIntegers
{
public function run()
{
$int2 = 1_000_000;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php74\Rector\LNumber\AddLiteralSeparatorToNumberRector\Fixture;

final class SkipLowValue
{
public function run()
{
$int = 1000;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ public function refactor(Node $node): ?Node
}
}

$node->value = $literalSeparatedNumber;
// this cannot be integer directly to $node->value, as PHPStan sees it as error type
// @see https://github.com/rectorphp/rector/issues/7454
$node->setAttribute(AttributeKey::RAW_VALUE, $literalSeparatedNumber);
$node->setAttribute(AttributeKey::REPRINT_RAW_VALUE, true);
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);

return $node;
}
Expand Down
23 changes: 21 additions & 2 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\EncapsedStringPart;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
Expand Down Expand Up @@ -252,8 +253,8 @@ protected function pSingleQuotedString(string $string): string
*/
protected function pScalar_DNumber(DNumber $dNumber): string
{
if (is_string($dNumber->value)) {
return $dNumber->value;
if ($this->shouldPrintNewRawValue($dNumber)) {
return (string) $dNumber->getAttribute(AttributeKey::RAW_VALUE);
}

return parent::pScalar_DNumber($dNumber);
Expand Down Expand Up @@ -469,6 +470,24 @@ protected function pModifiers(int $modifiers): string
. (($modifiers & Class_::MODIFIER_READONLY) !== 0 ? 'readonly ' : '');
}

/**
* Invoke re-print even if only raw value was changed.
* That allows PHPStan to use int strict types, while changing the value with literal "_"
*/
protected function pScalar_LNumber(LNumber $lNumber): string|int
{
if ($this->shouldPrintNewRawValue($lNumber)) {
return (string) $lNumber->getAttribute(AttributeKey::RAW_VALUE);
}

return parent::pScalar_LNumber($lNumber);
}

private function shouldPrintNewRawValue(LNumber|DNumber $lNumber): bool
{
return $lNumber->getAttribute(AttributeKey::REPRINT_RAW_VALUE) === true;
}

private function resolveContentOnExpr(Expr $expr, string $content): string
{
$parentNode = $expr->getAttribute(AttributeKey::PARENT_NODE);
Expand Down

0 comments on commit a638a67

Please sign in to comment.