-
-
Notifications
You must be signed in to change notification settings - Fork 344
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
Feature/duplicated array keys #484
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
44f20a9
Detectign duplicated array keys.
rafalwrzeszcz 5570e3e
Oh, we still support PHP 5.3… so 2009 ;).
rafalwrzeszcz dc87a6d
Added `public` modifier, just so Scrutinizer don't complain.
rafalwrzeszcz 48e6db8
Moved rule to CleanCode section.
rafalwrzeszcz b1ea2ef
Closes https://github.com/phpmd/phpmd/pull/350
eeree 103930e
Make Stickler Bot happy
eeree ddb8c51
Resolves https://github.com/phpmd/phpmd/pull/484#pullrequestreview-42…
eeree 3c5a822
Merge remote-tracking branch 'upstream/master' into feature/duplicate…
eeree a040a41
Resolves https://github.com/phpmd/phpmd/pull/484#pullrequestreview-43…
eeree File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
src/main/php/PHPMD/Rule/CleanCode/DuplicatedArrayKey.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
/** | ||
* This file is part of PHP Mess Detector. | ||
* | ||
* Copyright (c) Manuel Pichler <mapi@phpmd.org>. | ||
* All rights reserved. | ||
* | ||
* Licensed under BSD License | ||
* For full copyright and license information, please see the LICENSE file. | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @author Manuel Pichler <mapi@phpmd.org> | ||
* @copyright Manuel Pichler. All rights reserved. | ||
* @license https://opensource.org/licenses/bsd-license.php BSD License | ||
* @link http://phpmd.org/ | ||
*/ | ||
|
||
namespace PHPMD\Rule\CleanCode; | ||
|
||
use PDepend\Source\AST\AbstractASTNode; | ||
use PDepend\Source\AST\ASTArrayElement; | ||
use PDepend\Source\AST\ASTLiteral; | ||
use PDepend\Source\AST\ASTNode as PDependASTNode; | ||
use PHPMD\AbstractNode; | ||
use PHPMD\AbstractRule; | ||
use PHPMD\Node\ASTNode; | ||
use PHPMD\Rule\FunctionAware; | ||
use PHPMD\Rule\MethodAware; | ||
|
||
/** | ||
* Duplicated Array Key Rule | ||
* | ||
* This rule detects duplicated array keys. | ||
* | ||
* @author Rafał Wrzeszcz <rafal.wrzeszcz@wrzasq.pl> | ||
* @author Kamil Szymanaski <kamil.szymanski@gmail.com> | ||
*/ | ||
class DuplicatedArrayKey extends AbstractRule implements MethodAware, FunctionAware | ||
{ | ||
/** | ||
* Retrieves all arrays from single node and performs comparison logic on it | ||
* | ||
* @param AbstractNode $node | ||
* @return void | ||
*/ | ||
public function apply(AbstractNode $node) | ||
{ | ||
foreach ($node->findChildrenOfType('Array') as $arrayNode) { | ||
/** @var ASTNode $arrayNode */ | ||
$this->checkForDuplicatedArrayKeys($arrayNode); | ||
} | ||
} | ||
|
||
/** | ||
* This method checks if a given function or method contains an array literal | ||
* with duplicated entries for any key and emits a rule violation if so. | ||
* | ||
* @param ASTNode $node Array node. | ||
* @return void | ||
*/ | ||
private function checkForDuplicatedArrayKeys(ASTNode $node) | ||
{ | ||
$keys = array(); | ||
/** @var ASTArrayElement $arrayElement */ | ||
foreach ($node->getChildren() as $index => $arrayElement) { | ||
$arrayElement = $this->normalizeKey($arrayElement, $index); | ||
if (null === $arrayElement) { | ||
// skip everything that can't be resolved easily | ||
continue; | ||
} | ||
|
||
$key = $arrayElement->getImage(); | ||
if (isset($keys[$key])) { | ||
$this->addViolation($node, array($key, $arrayElement->getStartLine())); | ||
continue; | ||
} | ||
$keys[$key] = $arrayElement; | ||
} | ||
} | ||
|
||
/** | ||
* Changes key name to its string format. | ||
* | ||
* To compare keys, we have to cast them to string. | ||
* Non-associative keys have to use index as its key, | ||
* while boolean and nulls have to be casted respectively. | ||
* As current logic doesn't evaluate expressions nor constants, | ||
* statics, globals, etc. we simply skip them. | ||
* | ||
* @param AbstractASTNode $node Array key to evaluate. | ||
* @param int $index Fallback in case of non-associative arrays | ||
* @return AbstractASTNode Key name | ||
*/ | ||
private function normalizeKey(AbstractASTNode $node, $index) | ||
{ | ||
// non-associative - key name equals to its index | ||
if (count($node->getChildren()) === 0) { | ||
$node->setImage((string) $index); | ||
return $node; | ||
} | ||
|
||
$node = $node->getChild(0); | ||
if (!($node instanceof ASTLiteral)) { | ||
// skip expressions, method calls, globals and constants | ||
return null; | ||
} | ||
$node->setImage($this->castStringFromLiteral($node)); | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* Cleans string literals and casts boolean and null values as PHP engine does | ||
* | ||
* @param PDependASTNode $key | ||
* @return string | ||
*/ | ||
private function castStringFromLiteral(PDependASTNode $key) | ||
{ | ||
$value = $key->getImage(); | ||
switch ($value) { | ||
case 'false': | ||
return '0'; | ||
case 'true': | ||
return '1'; | ||
case 'null': | ||
return ''; | ||
default: | ||
return trim($value, '\'""'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,7 +68,7 @@ This rule has the following properties: | |
=================================== =============== =============================================== | ||
|
||
IfStatementAssignment | ||
============= | ||
===================== | ||
|
||
Since: PHPMD 2.7.0 | ||
|
||
|
@@ -91,6 +91,27 @@ Example: :: | |
} | ||
|
||
|
||
DuplicateArrayKey | ||
================= | ||
|
||
Since: PHPMD 2.7.0 | ||
|
||
Defining another value for the same key in an array literal overrides the previous key/value, which makes it effectively an unused code. If it's known from the beginning that the key will have different value, there is usually no point in defining first one. | ||
|
||
|
||
Example: :: | ||
|
||
function createArray() { | ||
return [ | ||
'non-associative 0-element', // not applied | ||
0 => 'associative 0-element', // applied | ||
false => 'associative 0-element', // applied | ||
'foo' => 'bar', // not applied | ||
"foo" => 'baz', // applied | ||
]; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Superfluous line |
||
|
||
Remark | ||
====== | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,4 +211,3 @@ Remark | |
This document is based on a ruleset xml-file, that was taken from the original source of the `PMD`__ project. This means that most parts of the content on this page are the intellectual work of the PMD community and its contributors and not of the PHPMD project. | ||
|
||
__ http://pmd.sourceforge.net/ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this removed? We might add the following to our
Should not be part of the PR, though. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Superfluous line