Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<?php declare(strict_types=1);

use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return ECSConfig::configure()
->withPaths([
return static function (\Symplify\EasyCodingStandard\Config\ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withSkip([
]);
$ecsConfig->skip([
__DIR__ . '/tests/fixtures',
])
->withPhpCsFixerSets(perCS20: true);
]);
$ecsConfig->sets([SetList::PSR_12, SetList::NAMESPACES, SetList::DOCBLOCK]);

$ecsConfig->rules([
]);
};
80 changes: 80 additions & 0 deletions src/Rules/DuplicatedArrayKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace PMarki\PHPStanRules\Rules;

use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Array_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* Check if keys in array are unique
*
* @implements Rule<Array_>
*/
class DuplicatedArrayKeys implements Rule
{
public function getNodeType(): string
{
return Array_::class;
}

/**
* @return array<int, \PHPStan\Rules\RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$node instanceof Array_) {
return [];
}

$errors = [];
$existingKeys = [];

foreach ($node->items as $item) {
if (!$item instanceof ArrayItem || $item->key === null) {
continue;
}

$key = $this->getKey($item->key);
if ($key === null) {
continue;
}

if (\in_array($key, $existingKeys, true)) {
$errors[] = RuleErrorBuilder::message("Array key '$key' is duplicated.")
->identifier('extraDuplicatedArrayKeys')
->line($item->key->getLine())
->build();
continue;
}

$existingKeys[] = $key;
}

return $errors;
}

private function getKey(Node $node): string|int|null
{
if ($node instanceof Node\Scalar\String_) {
$arr = [$node->value => 1];

return \array_key_first($arr);
}

if ($node instanceof Node\Scalar\Int_) {
return $node->value;
}

if ($node instanceof Node\Scalar\Float_) {
return (int) $node->value;
}

return null;
}
}
2 changes: 1 addition & 1 deletion src/Rules/ProtectedPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PHPStan\Node\ClassPropertyNode;
use PHPStan\Analyser\Scope;
use PHPStan\Node\ClassPropertyNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

Expand Down
38 changes: 38 additions & 0 deletions tests/DuplicatedArrayKeysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PMarki\PHPStanRules\Tests;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use PMarki\PHPStanRules\Rules\DuplicatedArrayKeys;

class DuplicatedArrayKeysTest extends RuleTestCase
{
public function testNoAbstractClass(): void
{
$this->analyse(
[__DIR__ . '/fixtures/DuplicatedArrayKeys.php'],
[
[
"Array key 'key1' is duplicated.",
6,
],
[
"Array key '2' is duplicated.",
12,
],
[
"Array key 'nested-key2' is duplicated.",
30,
],
],
);
}

protected function getRule(): Rule
{
return new DuplicatedArrayKeys();
}
}
38 changes: 38 additions & 0 deletions tests/fixtures/DuplicatedArrayKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

$array = [
'key1' => 'value1',
'key2' => 'value2',
'key1' => 'duplicate',
];

$array = [
1 => 'value1',
2 => 'value2',
2 => 'value3',
];

$array = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => [
'key1' => 'value1',
'key2' => 'value2',
]
];

$array = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => [
'nested-key1' => 'value1',
'nested-key2' => 'value2',
'nested-key2' => 'duplicated',
]
];

$array = [
'value1',
'value2',
'value1',
];