Skip to content

Commit

Permalink
[Php83] Handle default native constant on AddTypeToConstRector (#5843)
Browse files Browse the repository at this point in the history
* [Php83] Handle default native constant on AddTypeToConstRector

* [ci-review] Rector Rectify

* fix

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Apr 23, 2024
1 parent 775916c commit d5069f0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

final class DefaultNativeConstant
{
public const FOO = PHP_INT_MAX;
}

?>
-----
<?php

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

final class DefaultNativeConstant
{
public const int FOO = PHP_INT_MAX;
}

?>
20 changes: 15 additions & 5 deletions rules/Php83/Rector/ClassConst/AddTypeToConstRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
use PhpParser\Node\Stmt\ClassConst;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -33,6 +35,7 @@ final class AddTypeToConstRector extends AbstractRector implements MinPhpVersion
{
public function __construct(
private readonly ReflectionProvider $reflectionProvider,
private readonly StaticTypeMapper $staticTypeMapper
) {
}

Expand Down Expand Up @@ -160,12 +163,19 @@ private function findValueType(Expr $expr): ?Identifier
return new Identifier('float');
}

if ($expr instanceof ConstFetch && $expr->name->toLowerString() !== 'null') {
return new Identifier('bool');
}
if ($expr instanceof ConstFetch) {
if ($expr->name->toLowerString() === 'null') {
return new Identifier('null');
}

$type = $this->nodeTypeResolver->getNativeType($expr);
$nodeType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type, TypeKind::PROPERTY);

if (! $nodeType instanceof Identifier) {
return null;
}

if ($expr instanceof ConstFetch && $expr->name->toLowerString() === 'null') {
return new Identifier('null');
return $nodeType;
}

if ($expr instanceof Array_) {
Expand Down

0 comments on commit d5069f0

Please sign in to comment.