Skip to content
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

[Php83] Add support +/- value on constant on AddTypeToConstRector #5694

Merged
merged 1 commit into from
Mar 5, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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