SuperfluousComment   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 19
c 3
b 0
f 3
lcom 1
cbo 2
dl 0
loc 150
ccs 70
cts 70
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 16 4
A checkClass() 0 8 1
A checkProperties() 0 10 2
A checkMethods() 0 10 2
A checkNode() 0 6 2
A calculateNameToCommentSimilarityInPercent() 0 16 2
A getCommentDescription() 0 14 3
A transformDescriptionLines() 0 10 2
A transformString() 0 7 1
1
<?php
2
3
namespace MS\PHPMD\Rule\CleanCode;
4
5
use PDepend\Source\AST\ASTClass;
6
use PHPMD\AbstractNode;
7
use PHPMD\AbstractRule;
8
use PHPMD\Node\ClassNode;
9
use PHPMD\Rule\ClassAware;
10
use PDepend\Source\AST\AbstractASTArtifact;
11
12
/**
13
 * Class SuperfluousComment
14
 *
15
 * Don't write superfluous comments. It's adding by subtracting.
16
 *
17
 * @package MS\PHPMD\Rule\CleanCode
18
 */
19
class SuperfluousComment extends AbstractRule implements ClassAware
20
{
21
    /**
22
     * @var int
23
     */
24
    private $percent;
25
26
    /**
27
     * @param AbstractNode $node
28
     */
29 3
    public function apply(AbstractNode $node)
30
    {
31 3
        $this->percent = $this->getIntProperty('percent');
32
33 3
        if (true === $this->getBooleanProperty('checkClass')) {
34 3
            $this->checkClass($node);
35 3
        }
36
37 3
        if (true === $this->getBooleanProperty('checkProperties')) {
38 3
            $this->checkProperties($node);
39 3
        }
40
41 3
        if (true === $this->getBooleanProperty('checkMethods')) {
42 3
            $this->checkMethods($node);
43 3
        }
44 3
    }
45
46
    /**
47
     * @param AbstractNode $node
48
     */
49 3
    private function checkClass(AbstractNode $node)
50
    {
51 3
        $this->checkNode(
52 3
            $node,
53 3
            $node->getType(),
54 3
            $this->calculateNameToCommentSimilarityInPercent($node)
55 3
        );
56 3
    }
57
58
    /**
59
     * @param AbstractNode|ASTClass $node
60
     */
61 3
    private function checkProperties(AbstractNode $node)
62
    {
63 3
        foreach ($node->getProperties() as $property) {
64 1
            $this->checkNode(
65 1
                $node,
66 1
                'property ' . $property->getName(),
67 1
                $this->calculateNameToCommentSimilarityInPercent($property)
68 1
            );
69 3
        }
70 3
    }
71
72
    /**
73
     * @param AbstractNode|ClassNode $node
74
     */
75 3
    private function checkMethods(AbstractNode $node)
76
    {
77 3
        foreach ($node->getMethods() as $method) {
78 1
            $this->checkNode(
79 1
                $method,
80 1
                $method->getType(),
81 1
                $this->calculateNameToCommentSimilarityInPercent($method)
82 1
            );
83 3
        }
84 3
    }
85
86
    /**
87
     * @param AbstractNode $node
88
     * @param string       $type
89
     * @param int          $percent
90
     */
91 3
    private function checkNode(AbstractNode $node, $type, $percent)
92
    {
93 3
        if ($this->percent < $percent) {
94 2
            $this->addViolation($node, [$type, $percent]);
95 2
        }
96 3
    }
97
98
    /**
99
     * @param AbstractNode|AbstractASTArtifact $node
100
     *
101
     * @return float
102
     */
103 3
    private function calculateNameToCommentSimilarityInPercent($node)
104
    {
105 3
        $docComment = $node->getDocComment();
0 ignored issues
show
Bug introduced by
The method getDocComment does only exist in PDepend\Source\AST\AbstractASTArtifact, but not in PHPMD\AbstractNode.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
106
107 3
        if (empty($docComment)) {
108 2
            return 0;
109
        }
110
111 2
        similar_text(
112 2
            $this->transformString($node->getName()),
113 2
            $this->getCommentDescription($docComment),
114
            $percent
115 2
        );
116
117 2
        return round($percent);
118
    }
119
120
    /**
121
     * @param string $docComment
122
     *
123
     * @return string
124
     */
125 2
    private function getCommentDescription($docComment)
126
    {
127 2
        $lines = explode(PHP_EOL, $docComment);
128
129 2
        $descriptionLines = [];
130
131 2
        foreach ($lines as $line) {
132 2
            if (false === strpos($line, '@')) {
133 2
                $descriptionLines[] = $line;
134 2
            }
135 2
        }
136
137 2
        return $this->transformDescriptionLines($descriptionLines);
138
    }
139
140
    /**
141
     * @param array $descriptionLines
142
     *
143
     * @return string
144
     */
145 2
    private function transformDescriptionLines(array $descriptionLines)
146
    {
147 2
        $description = '';
148
149 2
        foreach ($descriptionLines as $line) {
150 2
            $description .= $this->transformString($line);
151 2
        }
152
153 2
        return $description;
154
    }
155
156
    /**
157
     * @param string $string
158
     *
159
     * @return string
160
     */
161 2
    private function transformString($string)
162
    {
163 2
        $string = strtolower($string);
164 2
        $string = preg_replace('/[^a-z0-9]/', '', $string);
165
166 2
        return $string;
167
    }
168
}
169