Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DuplicateKey #12

Merged
merged 1 commit into from
Oct 28, 2017
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
23 changes: 23 additions & 0 deletions docs/DuplicateKey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# DuplicateKey

If the keys of the array duplicate, it will be overwritten with the specified value later.

NOTE: Array keys are cast in some cases. However, it is not possible to detect duplicate keys in such cases.

## Before

```php
<?php
$array = ["a" => 1, "a" => 2]; // DuplicateKey: Duplicate keys found in array.
```

## After

```php
<?php
$array = ["a" => 1, "b" => 2]; // OK!
```

## Reference

https://secure.php.net/manual/en/language.types.array.php
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@

- [SquareBracketSyntax](SquareBracketSyntax.md)
- [RedundantTernaryOperator](RedundantTernaryOperator.md)
- [DuplicateKey](DuplicateKey.md)
85 changes: 85 additions & 0 deletions src/Tool/DuplicateKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php declare(strict_types=1);

namespace Pahout\Tool;

use \ast\Node;
use Pahout\Logger;
use Pahout\Hint;

/**
* Detect duplicate key in array.
*
* If the keys of the array duplicate, it will be overwritten with the specified value later.
*
* ## Before
*
* ```
* <?php
* $array = ["a" => 1, "a" => 2];
* ```
*
* ## After
*
* ```
* <?php
* $array = ["a" => 1, "b" => 2];
* ```
*
*/
class DuplicateKey implements Base
{
/** Analyze array declarations node (AST_ARRAY) */
public const ENTRY_POINT = \ast\AST_ARRAY;
/** PHP version to enable this tool */
public const PHP_VERSION = '0.0.0';
public const HINT_TYPE = "DuplicateKey";
private const HINT_MESSAGE = 'Duplicate keys found in array.';
private const HINT_LINK = Hint::DOCUMENT_LINK."/DuplicateKey.md";

/**
* Detect duplicate numbers, strings, node key
*
* @param string $file File name to be analyzed.
* @param Node $node AST node to be analyzed.
* @return Hint[] List of hints obtained from results.
*/
public function run(string $file, Node $node): array
{
$hints = [];
$keys = [];
$elems = [];

foreach ($node->children as $elem) {
if ($elem instanceof Node && !is_null($elem->children['key'])) {
$elems[] = $elem;
$key = $elem->children['key'];
$keys[] = $key instanceof Node ? $key->children : $key;
}
}

while (count($keys) > 0) {
$elem = array_pop($elems);
$key = array_pop($keys);

if ($key instanceof Node) {
// If the key is a node, use equal comparison operator
$strict = false;
} else {
// If the key is not a node, use identical comparison operator
$strict = true;
}

if (in_array($key, $keys, $strict)) {
$hints[] = new Hint(
self::HINT_TYPE,
self::HINT_MESSAGE,
$file,
$elem->lineno,
self::HINT_LINK
);
}
}

return $hints;
}
}
1 change: 1 addition & 0 deletions src/ToolBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ToolBox
'EscapeShellArg',
'SquareBracketSyntax',
'RedundantTernaryOperator',
'DuplicateKey',
];

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

namespace Pahout\Test\Tool;

use PHPUnit\Framework\TestCase;
use Pahout\Test\helper\PahoutHelper;
use Pahout\Tool\DuplicateKey;
use Pahout\Hint;
use Pahout\Logger;
use Pahout\Config;
use Symfony\Component\Console\Output\ConsoleOutput;

class DuplicateKeyTest extends TestCase
{
public function setUp()
{
Logger::getInstance(new ConsoleOutput());
}

public function test_duplicate_key()
{
$code = <<<'CODE'
<?php
$array = [
"a" => 1,
"a" => 2,
];
CODE;
$root = \ast\parse_code($code, Config::AST_VERSION);

$tester = PahoutHelper::create(new DuplicateKey());
$tester->test($root);

$this->assertEquals(
[
new Hint(
'DuplicateKey',
'Duplicate keys found in array.',
'./test.php',
4,
Hint::DOCUMENT_LINK.'/DuplicateKey.md'
)
],
$tester->hints
);
}

public function test_duplicate_node_key()
{
$code = <<<'CODE'
<?php
$array = [
$test => 1,
$test => 2
];
CODE;
$root = \ast\parse_code($code, Config::AST_VERSION);

$tester = PahoutHelper::create(new DuplicateKey());
$tester->test($root);

$this->assertEquals(
[
new Hint(
'DuplicateKey',
'Duplicate keys found in array.',
'./test.php',
4,
Hint::DOCUMENT_LINK.'/DuplicateKey.md'
)
],
$tester->hints
);
}

public function test_unduplicate_key()
{
$code = <<<'CODE'
<?php
$array = [
"a" => 1,
"b" => 2
];
CODE;
$root = \ast\parse_code($code, Config::AST_VERSION);

$tester = PahoutHelper::create(new DuplicateKey());
$tester->test($root);

$this->assertEmpty($tester->hints);
}
}