[PHP 7.4] Add function deprecations

This commit is contained in:
Tomas Votruba 2018-12-31 01:10:56 +01:00
parent 6d68f0a3ee
commit 41b0a2601f
9 changed files with 100 additions and 9 deletions

View File

@ -1,3 +1,10 @@
services:
Rector\Php\Rector\Property\TypedPropertyRector: ~
Rector\Php\Rector\Assign\NullCoalescingOperatorRector: ~
Rector\Rector\Function_\FunctionReplaceRector:
# https://wiki.php.net/rfc/deprecations_php_7_4#the_real_type
is_float: 'is_real'
# https://wiki.php.net/rfc/deprecations_php_7_4#apache_request_headers_function
apache_request_headers: 'getallheaders'
hebrevc: ['nl2br', 'hebrev']

View File

@ -3,6 +3,7 @@
namespace Rector\Rector\Function_;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name\FullyQualified;
use Rector\Rector\AbstractRector;
@ -55,9 +56,37 @@ final class FunctionReplaceRector extends AbstractRector
continue;
}
// rename of function into wrap function
// e.g. one($arg) → three(two($agr));
if (is_array($newFunction)) {
return $this->wrapFuncCalls($node, $newFunction);
}
$node->name = new FullyQualified($newFunction);
}
return $node;
}
/**
* @param string[] $newFunctions
*/
private function wrapFuncCalls(FuncCall $funcCallNode, array $newFunctions): FuncCall
{
$previousNode = null;
$newFunctions = array_reverse($newFunctions);
foreach ($newFunctions as $wrapFunction) {
if ($previousNode === null) {
$arguments = $funcCallNode->args;
} else {
$arguments = [new Arg($previousNode)];
}
$funcCallNode = new FuncCall(new FullyQualified($wrapFunction), $arguments);
$previousNode = $funcCallNode;
}
return $funcCallNode;
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\Tests\Rector\Function_\FunctionReplaceRector\Fixture;
class DoubleFunction
{
public function run($text)
{
hebrevc($text);
}
}
?>
-----
<?php
namespace Rector\Tests\Rector\Function_\FunctionReplaceRector\Fixture;
class DoubleFunction
{
public function run($text)
{
\nl2br(\hebrev($text));
}
}
?>

View File

@ -1,5 +1,7 @@
<?php
namespace Rector\Tests\Rector\Function_\FunctionReplaceRector\Fixture;
class SomeClass
{
public function someMethod()
@ -16,6 +18,8 @@ function a(callable $a) {
-----
<?php
namespace Rector\Tests\Rector\Function_\FunctionReplaceRector\Fixture;
class SomeClass
{
public function someMethod()

View File

@ -2,7 +2,7 @@
namespace Rector\Tests\Rector\Function_\FunctionReplaceRector\Fixture;
class SomeClass
class CaseSensitive
{
protected function configure(): void
{
@ -22,7 +22,7 @@ class SomeClass
namespace Rector\Tests\Rector\Function_\FunctionReplaceRector\Fixture;
class SomeClass
class CaseSensitive
{
protected function configure(): void
{

View File

@ -9,7 +9,11 @@ final class FunctionReplaceRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/fixture2.php.inc']);
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc',
__DIR__ . '/Fixture/fixture2.php.inc',
__DIR__ . '/Fixture/double_function.php.inc',
]);
}
protected function getRectorClass(): string
@ -25,6 +29,7 @@ final class FunctionReplaceRectorTest extends AbstractRectorTestCase
return [
'view' => 'Laravel\Templating\render',
'sprintf' => 'Safe\sprintf',
'hebrevc' => ['nl2br', 'hebrev'],
];
}
}

View File

@ -1,6 +1,10 @@
<?php
$someService = new SomeClass;
namespace Rector\Tests\Rector\Property\PropertyNameReplacerRector\Fixture;
use Rector\Tests\Rector\Property\PropertyNameReplacerRector\Source\ClassWithProperties;
$someService = new ClassWithProperties();
$someService->oldProperty = 5;
$someService->anotherOLDProperty = 5;
@ -8,7 +12,11 @@ $someService->anotherOLDProperty = 5;
-----
<?php
$someService = new SomeClass;
namespace Rector\Tests\Rector\Property\PropertyNameReplacerRector\Fixture;
use Rector\Tests\Rector\Property\PropertyNameReplacerRector\Source\ClassWithProperties;
$someService = new ClassWithProperties();
$someService->newProperty = 5;
$someService->anotherNewProperty = 5;

View File

@ -4,6 +4,7 @@ namespace Rector\Tests\Rector\Property\PropertyNameReplacerRector;
use Rector\Rector\Property\PropertyNameReplacerRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Tests\Rector\Property\PropertyNameReplacerRector\Source\ClassWithProperties;
final class PropertyNameReplacerRectorTest extends AbstractRectorTestCase
{
@ -22,9 +23,11 @@ final class PropertyNameReplacerRectorTest extends AbstractRectorTestCase
*/
protected function getRectorConfiguration(): array
{
return ['SomeClass' => [
'oldProperty' => 'newProperty',
'anotherOldProperty' => 'anotherNewProperty',
]];
return [
ClassWithProperties::class => [
'oldProperty' => 'newProperty',
'anotherOldProperty' => 'anotherNewProperty',
],
];
}
}

View File

@ -0,0 +1,8 @@
<?php declare(strict_types=1);
namespace Rector\Tests\Rector\Property\PropertyNameReplacerRector\Source;
final class ClassWithProperties
{
}