Skip to content

Commit

Permalink
Merge InArrayAndArrayKeysToArrayKeyExistsRector to ArrayKeysAndInArra…
Browse files Browse the repository at this point in the history
…yToArrayKeyExistsRector with almost identical behavior (#2047)
  • Loading branch information
TomasVotruba committed Apr 10, 2022
1 parent 9b8e161 commit 0227d24
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 284 deletions.
50 changes: 7 additions & 43 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 510 Rules Overview
# 508 Rules Overview

<br>

## Categories

- [Arguments](#arguments) (5)

- [CodeQuality](#codequality) (72)
- [CodeQuality](#codequality) (70)

- [CodingStyle](#codingstyle) (35)

Expand Down Expand Up @@ -354,14 +354,11 @@ Replace `array_keys()` and `in_array()` to `array_key_exists()`
- class: [`Rector\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector`](../rules/CodeQuality/Rector/FuncCall/ArrayKeysAndInArrayToArrayKeyExistsRector.php)

```diff
class SomeClass
function run($packageName, $values)
{
public function run($packageName, $values)
{
- $keys = array_keys($values);
- return in_array($packageName, $keys, true);
+ return array_key_exists($packageName, $values);
}
- $keys = array_keys($values);
- return in_array($packageName, $keys, true);
+ return array_key_exists($packageName, $values);
}
```

Expand Down Expand Up @@ -855,19 +852,6 @@ Changes comparison with get_class to instanceof

<br>

### InArrayAndArrayKeysToArrayKeyExistsRector

Simplify `in_array` and `array_keys` functions combination into `array_key_exists` when `array_keys` has one argument only

- class: [`Rector\CodeQuality\Rector\FuncCall\InArrayAndArrayKeysToArrayKeyExistsRector`](../rules/CodeQuality/Rector/FuncCall/InArrayAndArrayKeysToArrayKeyExistsRector.php)

```diff
-in_array("key", array_keys($array), true);
+array_key_exists("key", $array);
```

<br>

### InlineConstructorDefaultToPropertyRector

Move property default from constructor to property default
Expand Down Expand Up @@ -1243,26 +1227,6 @@ Simplify negated conditions with de Morgan theorem

<br>

### SimplifyDuplicatedTernaryRector

Remove ternary that duplicated return value of true : false

- class: [`Rector\CodeQuality\Rector\Ternary\SimplifyDuplicatedTernaryRector`](../rules/CodeQuality/Rector/Ternary/SimplifyDuplicatedTernaryRector.php)

```diff
class SomeClass
{
public function run(bool $value, string $name)
{
- $isTrue = $value ? true : false;
+ $isTrue = $value;
$isName = $name ? true : false;
}
}
```

<br>

### SimplifyEmptyArrayCheckRector

Simplify `is_array` and `empty` functions combination into a simple identical check for an empty array
Expand Down Expand Up @@ -1641,7 +1605,7 @@ When throwing into a catch block, checks that the previous exception is passed t

### UnnecessaryTernaryExpressionRector

Remove unnecessary ternary expressions.
Remove unnecessary ternary expressions

- class: [`Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector`](../rules/CodeQuality/Rector/Ternary/UnnecessaryTernaryExpressionRector.php)

Expand Down
2 changes: 0 additions & 2 deletions config/set/code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use Rector\CodeQuality\Rector\FuncCall\CallUserFuncWithArrowFunctionToInlineRector;
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector;
use Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector;
use Rector\CodeQuality\Rector\FuncCall\InArrayAndArrayKeysToArrayKeyExistsRector;
use Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector;
use Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector;
use Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector;
Expand Down Expand Up @@ -86,7 +85,6 @@
$services->set(ReplaceMultipleBooleanNotRector::class);
$services->set(ForeachToInArrayRector::class);
$services->set(SimplifyForeachToCoalescingRector::class);
$services->set(InArrayAndArrayKeysToArrayKeyExistsRector::class);
$services->set(SimplifyFuncGetArgsCountRector::class);
$services->set(SimplifyInArrayValuesRector::class);
$services->set(SimplifyStrposLowerRector::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\Fixture;

final class AnotherInArray
{
public function resultIntoAVariable(): void
{
$array = ['foo', 'bar'];
$key = 'key';

$result = in_array($key, array_keys($array), true);
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\Fixture;

final class AnotherInArray
{
public function resultIntoAVariable(): void
{
$array = ['foo', 'bar'];
$key = 'key';

$result = array_key_exists($key, $array);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\Fixture;

final class JustCalledArrayKeys
{
public function hasKey(): bool
{
return !in_array('key', array_keys([
'key' => ',',
1 => ';',
]));
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\Fixture;

final class JustCalledArrayKeys
{
public function hasKey(): bool
{
return !array_key_exists('key', [
'key' => ',',
1 => ';',
]);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\Fixture;

final class SkipArrayKeysWithExtraArgument
{
public function hasMoreThanOneArgument(): bool
{
return in_array('key', array_keys([
'key' => ',',
1 => ';',
], 'key'));
}
}

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 0227d24

Please sign in to comment.