Skip to content

Commit fd8871c

Browse files
authored
Merge pull request #547 from werlos/NoDuplicatedArrayKeyFixer_add_option_allow_duplicated_expressions
NoDuplicatedArrayKeyFixer - add option "ignore_expressions"
2 parents 0ea9643 + c091b66 commit fd8871c

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## v2.6.0
44
- Add StringableInterfaceFixer
5+
- NoDuplicatedArrayKeyFixer - add option "ignore_expressions"
56

67
## v2.5.0 - *2021-05-04*
78
- Add PHP CS Fixer v3 support

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Latest stable version](https://img.shields.io/packagist/v/kubawerlos/php-cs-fixer-custom-fixers.svg?label=current%20version)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
44
[![PHP version](https://img.shields.io/packagist/php-v/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://php.net)
55
[![License](https://img.shields.io/github/license/kubawerlos/php-cs-fixer-custom-fixers.svg)](LICENSE)
6-
![Tests](https://img.shields.io/badge/tests-2827-brightgreen.svg)
6+
![Tests](https://img.shields.io/badge/tests-2831-brightgreen.svg)
77
[![Downloads](https://img.shields.io/packagist/dt/kubawerlos/php-cs-fixer-custom-fixers.svg)](https://packagist.org/packages/kubawerlos/php-cs-fixer-custom-fixers)
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)
@@ -156,6 +156,8 @@ There must be no comment generated by Doctrine Migrations.
156156

157157
#### NoDuplicatedArrayKeyFixer
158158
Duplicated array keys must be removed.
159+
Configuration options:
160+
- `ignore_expressions` (`bool`): whether to keep duplicated expressions (as they might return different values) or not; defaults to `true`
159161
```diff
160162
<?php
161163
$x = [

src/Fixer/NoDuplicatedArrayKeyFixer.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
namespace PhpCsFixerCustomFixers\Fixer;
1515

16+
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
17+
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
18+
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
19+
use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
1620
use PhpCsFixer\FixerDefinition\CodeSample;
1721
use PhpCsFixer\FixerDefinition\FixerDefinition;
1822
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
@@ -24,8 +28,11 @@
2428
use PhpCsFixerCustomFixers\Analyzer\ArrayAnalyzer;
2529
use PhpCsFixerCustomFixers\TokenRemover;
2630

27-
final class NoDuplicatedArrayKeyFixer extends AbstractFixer
31+
final class NoDuplicatedArrayKeyFixer extends AbstractFixer implements ConfigurableFixerInterface
2832
{
33+
/** @var bool */
34+
private $ignoreExpressions = true;
35+
2936
public function getDefinition(): FixerDefinitionInterface
3037
{
3138
return new FixerDefinition(
@@ -40,6 +47,26 @@ public function getDefinition(): FixerDefinitionInterface
4047
);
4148
}
4249

50+
public function getConfigurationDefinition(): FixerConfigurationResolverInterface
51+
{
52+
return new FixerConfigurationResolver([
53+
(new FixerOptionBuilder('ignore_expressions', 'whether to keep duplicated expressions (as they might return different values) or not'))
54+
->setAllowedTypes(['bool'])
55+
->setDefault($this->ignoreExpressions)
56+
->getOption(),
57+
]);
58+
}
59+
60+
/**
61+
* @param null|array<string, bool> $configuration
62+
*/
63+
public function configure(?array $configuration = null): void
64+
{
65+
if (isset($configuration['ignore_expressions'])) {
66+
$this->ignoreExpressions = $configuration['ignore_expressions'];
67+
}
68+
}
69+
4370
public function getPriority(): int
4471
{
4572
return 0;
@@ -118,7 +145,7 @@ private function getKeyContentIfPossible(Tokens $tokens, ArrayElementAnalysis $a
118145
continue;
119146
}
120147

121-
if ($token->equalsAny([[\T_VARIABLE], '('])) {
148+
if ($this->ignoreExpressions && $token->equalsAny([[\T_VARIABLE], '('])) {
122149
return null;
123150
}
124151

tests/Fixer/NoDuplicatedArrayKeyFixerTest.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,25 @@ public function testIsRisky(): void
2525
self::assertFalse($this->fixer->isRisky());
2626
}
2727

28+
public function testConfiguration(): void
29+
{
30+
$options = $this->fixer->getConfigurationDefinition()->getOptions();
31+
self::assertArrayHasKey(0, $options);
32+
self::assertSame('ignore_expressions', $options[0]->getName());
33+
}
34+
2835
/**
36+
* @param null|array<string, bool> $configuration
37+
*
2938
* @dataProvider provideFixCases
3039
*/
31-
public function testFix(string $expected, ?string $input = null): void
40+
public function testFix(string $expected, ?string $input = null, ?array $configuration = null): void
3241
{
33-
$this->doTest($expected, $input);
42+
$this->doTest($expected, $input, $configuration);
3443
}
3544

3645
/**
37-
* @return iterable<array{0: string, 1?: string}>
46+
* @return iterable<array{0: string, 1?: null|string, 2?: array<string, bool>}>
3847
*/
3948
public static function provideFixCases(): iterable
4049
{
@@ -198,5 +207,32 @@ public static function provideFixCases(): iterable
198207
],
199208
];',
200209
];
210+
211+
yield [
212+
'<?php $x = [
213+
"foo" => 1,
214+
"FOO" => 2,
215+
];',
216+
];
217+
218+
yield [
219+
'<?php $x = [
220+
getRandomIndex() => 1,
221+
getRandomIndex() => 2,
222+
];',
223+
null,
224+
['ignore_expressions' => true],
225+
];
226+
227+
yield [
228+
'<?php $x = [
229+
getRandomIndex() => 2,
230+
];',
231+
'<?php $x = [
232+
getRandomIndex() => 1,
233+
getRandomIndex() => 2,
234+
];',
235+
['ignore_expressions' => false],
236+
];
201237
}
202238
}

0 commit comments

Comments
 (0)