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
125 changes: 125 additions & 0 deletions src/main/php/PHPMD/Rule/CleanCode/DuplicatedArrayKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?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/
*/

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


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 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.

*
* @author Rafał Wrzeszcz <rafal.wrzeszcz@wrzasq.pl>
* @author Kamil Szymanaski <kamil.szymanski@gmail.com>
*/
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...

* with duplicated entries for any key and emits a rule violation if so.
*
* @param AbstractNode $node
* @return void
*/
public function apply(AbstractNode $node)
{
foreach ($node->findChildrenOfType('Array') as $arrayNode) {
/** @var ASTNode $arrayNode */
$this->analyzeArray($arrayNode);
}
}

/**
* Analyzes single array.
Copy link
Member

Choose a reason for hiding this comment

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

Says nothing. As this method does actually, what the doc block of apply() says, it should be moved here.

*
* @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?

{
$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();
Copy link
Member

Choose a reason for hiding this comment

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

One white line before

if (isset($keys[$key])) {
$this->addViolation($node, array($key, $arrayElement->getStartLine()));
continue;
}
$keys[$key] = $arrayElement;
}
}

/**
* Sets normalized name as node's image.
*
Copy link
Member

Choose a reason for hiding this comment

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

This method could benefit from a longer description, telling the what & why.

* @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)
{
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->stringFromLiteral($node));

return $node;
}

/**
* Cleans string literals and casts boolean and null values as PHP engine does
*
* @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

{
$value = $key->getImage();
switch ($value) {
case 'false':
return '0';
case 'true':
return '1';
case 'null':
return '';
default:
return trim($value, '\'""');
}
}
}
26 changes: 26 additions & 0 deletions src/main/resources/rulesets/cleancode.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,32 @@ class Foo
{
Bar::baz();
}
}
]]>
</example>
</rule>

<rule name="DuplicatedArrayKey"
message="Duplicated array entry for key {0}, first declared at line {1}."
Copy link
Member

Choose a reason for hiding this comment

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

Duplicated array key...

class="PHPMD\Rule\CleanCode\DuplicatedArrayKey"
externalInfoUrl="http://phpmd.org/rules/cleancode.html#duplicatedarraykey">
<description>
<![CDATA[
Defining another value for the same key in array literal overrides previous one,
Copy link
Member

Choose a reason for hiding this comment

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

"in an array literal overrides the previous key/value"

which makes it effectively an unused code. If it's known from the beginning that
Copy link
Member

Choose a reason for hiding this comment

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

The second sentence does not add much info, though... How could we add some? Otherwise remove.

the key will have different value, there is usually no point in defining first one.
]]>
</description>
<priority>2</priority>
<example>
<![CDATA[
function createArray() {
return [
'non-associative 0element', // not applied
0 => 'associative 0-element', // applied
'foo' => 'bar', // not applied
"foo" => 'baz', // applied
];
Copy link
Member

Choose a reason for hiding this comment

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

I'd like to see a boolean cast example, e.g. false -> 0.

}
]]>
</example>
Expand Down
23 changes: 21 additions & 2 deletions src/site/rst/rules/cleancode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ This rule has the following properties:
=================================== =============== ===============================================

IfStatementAssignment
=============
=====================

Since: PHPMD 2.7.0

Expand All @@ -91,10 +91,29 @@ Example: ::
}


DuplicateArrayKey
=================

Since: PHPMD 2.7.0

Defining another value for the same key in array literal overrides previous one, 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.
Copy link
Member

Choose a reason for hiding this comment

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

"in an array literal overrides the previous key/value"

Copy link
Member

Choose a reason for hiding this comment

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

Adjust second sentence here, too.



Example: ::

function createArray() {
return [
'non-associative 0element', // not applied
0 => 'associative 0-element', // applied
'foo' => 'bar', // not applied
"foo" => 'baz', // applied
];
}
Copy link
Member

Choose a reason for hiding this comment

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

Boolean cast example.


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


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

@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.

Why was this removed?

We might add the following to our .editorconfig file:

[*.rst]
insert_final_newline = true
trim_trailing_whitespace = true

Should not be part of the PR, though.

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

1 change: 0 additions & 1 deletion src/site/rst/rules/design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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/

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.

Why was this removed?

We might add the following to our .editorconfig file:

[*.rst]
insert_final_newline = true
trim_trailing_whitespace = true

Should not be part of the PR, though.