Issues (38)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

Rule/CleanCode/SuperfluousComment.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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