mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 08:20:53 +00:00
[CodingStyle] Remove PreslashSimpleFunctionRector, let php-cs-fixer doing good work handle that (#1310)
This commit is contained in:
parent
2215cb23ec
commit
dc07c1f9a8
|
@ -1,4 +1,4 @@
|
|||
# 501 Rules Overview
|
||||
# 499 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -12,7 +12,7 @@
|
|||
|
||||
- [CodeQuality](#codequality) (69)
|
||||
|
||||
- [CodingStyle](#codingstyle) (38)
|
||||
- [CodingStyle](#codingstyle) (37)
|
||||
|
||||
- [Compatibility](#compatibility) (1)
|
||||
|
||||
|
@ -20,7 +20,7 @@
|
|||
|
||||
- [DeadCode](#deadcode) (50)
|
||||
|
||||
- [DependencyInjection](#dependencyinjection) (3)
|
||||
- [DependencyInjection](#dependencyinjection) (2)
|
||||
|
||||
- [DowngradePhp53](#downgradephp53) (1)
|
||||
|
||||
|
@ -2317,25 +2317,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
|
||||
<br>
|
||||
|
||||
### PreslashSimpleFunctionRector
|
||||
|
||||
Add pre-slash to short named functions to improve performance
|
||||
|
||||
- class: [`Rector\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector`](../rules/CodingStyle/Rector/FuncCall/PreslashSimpleFunctionRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function shorten($value)
|
||||
{
|
||||
- return trim($value);
|
||||
+ return \trim($value);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### RemoveDoubleUnderscoreInMethodNameRector
|
||||
|
||||
Non-magic PHP object methods cannot start with "__"
|
||||
|
@ -4001,18 +3982,14 @@ Turns action injection in Controllers to constructor injection
|
|||
final class SomeController
|
||||
{
|
||||
- public function default(ProductRepository $productRepository)
|
||||
+ /**
|
||||
+ * @var ProductRepository
|
||||
+ */
|
||||
+ private $productRepository;
|
||||
+ public function __construct(ProductRepository $productRepository)
|
||||
{
|
||||
- $products = $productRepository->fetchAll();
|
||||
+ $this->productRepository = $productRepository;
|
||||
+ public function __construct(
|
||||
+ private ProductRepository $productRepository
|
||||
+ ) {
|
||||
+ }
|
||||
+
|
||||
+ public function default()
|
||||
+ {
|
||||
{
|
||||
- $products = $productRepository->fetchAll();
|
||||
+ $products = $this->productRepository->fetchAll();
|
||||
}
|
||||
}
|
||||
|
@ -4060,35 +4037,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
|
||||
<br>
|
||||
|
||||
### ReplaceVariableByPropertyFetchRector
|
||||
|
||||
Turns variable in controller action to property fetch, as follow up to action injection variable to property change.
|
||||
|
||||
- class: [`Rector\DependencyInjection\Rector\Variable\ReplaceVariableByPropertyFetchRector`](../rules/DependencyInjection/Rector/Variable/ReplaceVariableByPropertyFetchRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeController
|
||||
{
|
||||
/**
|
||||
* @var ProductRepository
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
public function __construct(ProductRepository $productRepository)
|
||||
{
|
||||
$this->productRepository = $productRepository;
|
||||
}
|
||||
|
||||
public function default()
|
||||
{
|
||||
- $products = $productRepository->fetchAll();
|
||||
+ $products = $this->productRepository->fetchAll();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## DowngradePhp53
|
||||
|
||||
### DirConstToFileConstRector
|
||||
|
@ -8883,7 +8831,7 @@ Add unique use imports collected during Rector run
|
|||
|
||||
Change global `$variables` to private properties
|
||||
|
||||
- class: [`Rector\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector`](../rules/Privatization/Rector/ClassMethod/ChangeGlobalVariablesToPropertiesRector.php)
|
||||
- class: [`Rector\Privatization\Rector\Class_\ChangeGlobalVariablesToPropertiesRector`](../rules/Privatization/Rector/Class_/ChangeGlobalVariablesToPropertiesRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class AutoImportTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureAutoImport');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/auto_import.php';
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
public function shorten($value)
|
||||
{
|
||||
return trim($value);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
public function shorten($value)
|
||||
{
|
||||
return \trim($value);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector\Fixture;
|
||||
|
||||
class SkipDefinedPreviousCall
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
function func_in_func($input) {
|
||||
echo $input;
|
||||
}
|
||||
|
||||
func_in_func("Test");
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector\Fixture;
|
||||
|
||||
class SkipVariableName
|
||||
{
|
||||
public function shorten($value)
|
||||
{
|
||||
return $value();
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector\FixtureAutoImport;
|
||||
|
||||
class AutoImportNames
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return new \stdClass();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector\FixtureAutoImport;
|
||||
|
||||
use stdClass;
|
||||
class AutoImportNames
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
return new stdClass();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,30 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector\FixtureAutoImport;
|
||||
|
||||
class DoNotAutoImportNames
|
||||
{
|
||||
public function shorten($value)
|
||||
{
|
||||
new \stdClass;
|
||||
return trim($value);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector\FixtureAutoImport;
|
||||
|
||||
use stdClass;
|
||||
class DoNotAutoImportNames
|
||||
{
|
||||
public function shorten($value)
|
||||
{
|
||||
new stdClass;
|
||||
return \trim($value);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class PreslashSimpleFunctionRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector;
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
|
||||
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(PreslashSimpleFunctionRector::class);
|
||||
};
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(PreslashSimpleFunctionRector::class);
|
||||
};
|
|
@ -1,95 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Rector\FuncCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @changelog https://stackoverflow.com/questions/55419673/php7-adding-a-slash-to-all-standard-php-functions-php-cs-fixer-rule
|
||||
*
|
||||
* @see \Rector\Tests\CodingStyle\Rector\FuncCall\PreslashSimpleFunctionRector\PreslashSimpleFunctionRectorTest
|
||||
*/
|
||||
final class PreslashSimpleFunctionRector extends AbstractRector
|
||||
{
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
'Add pre-slash to short named functions to improve performance',
|
||||
[
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function shorten($value)
|
||||
{
|
||||
return trim($value);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function shorten($value)
|
||||
{
|
||||
return \trim($value);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [FuncCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FuncCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node->name instanceof FullyQualified) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$functionName = $this->getName($node);
|
||||
if ($functionName === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (\str_contains($functionName, '\\')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$isDefinedPreviousCall = (bool) $this->betterNodeFinder->findFirstPreviousOfNode($node, function (Node $node) use (
|
||||
$functionName
|
||||
): bool {
|
||||
if (! $node instanceof Function_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isName($node->name, $functionName);
|
||||
});
|
||||
if ($isDefinedPreviousCall) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$node->name = new FullyQualified($functionName);
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user