Issues (7)

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/ClassHelper.php (3 issues)

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 Hgraca\Helper;
4
5
use Hgraca\Helper\Concept\ReflectionHelperAbstract;
6
use ReflectionClass;
7
use ReflectionException;
8
use ReflectionMethod;
9
use ReflectionProperty;
10
11
final class ClassHelper extends ReflectionHelperAbstract
12
{
13 1
    public static function extractCanonicalClassName(string $classFqcn): string
14
    {
15 1
        return substr($classFqcn, strrpos($classFqcn, '\\') + 1);
16
    }
17
18
    /**
19
     * Returns a key-value array with all constants in the class
20
     * It uses Reflection to find them
21
     *
22
     * @param string $classFqcn
23
     *
24
     * @return array
25
     */
26 1
    public static function findConstants(string $classFqcn): array
27
    {
28 1
        $reflectionClass = self::getReflectionClass($classFqcn);
29
30 1
        return $reflectionClass->getConstants();
31
    }
32
33
    /**
34
     * Returns an array with all properties names of the mixed
35
     *
36
     * @param string   $classFqcn
37
     * @param string[] $excludedNames
38
     * @param string[] $excludedVisibility = ['public', 'protected', 'private', 'static']
39
     *
40
     * @return string[]
41
     */
42 4
    public static function findPropertiesNames(
43
        string $classFqcn,
44
        array $excludedNames = [],
45
        array $excludedVisibility = []
46
    ) {
47 4
        $defaultVisibility = ['public' => true, 'protected' => true, 'private' => true, 'static' => true];
48 4
        $excludedVisibility = array_merge(
49
            $defaultVisibility,
50 4
            self::setGivenExcludedVisivilitiesToKeysWithValueFalse($excludedVisibility)
51
        );
52
53 4
        $reflectionClass = self::getReflectionClass($classFqcn);
54 4
        $reflectionPropertiesArray = $reflectionClass->getProperties();
55
56 4
        return self::filterProperties($excludedNames, $excludedVisibility, $reflectionPropertiesArray);
57
    }
58
59 5
    public static function hasMethod(string $classFqcn, string $method): bool
60
    {
61 5
        $reflectionClass = self::getReflectionClass($classFqcn);
62
63 5
        return $reflectionClass->hasMethod($method);
64
    }
65
66
    /**
67
     * @param string $classFqcn
68
     * @param string $method
69
     *
70
     * @return array [index => [name, class]]
71
     */
72 2 View Code Duplication
    public static function getParameters(string $classFqcn, string $method): array
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        try {
75 2
            $reflectionMethod = new ReflectionMethod($classFqcn, $method);
76
        } catch (ReflectionException $e) {
77
            return [];
78
        }
79
80 2
        foreach ($reflectionMethod->getParameters() as $index => $param) {
81 1
            $reflectionParameters[$index]['name'] = $param->getName();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$reflectionParameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $reflectionParameters = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
82 1
            if (null !== $param->getClass()) {
83 1
                $reflectionParameters[$index]['class'] = $param->getClass()->name;
84
            }
85
        }
86
87 2
        return $reflectionParameters ?? [];
88
    }
89
90
    /**
91
     * @return ReflectionProperty[]
92
     */
93 10
    public static function getReflectionProperties(string $classFqcn): array
94
    {
95 10
        $reflectionClass = self::getReflectionClass($classFqcn);
96
97 10
        return $reflectionClass->getProperties();
98
    }
99
100
    /**
101
     * @param ReflectionProperty[] $propertyList
102
     */
103 4
    public static function setReflectionPropertiesAccessible(array &$propertyList)
104
    {
105 4
        foreach ($propertyList as $reflectionProperty) {
106 4
            $reflectionProperty->setAccessible(true);
107
        }
108 4
    }
109
110
    /**
111
     * Gets the property from the current class, when not available will look on parent classes before failing
112
     *
113
     * @throws ReflectionException
114
     *
115
     * @return ReflectionProperty
116
     */
117 6
    public static function getReflectionProperty(ReflectionClass $class, string $propertyName): ReflectionProperty
118
    {
119
        try {
120 6
            return $class->getProperty($propertyName);
121 4
        } catch (ReflectionException $e) {
122 4
            $parentClass = $class->getParentClass();
123 4
            if ($parentClass === false) {
124 2
                throw $e;
125
            }
126
127 4
            return self::getReflectionProperty($parentClass, $propertyName);
128
        }
129
    }
130
131 4
    private static function excludeProperty(
132
        array $excludedNames,
133
        array $excludedVisibility,
134
        ReflectionProperty $reflectionProperty
135
    ): bool {
136 4
        return in_array($reflectionProperty->getName(), $excludedNames) ||
137 4
        (!$excludedVisibility['public'] && $reflectionProperty->isPublic()) ||
138 4
        (!$excludedVisibility['protected'] && $reflectionProperty->isProtected()) ||
139 4
        (!$excludedVisibility['private'] && $reflectionProperty->isPrivate()) ||
140 4
        (!$excludedVisibility['static'] && $reflectionProperty->isStatic());
141
    }
142
143
    /**
144
     * @param string[] $excludedNames
145
     * @param bool[] $excludedVisibility
146
     * @param ReflectionProperty[] $reflectionPropertiesArray
147
     *
148
     * @return string[]
149
     */
150 4
    private static function filterProperties(
151
        array $excludedNames,
152
        array $excludedVisibility,
153
        array $reflectionPropertiesArray
154
    ) {
155 4
        $propertyArray = [];
156
157
        /** @var ReflectionProperty $reflectionProperty */
158 4
        foreach ($reflectionPropertiesArray as $reflectionProperty) {
159 4
            if (self::excludeProperty($excludedNames, $excludedVisibility, $reflectionProperty)) {
160 3
                continue;
161
            }
162
163 4
            $reflectionProperty->setAccessible(true);
164 4
            $propertyArray[] = $reflectionProperty->getName();
165
        }
166
167 4
        return $propertyArray;
168
    }
169
170
    /**
171
     * @param array $excludedVisibility
172
     *
173
     * @return array
174
     */
175 4
    private static function setGivenExcludedVisivilitiesToKeysWithValueFalse(array $excludedVisibility)
176
    {
177 4
        return array_fill_keys(array_keys(array_flip($excludedVisibility)), false);
178
    }
179
}
180