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

Feature/duplicated array keys #484

Merged
merged 9 commits into from Jun 11, 2017

Conversation

eeree
Copy link
Contributor

@eeree eeree commented Jun 5, 2017

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.

* @link http://phpmd.org/
*/

class testRuleAppliesMultipleTimesToMethodWithAssotiativeArrayDefinitionWithDuplicatedKeys

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

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

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

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

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';

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@ravage84 ravage84 self-assigned this Jun 6, 2017
@ravage84 ravage84 added this to the 2.7.0 milestone Jun 6, 2017
Copy link
Member

@ravage84 ravage84 left a 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()
Copy link
Member

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/
*/

Copy link
Member

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.
Copy link
Member

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
Copy link
Member

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.

Copy link
Member

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)
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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)
Copy link
Member

@ravage84 ravage84 Jun 6, 2017

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?

@ravage84
Copy link
Member

ravage84 commented Jun 6, 2017

@eeree if we ever meet, remember me to buy you a drink 😼

@ravage84
Copy link
Member

ravage84 commented Jun 6, 2017

Ignore the Stickler CI nags, created #487 for that...

@eeree
Copy link
Contributor Author

eeree commented Jun 11, 2017

@ravage84
#484 (review) done

ravage84 added a commit that referenced this pull request Jun 11, 2017
@@ -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.
Copy link
Member

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

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/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in d40a0f1

Copy link
Member

@ravage84 ravage84 left a 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 👍

@ravage84 ravage84 merged commit a295850 into phpmd:master Jun 11, 2017
@ravage84
Copy link
Member

🍸

@cweiske
Copy link

cweiske commented Jan 21, 2019

Since this feature has not been released 1.5 years later: phpstan is also able to detect duplicate array keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

None yet

5 participants