mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-15 13:25:30 +01:00
Merge pull request #2049 from jeroenherczeg/strict_array_search
Added StrictArraySearchRector - Issue #2009
This commit is contained in:
commit
8bc5ccf9c4
@ -1,4 +1,4 @@
|
||||
# All 357 Rectors Overview
|
||||
# All 365 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
- [General](#general)
|
||||
@ -602,6 +602,29 @@ If conditions is always true, perform the content right away
|
||||
|
||||
<br>
|
||||
|
||||
### `RemoveSoleValueSprintfRector`
|
||||
|
||||
- class: `Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector`
|
||||
|
||||
Remove sprintf() wrapper if not needed
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- $value = sprintf('%s', 'hi');
|
||||
+ $value = 'hi';
|
||||
|
||||
$welcome = 'hello';
|
||||
- $value = sprintf('%s', $welcome);
|
||||
+ $value = $welcome;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `SimplifyArraySearchRector`
|
||||
|
||||
- class: `Rector\CodeQuality\Rector\Identical\SimplifyArraySearchRector`
|
||||
@ -1055,6 +1078,25 @@ Changes switch with 2 options to if-else
|
||||
|
||||
<br>
|
||||
|
||||
### `CallUserFuncCallToVariadicRector`
|
||||
|
||||
- class: `Rector\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector`
|
||||
|
||||
Replace call_user_func_call with variadic
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- call_user_func_array('some_function', $items);
|
||||
+ some_function(...$items);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `CatchExceptionNameMatchingTypeRector`
|
||||
|
||||
- class: `Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector`
|
||||
@ -1195,6 +1237,31 @@ Import fully qualified names to use statements
|
||||
|
||||
<br>
|
||||
|
||||
### `MakeInheritedMethodVisibilitySameAsParentRector`
|
||||
|
||||
- class: `Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector`
|
||||
|
||||
Make method visibility same as parent one
|
||||
|
||||
```diff
|
||||
class ChildClass extends ParentClass
|
||||
{
|
||||
- public function run()
|
||||
+ protected function run()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
class ParentClass
|
||||
{
|
||||
protected function run()
|
||||
{
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `ManualJsonStringToJsonEncodeArrayRector`
|
||||
|
||||
- class: `Rector\CodingStyle\Rector\String_\ManualJsonStringToJsonEncodeArrayRector`
|
||||
@ -1420,6 +1487,19 @@ Separate class constant in a string to class constant fetch and string
|
||||
|
||||
<br>
|
||||
|
||||
### `StrictArraySearchRector`
|
||||
|
||||
- class: `Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector`
|
||||
|
||||
Makes array_search search for identical elements
|
||||
|
||||
```diff
|
||||
-array_search($value, $items);
|
||||
+array_search($value, $items, true);
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `SymplifyQuoteEscapeRector`
|
||||
|
||||
- class: `Rector\CodingStyle\Rector\String_\SymplifyQuoteEscapeRector`
|
||||
@ -2282,17 +2362,21 @@ Remove temporary *Uuid relation properties
|
||||
Migrates addFilter to addQuery
|
||||
|
||||
```diff
|
||||
use ONGR\ElasticsearchDSL\Search;
|
||||
use ONGR\ElasticsearchDSL\Query\TermsQuery;
|
||||
+use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$search = new \ONGR\ElasticsearchDSL\Search();
|
||||
$search = new Search();
|
||||
|
||||
- $search->addFilter(
|
||||
- new \ONGR\ElasticsearchDSL\Query\TermsQuery('categoryIds', [1, 2])
|
||||
- new TermsQuery('categoryIds', [1, 2])
|
||||
+ $search->addQuery(
|
||||
+ new \ONGR\ElasticsearchDSL\Query\TermsQuery('categoryIds', [1, 2]),
|
||||
+ \ONGR\ElasticsearchDSL\Query\Compound\BoolQuery::FILTER
|
||||
+ new TermsQuery('categoryIds', [1, 2]),
|
||||
+ BoolQuery::FILTER
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -3038,9 +3122,6 @@ services:
|
||||
+ $this->doTestSingle($number);
|
||||
+ }
|
||||
+
|
||||
+ /**
|
||||
+ * @return int[]
|
||||
+ */
|
||||
+ public function provideDataForTest(): \Iterator
|
||||
+ {
|
||||
+ yield [1];
|
||||
@ -3637,6 +3718,25 @@ Remove unused private method
|
||||
|
||||
## Php53
|
||||
|
||||
### `DirNameFileConstantToDirConstantRector`
|
||||
|
||||
- class: `Rector\Php53\Rector\FuncCall\DirNameFileConstantToDirConstantRector`
|
||||
|
||||
Convert dirname(__FILE__) to __DIR__
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- return dirname(__FILE__);
|
||||
+ return __DIR__;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `TernaryToElvisRector`
|
||||
|
||||
- class: `Rector\Php53\Rector\Ternary\TernaryToElvisRector`
|
||||
@ -3674,13 +3774,41 @@ Remove & from function and method calls
|
||||
|
||||
<br>
|
||||
|
||||
### `RemoveZeroBreakContinueRector`
|
||||
|
||||
- class: `Rector\Php54\Rector\Break_\RemoveZeroBreakContinueRector`
|
||||
|
||||
Remove 0 from break and continue
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run($random)
|
||||
{
|
||||
- continue 0;
|
||||
- break 0;
|
||||
+ continue;
|
||||
+ break;
|
||||
|
||||
$five = 5;
|
||||
- continue $five;
|
||||
+ continue 5;
|
||||
|
||||
- break $random;
|
||||
+ break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## Php55
|
||||
|
||||
### `PregReplaceEModifierRector`
|
||||
|
||||
- class: `Rector\Php55\Rector\FuncCall\PregReplaceEModifierRector`
|
||||
|
||||
The /e modifier is no longer supported, use preg_replace_callback instead
|
||||
The /e modifier is no longer supported, use preg_replace_callback instead
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
@ -3804,8 +3932,8 @@ Changes call_user_method()/call_user_method_array() to call_user_func()/call_use
|
||||
list() cannot be empty
|
||||
|
||||
```diff
|
||||
-list() = $values;
|
||||
+list($generated) = $values;
|
||||
-'list() = $values;'
|
||||
+'list($unusedGenerated) = $values;'
|
||||
```
|
||||
|
||||
<br>
|
||||
@ -4115,6 +4243,29 @@ Changes is_array + Traversable check to is_iterable
|
||||
|
||||
<br>
|
||||
|
||||
### `ListToArrayDestructRector`
|
||||
|
||||
- class: `Rector\Php71\Rector\List_\ListToArrayDestructRector`
|
||||
|
||||
Remove & from new &X
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- list($id1, $name1) = $data;
|
||||
+ [$id1, $name1] = $data;
|
||||
|
||||
- foreach ($data as list($id, $name)) {
|
||||
+ foreach ($data as [$id, $name]) {
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `MultiExceptionCatchRector`
|
||||
|
||||
- class: `Rector\Php71\Rector\TryCatch\MultiExceptionCatchRector`
|
||||
@ -4322,8 +4473,11 @@ String asserts must be passed directly to assert()
|
||||
Removes (unset) cast
|
||||
|
||||
```diff
|
||||
-$different = (unset) $value;
|
||||
+$different = null;
|
||||
|
||||
-$value = (unset) $value;
|
||||
+$value = null;
|
||||
+unset($value);
|
||||
```
|
||||
|
||||
<br>
|
||||
@ -5433,6 +5587,25 @@ services:
|
||||
|
||||
<br>
|
||||
|
||||
### `MissingClassConstantReferenceToStringRector`
|
||||
|
||||
- class: `Rector\Restoration\Rector\ClassConstFetch\MissingClassConstantReferenceToStringRector`
|
||||
|
||||
Convert missing class reference to string
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- return NonExistingClass::class;
|
||||
+ return 'NonExistingClass';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## SOLID
|
||||
|
||||
### `FinalizeClassesWithoutChildrenRector`
|
||||
|
@ -917,7 +917,7 @@ if (true) {
|
||||
|
||||
```php
|
||||
?>
|
||||
<strong>feel</strong><?php
|
||||
<strong>feel</strong><?php
|
||||
```
|
||||
<br>
|
||||
|
||||
|
@ -0,0 +1,46 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Rector\FuncCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\CodingStyle\Tests\Rector\FuncCall\StrictArraySearchRector\StrictArraySearchRectorTest
|
||||
*/
|
||||
final class StrictArraySearchRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Makes array_search search for identical elements', [
|
||||
new CodeSample('array_search($value, $items);', 'array_search($value, $items, true);'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [FuncCall::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FuncCall $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $this->isName($node, 'array_search')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (count($node->args) === 2) {
|
||||
$node->args[2] = $this->createArg($this->createTrue());
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\FuncCall\StrictArraySearchRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
array_search($value, $items);
|
||||
}
|
||||
}
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\FuncCall\StrictArraySearchRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
array_search($value, $items, true);
|
||||
}
|
||||
}
|
||||
?>
|
@ -0,0 +1,28 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\FuncCall\StrictArraySearchRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\CodingStyle\Rector\FuncCall\StrictArraySearchRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class StrictArraySearchRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideDataForTest()
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public function provideDataForTest(): Iterator
|
||||
{
|
||||
yield [__DIR__ . '/Fixture/fixture.php.inc'];
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return StrictArraySearchRector::class;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user