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
Conversation
* @link http://phpmd.org/ | ||
*/ | ||
|
||
class testRuleAppliesMultipleTimesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys |
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.
Each class must be in a namespace of at least one level (a top-level vendor name)
Class name "testRuleAppliesMultipleTimesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys" is not in camel caps format
* @link http://phpmd.org/ | ||
*/ | ||
|
||
class testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys |
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.
Each class must be in a namespace of at least one level (a top-level vendor name)
Class name "testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys" is not in camel caps format
* @link http://phpmd.org/ | ||
*/ | ||
|
||
class testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedMixedQuotedKeys |
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.
Each class must be in a namespace of at least one level (a top-level vendor name)
Class name "testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedMixedQuotedKeys" is not in camel caps format
* @link http://phpmd.org/ | ||
*/ | ||
|
||
class testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedMixedTypeKeys |
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.
Each class must be in a namespace of at least one level (a top-level vendor name)
Class name "testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedMixedTypeKeys" is not in camel caps format
return 'foo'; | ||
} | ||
|
||
class testRuleAppliesWhenKeyIsDeclaredInNonStandardWay extends testRuleAppliesWhenKeyIsDeclaredInNonStandardWayParent |
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.
Class name "testRuleAppliesWhenKeyIsDeclaredInNonStandardWay" is not in camel caps format
{ | ||
const DUPLICATED_KEY = 'foo'; | ||
|
||
static $classStaticPropertyDuplicate = 'foo'; |
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.
Visibility must be declared on property "$classStaticPropertyDuplicate"
} | ||
} | ||
|
||
interface testRuleAppliesWhenKeyIsDeclaredInNonStandardWayInterface |
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.
Each class must be in a file by itself
Interface name "testRuleAppliesWhenKeyIsDeclaredInNonStandardWayInterface" is not in camel caps format
const INTERFACE_DUPLICATED_KEY = 'foo'; | ||
} | ||
|
||
abstract class testRuleAppliesWhenKeyIsDeclaredInNonStandardWayAbstract |
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.
Each interface must be in a file by itself
Class name "testRuleAppliesWhenKeyIsDeclaredInNonStandardWayAbstract" is not in camel caps format
} | ||
|
||
abstract class testRuleAppliesWhenKeyIsDeclaredInNonStandardWayAbstract | ||
implements testRuleAppliesWhenKeyIsDeclaredInNonStandardWayInterface |
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.
The implements keyword must be on the same line as the class name
const DUPLICATED_KEY = 'foo'; | ||
} | ||
|
||
class testRuleAppliesWhenKeyIsDeclaredInNonStandardWayParent |
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.
Each class must be in a file by itself
Class name "testRuleAppliesWhenKeyIsDeclaredInNonStandardWayParent" is not in camel caps format
} | ||
|
||
class testRuleAppliesWhenKeyIsDeclaredInNonStandardWayParent | ||
extends testRuleAppliesWhenKeyIsDeclaredInNonStandardWayAbstract |
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.
The extends keyword must be on the same line as the class name
* @link http://phpmd.org/ | ||
*/ | ||
|
||
class testRuleNotAppliesToMethodWithAssotiativeArrayDefinitionWithoutDuplicatedKeys |
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.
Each class must be in a namespace of at least one level (a top-level vendor name)
Class name "testRuleNotAppliesToMethodWithAssotiativeArrayDefinitionWithoutDuplicatedKeys" is not in camel caps format
* @link http://phpmd.org/ | ||
*/ | ||
|
||
class testRuleNotAppliesToMethodWithNonAssotiativeArrayDefinition |
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.
Each class must be in a namespace of at least one level (a top-level vendor name)
Class name "testRuleNotAppliesToMethodWithNonAssotiativeArrayDefinition" is not in camel caps format
* @link http://phpmd.org/ | ||
*/ | ||
|
||
class testRuleNotAppliesToMethodWithoutArrayDefinition |
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.
Each class must be in a namespace of at least one level (a top-level vendor name)
Class name "testRuleNotAppliesToMethodWithoutArrayDefinition" is not in camel caps format
|
||
namespace PHPMDTest; | ||
|
||
class testRuleAppliesMultipleTimesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys |
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.
Class name "testRuleAppliesMultipleTimesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys" is not in camel caps format
|
||
namespace PHPMDTest; | ||
|
||
class testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys |
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.
Class name "testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys" is not in camel caps format
|
||
namespace PHPMDTest; | ||
|
||
class testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedMixedQuotedKeys |
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.
Class name "testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedMixedQuotedKeys" is not in camel caps format
|
||
namespace PHPMDTest; | ||
|
||
class testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedMixedTypeKeys |
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.
Class name "testRuleAppliesToMethodWithAssotiativeArrayDefinitionWithDuplicatedMixedTypeKeys" is not in camel caps format
const INTERFACE_DUPLICATED_KEY = 'foo'; | ||
} | ||
|
||
abstract class testRuleAppliesWhenKeyIsDeclaredInNonStandardWayAbstract implements |
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.
Each interface must be in a file by itself
Class name "testRuleAppliesWhenKeyIsDeclaredInNonStandardWayAbstract" is not in camel caps format
const DUPLICATED_KEY = 'foo'; | ||
} | ||
|
||
class testRuleAppliesWhenKeyIsDeclaredInNonStandardWayParent extends testRuleAppliesWhenKeyIsDeclaredInNonStandardWayAbstract |
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.
Each class must be in a file by itself
Class name "testRuleAppliesWhenKeyIsDeclaredInNonStandardWayParent" is not in camel caps format
Line exceeds 120 characters; contains 125 characters
|
||
namespace PHPMDTest; | ||
|
||
class testRuleNotAppliesToMethodWithAssotiativeArrayDefinitionWithoutDuplicatedKeys |
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.
Class name "testRuleNotAppliesToMethodWithAssotiativeArrayDefinitionWithoutDuplicatedKeys" is not in camel caps format
|
||
namespace PHPMDTest; | ||
|
||
class testRuleNotAppliesToMethodWithNonAssotiativeArrayDefinition |
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.
Class name "testRuleNotAppliesToMethodWithNonAssotiativeArrayDefinition" is not in camel caps format
|
||
namespace PHPMDTest; | ||
|
||
class testRuleNotAppliesToMethodWithoutArrayDefinition |
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.
Class name "testRuleNotAppliesToMethodWithoutArrayDefinition" is not in camel caps format
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.
Please add a test with two or more arrays with the same duplicated array keys
And change some of the tests that not all return, at least one should assign the content of the array to a variable or whatever.
@@ -2,7 +2,7 @@ | |||
|
|||
class testRuleAppliesMultipleTimesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys | |||
{ | |||
function testRuleAppliesMultipleTimesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys() | |||
public function testRuleAppliesMultipleTimesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys() |
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.
You shouldn't do this just to satisfy some CI, but this actually communicates intention explicitly.
* @license https://opensource.org/licenses/bsd-license.php BSD License | ||
* @link http://phpmd.org/ | ||
*/ | ||
|
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
/** | ||
* Duplicated Array Key Rule | ||
* | ||
* This rule detects if array literal has duplicated entries for any key. |
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.
This rule detects duplicated array keys.
class DuplicatedArrayKey extends AbstractRule implements MethodAware, FunctionAware | ||
{ | ||
/** | ||
* This method checks if a given function or method contains an array literal |
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.
Overly complicated: "This method checks if a given function or method contains an array which has duplicated keys and emits a rule violation if so.
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.
Actually, this method only loops over it...
* @param PDependASTNode $key | ||
* @return string | ||
*/ | ||
private function stringFromLiteral(PDependASTNode $key) |
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.
castStringFromLiteral
parent::INTERFACE_DUPLICATED_KEY => 'bar', // not applied (to be implemented?) | ||
static::INTERFACE_DUPLICATED_KEY => 'bar', // not applied (to be implemented?) | ||
$foo => 'bar', // not applied - resolving variables is impossible in this context | ||
$baz => 'bar', // not applied - see above |
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.
copy comment to this line. It might not be clear where above.
$baz => 'bar', // not applied - see above | ||
$this->getDuplicatedName() => 'bar', // not applied - resolving variable depends on inheritance tree | ||
self::$classStaticPropertyDuplicate => 'bar', // not applied - static property may be modified externally | ||
self::$classPrivateStaticPropertyDuplicate, // not applied - see above |
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.
copy comment to this line. It might not be clear where above.
self::$classStaticPropertyDuplicate => 'bar', // not applied - static property may be modified externally | ||
self::$classPrivateStaticPropertyDuplicate, // not applied - see above | ||
$this->classPrivatePropertyDuplicate => 'bar', // not applied - may be modified at any time | ||
$this->classProtectedPropertyDuplicate => 'bar', // not applied - see above |
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.
copy comment to this line. It might not be clear where above.
self::$classPrivateStaticPropertyDuplicate, // not applied - see above | ||
$this->classPrivatePropertyDuplicate => 'bar', // not applied - may be modified at any time | ||
$this->classProtectedPropertyDuplicate => 'bar', // not applied - see above | ||
$this->classPublicPropertyDuplicate => 'bar', // not applied - see above |
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.
copy comment to this line. It might not be clear where above.
$this->classPublicPropertyDuplicate => 'bar', // not applied - see above | ||
global_function_duplicate() => 'bar', // not applied - may be different depending on namespace | ||
GLOBAL_DUPLICATE => 'bar', // not applied - may be different depending on context | ||
"foo" => 'bar', // applied - checkpoint |
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.
checkpoint? I don't get this.
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.
I've added an additional duplicated key in the end, to check if after all the checks above, this one is not skipped.
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.
The comment itself is not clear enough. Please, try to rephrase it.
* @param ASTNode $node Array node. | ||
* @return void | ||
*/ | ||
private function analyzeArray(ASTNode $node) |
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.
Very general name. How about checkForDuplicatedArrayKeys
?
@eeree if we ever meet, remember me to buy you a drink 😼 |
Ignore the Stickler CI nags, created #487 for that... |
@ravage84 |
@@ -79,14 +79,19 @@ private function analyzeArray(ASTNode $node) | |||
} | |||
|
|||
/** | |||
* Sets normalized name as node's image. | |||
* To compare keys, we have to cast them to string. |
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.
There is no short description. It should always have a short description and if necessary a long description.
https://www.phpdoc.org/docs/latest/getting-started/your-first-set-of-documentation.html#example
src/site/rst/rules/cleancode.rst
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Done in d40a0f1
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.
Fix the last doc block and I think we are good 👍
🍸 |
Since this feature has not been released 1.5 years later: phpstan is also able to detect duplicate array keys. |
As #350 stuck for some time and also implementation was a bit buggy (nested arrays recursion issue and boolean/null array keys checks), I've decided to fix what's left.