Skip to content

Commit

Permalink
[Php83] Add support +/- value on constant on AddTypeToConstRector (#5694
Browse files Browse the repository at this point in the history
)
  • Loading branch information
samsonasik committed Mar 5, 2024
1 parent 10c7bc6 commit 84639e6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture;

final class PlusOrMinus
{
public const MINUS_1 = -1;

public const PLUS_1 = +1;
}

?>
-----
<?php

namespace Rector\Tests\Php83\Rector\ClassConst\AddTypeToConstRector\Fixture;

final class PlusOrMinus
{
public const int MINUS_1 = -1;

public const int PLUS_1 = +1;
}

?>
6 changes: 6 additions & 0 deletions rules/Php83/Rector/ClassConst/AddTypeToConstRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node\Expr\UnaryPlus;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
Expand Down Expand Up @@ -142,6 +144,10 @@ public function isConstGuardedByParents(Const_ $const, array $parentClassReflect

private function findValueType(Expr $expr): ?Identifier
{
if ($expr instanceof UnaryPlus || $expr instanceof UnaryMinus) {
return $this->findValueType($expr->expr);
}

if ($expr instanceof String_) {
return new Identifier('string');
}
Expand Down

0 comments on commit 84639e6

Please sign in to comment.