Issues (76)

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.

src/Parser/Parser.php (1 issue)

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
declare(strict_types=1);
4
5
namespace ParaTest\Parser;
6
7
class Parser
8
{
9
    /**
10
     * The path to the source file to parse.
11
     *
12
     * @var string
13
     */
14
    private $path;
15
16
    /**
17
     * @var \ReflectionClass
18
     */
19
    private $refl;
20
21
    /**
22
     * Matches a test method beginning with the conventional "test"
23
     * word.
24
     *
25
     * @var string
26
     */
27
    private static $testName = '/^test/';
28
29
    /**
30
     * A pattern for matching test methods that use the @test annotation.
31
     *
32
     * @var string
33
     */
34
    private static $testAnnotation = '/@test\b/';
35
36 31
    public function __construct(string $srcPath)
37
    {
38 31
        if (!file_exists($srcPath)) {
39 1
            throw new \InvalidArgumentException('file not found: ' . $srcPath);
40
        }
41
42 30
        $this->path = $srcPath;
43 30
        $declaredClasses = get_declared_classes();
44 30
        require_once $this->path;
45 30
        $class = $this->getClassName($this->path, $declaredClasses);
46 30
        if (!$class) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $class of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
47 2
            throw new NoClassInFileException();
48
        }
49
        try {
50 28
            $this->refl = new \ReflectionClass($class);
51
        } catch (\ReflectionException $e) {
52
            throw new \InvalidArgumentException('Unable to instantiate ReflectionClass. ' . $class . ' not found in: ' . $srcPath);
53
        }
54 28
    }
55
56
    /**
57
     * Returns the fully constructed class
58
     * with methods or null if the class is abstract.
59
     *
60
     * @return null|ParsedClass
61
     */
62 28
    public function getClass()
63
    {
64 28
        return ($this->refl->isAbstract())
65
            ? null
66 28
            : new ParsedClass(
67 28
                (string) $this->refl->getDocComment(),
68 28
                $this->getCleanReflectionName(),
69 28
                $this->refl->getNamespaceName(),
70 28
                $this->getMethods()
71
            );
72
    }
73
74
    /**
75
     * Return reflection name with null bytes stripped.
76
     *
77
     * @return string
78
     */
79 28
    private function getCleanReflectionName(): string
80
    {
81 28
        return str_replace("\x00", '', $this->refl->getName());
82
    }
83
84
    /**
85
     * Return all test methods present in the file.
86
     *
87
     * @return array
88
     */
89 28
    private function getMethods(): array
90
    {
91 28
        $tests = [];
92 28
        $methods = $this->refl->getMethods(\ReflectionMethod::IS_PUBLIC);
93 28
        foreach ($methods as $method) {
94 28
            $hasTestName = preg_match(self::$testName, $method->getName());
95 28
            $docComment = $method->getDocComment();
96 28
            $hasTestAnnotation = false !== $docComment && preg_match(self::$testAnnotation, $docComment);
97 28
            $isTestMethod = $hasTestName || $hasTestAnnotation;
98 28
            if ($isTestMethod) {
99 28
                $tests[] = new ParsedFunction((string) $method->getDocComment(), 'public', $method->getName());
100
            }
101
        }
102
103 28
        return $tests;
104
    }
105
106
    /**
107
     * Return the class name of the class contained
108
     * in the file.
109
     *
110
     * @param mixed $filename
111
     * @param mixed $previousDeclaredClasses
112
     *
113
     * @return string
114
     */
115 30
    private function getClassName(string $filename, array $previousDeclaredClasses)
116
    {
117 30
        $filename = realpath($filename);
118 30
        $classes = get_declared_classes();
119 30
        $newClasses = array_values(array_diff($classes, $previousDeclaredClasses));
120
121 30
        $className = $this->_searchForUnitTestClass($newClasses, $filename);
122 30
        if (isset($className)) {
123 12
            return $className;
124
        }
125
126 22
        $className = $this->_searchForUnitTestClass($classes, $filename);
127 22
        if (isset($className)) {
128 20
            return $className;
129
        }
130 2
    }
131
132
    /**
133
     * Search for the name of the unit test.
134
     *
135
     * @param string[] $classes
136
     * @param string   $filename
137
     *
138
     * @return string|null
139
     */
140 30
    private function _searchForUnitTestClass(array $classes, string $filename)
141
    {
142
        // TODO: After merging this PR or other PR for phpunit 6 support, keep only the applicable subclass name
143 30
        $matchingClassName = null;
144 30
        foreach ($classes as $className) {
145 30
            $class = new \ReflectionClass($className);
146 30
            if ($class->getFileName() === $filename) {
147 28
                if ($class->isSubclassOf('PHPUnit\Framework\TestCase')) {
148 28
                    if ($this->classNameMatchesFileName($filename, $className)) {
149 21
                        return $className;
150 18
                    } elseif ($matchingClassName === null) {
151 27
                        $matchingClassName = $className;
152
                    }
153
                }
154
            }
155
        }
156
157 25
        return $matchingClassName;
158
    }
159
160
    /**
161
     * @param $filename
162
     * @param $className
163
     *
164
     * @return bool
165
     */
166 28
    private function classNameMatchesFileName(string $filename, string $className): bool
167
    {
168 28
        return strpos($filename, $className) !== false
169 28
            || strpos($filename, $this->invertSlashes($className)) !== false;
170
    }
171
172 18
    private function invertSlashes(string $className): string
173
    {
174 18
        return str_replace('\\', '/', $className);
175
    }
176
}
177