Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
2 changes: 2 additions & 0 deletions config/set/php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector;
use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector;
use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
use Rector\Php85\Rector\Double\DoubleToFloatCastRector;
use Rector\Php85\Rector\FuncCall\ArrayKeyExistsNullToEmptyStringRector;
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
Expand All @@ -34,6 +35,7 @@
DeprecatedAnnotationToDeprecatedAttributeRector::class,
ColonAfterSwitchCaseRector::class,
ArrayKeyExistsNullToEmptyStringRector::class,
DoubleToFloatCastRector::class,
]
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Double\DoubleToFloatCastRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DoubleToFloatCastRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$var = 4;
$tmp = (double)$var;
?>
-----
<?php

$var = 4;
$tmp = (float) $var;
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php85\Rector\Double\DoubleToFloatCastRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DoubleToFloatCastRector::class);

$rectorConfig->phpVersion(PhpVersion::PHP_85);
};
65 changes: 65 additions & 0 deletions rules/Php85/Rector/Double/DoubleToFloatCastRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Rector\Php85\Rector\Double;

use PhpParser\Node;
use PhpParser\Node\Expr\Cast\Double;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;

/**
* @see https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_non-standard_cast_names
* @see \Rector\Tests\Php85\Rector\Double\DoubleToFloatCastRector\DoubleToFloatCastRectorTest
*/
final class DoubleToFloatCastRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace deprecated (double) cast with (float)',
[
new CodeSample(
<<<'CODE_SAMPLE'
$tmp = (double) $var;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$tmp = (float) $var;
CODE_SAMPLE
),
]
);
}

public function getNodeTypes(): array
{
return [Double::class];
}

/**
* @param Double $node
*/
public function refactor(Node $node): ?Node
{
$kind = $node->getAttribute(AttributeKey::KIND);
if ($kind !== Double::KIND_DOUBLE) {
return null;
}

$node->setAttribute(AttributeKey::KIND, Double::KIND_FLOAT);
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null);

return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::DEPRECATE_DOUBLE_CAST;
}
}
3 changes: 3 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -798,4 +798,7 @@ final class PhpVersionFeature
* @var int
*/
public const DEPRECATE_NULL_ARG_IN_ARRAY_KEY_EXISTS_FUNCTION = PhpVersion::PHP_85;

public const DEPRECATE_DOUBLE_CAST = PhpVersion::PHP_85;

}
Loading