GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

calculateNameToCommentSimilarityInPercent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0054
1
<?php
2
3
namespace MS\PHPMD\Rule\Naming;
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
 * Don't write superfluous comments. It's adding by subtracting.
14
 */
15
class CommentDescription extends AbstractRule implements ClassAware
16
{
17
    /**
18
     * @var int
19
     */
20
    private $percent;
21
22
    /**
23
     * @param AbstractNode $node
24
     */
25 4
    public function apply(AbstractNode $node)
26
    {
27 4
        $this->percent = $this->getIntProperty('percent');
28
29 4
        if (true === $this->getBooleanProperty('checkClass')) {
30 4
            $this->checkClass($node);
31 4
        }
32
33 4
        if (true === $this->getBooleanProperty('checkProperties')) {
34 4
            $this->checkProperties($node);
35 4
        }
36
37 4
        if (true === $this->getBooleanProperty('checkMethods')) {
38 4
            $this->checkMethods($node);
39 4
        }
40 4
    }
41
42
    /**
43
     * @param AbstractNode $node
44
     */
45 4
    private function checkClass(AbstractNode $node)
46
    {
47 4
        $this->checkNode(
48 4
            $node,
49 4
            $node->getType(),
50 4
            $this->calculateNameToCommentSimilarityInPercent($node)
51 4
        );
52 4
    }
53
54
    /**
55
     * @param AbstractNode|ASTClass $node
56
     */
57 4
    private function checkProperties(AbstractNode $node)
58
    {
59 4
        foreach ($node->getProperties() as $property) {
60 2
            $this->checkNode(
61 2
                $node,
62 2
                'property ' . $property->getName(),
63 2
                $this->calculateNameToCommentSimilarityInPercent($property)
64 2
            );
65 4
        }
66 4
    }
67
68
    /**
69
     * @param AbstractNode|ClassNode $node
70
     */
71 4
    private function checkMethods(AbstractNode $node)
72
    {
73 4
        foreach ($node->getMethods() as $method) {
74 4
            $this->checkNode(
75 4
                $method,
76 4
                $method->getType(),
77 4
                $this->calculateNameToCommentSimilarityInPercent($method)
78 4
            );
79 4
        }
80 4
    }
81
82
    /**
83
     * @param AbstractNode $node
84
     * @param string       $type
85
     * @param int          $percent
86
     */
87 4
    private function checkNode(AbstractNode $node, $type, $percent)
88
    {
89 4
        if ($this->percent < $percent) {
90 4
            $this->addViolation($node, [$type, $percent]);
91 4
        }
92 4
    }
93
94
    /**
95
     * @param AbstractNode|AbstractASTArtifact $node
96
     *
97
     * @return float
98
     */
99 4
    private function calculateNameToCommentSimilarityInPercent($node)
100
    {
101 4
        $docComment = $node->getComment();
0 ignored issues
show
Bug introduced by
The method getComment 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...
102
103 4
        if (empty($docComment)) {
104
            return 0;
105
        }
106
107 4
        similar_text(
108 4
            $this->transformString($node->getName()),
109 4
            $this->getCommentDescription($docComment),
110
            $percent
111 4
        );
112
113 4
        return round($percent);
114
    }
115
116
    /**
117
     * @param string $docComment
118
     *
119
     * @return string
120
     */
121 4
    private function getCommentDescription($docComment)
122
    {
123 4
        $lines = explode(PHP_EOL, $docComment);
124
125 4
        $descriptionLines = [];
126
127 4
        foreach ($lines as $line) {
128 4
            if (false === strpos($line, '@')) {
129 4
                $descriptionLines[] = $line;
130 4
            }
131 4
        }
132
133 4
        return $this->transformDescriptionLines($descriptionLines);
134
    }
135
136
    /**
137
     * @param array $descriptionLines
138
     *
139
     * @return string
140
     */
141 4
    private function transformDescriptionLines(array $descriptionLines)
142
    {
143 4
        $description = '';
144
145 4
        foreach ($descriptionLines as $line) {
146 4
            $description .= $this->transformString($line);
147 4
        }
148
149 4
        return $description;
150
    }
151
152
    /**
153
     * @param string $string
154
     *
155
     * @return string
156
     */
157 4
    private function transformString($string)
158
    {
159 4
        $string = strtolower($string);
160 4
        $string = preg_replace('/[^a-z0-9]/', '', $string);
161
162 4
        return $string;
163
    }
164
}
165