Issues (4)

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/Representer.php (4 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
namespace einfach\representer;
3
4
use einfach\representer\serializer\ArraySerializer;
5
6
/**
7
 * Trait Representer
8
 *
9
 * @package einfach\representer
10
 */
11
trait Representer
12
{
13
    use ArraySerializer;
14
15
    /**
16
     * Object that is being represented
17
     * or a collection handler
18
     */
19
    protected $source;
20
    /**
21
     * Class name to be restored
22
     *
23
     * @var string
24
     */
25
    protected $targetClassName;
26
    /**
27
     * Strategy indicator
28
     * 1 = one
29
     * 2 = collection
30
     * 3 = restore one
31
     * 4 = restore collection
32
     *
33
     * @var string
34
     */
35
    protected $strategy;
36
37 10
    public function __construct($source, $strategy)
38
    {
39 10
        if (is_null($strategy)) {
40
            throw new \Exception('Representer can not be initialized without a strategy param');
41
        }
42
43 10
        $this->source = $source;
44 10
        $this->strategy = $strategy;
45 10
    }
46
47
    public function rules()
48
    {
49
        return [];
50
    }
51
52
    /**
53
     * Return property name to wrap a collection representation
54
     * If `null` - no wrapper added
55
     *
56
     * @return null | string
57
     */
58 2
    public function collectionWrapper()
59
    {
60 2
        return null;
61
    }
62
63 5
    public function setTargetClassName($name)
64
    {
65 5
        $this->targetClassName = $name;
66 5
    }
67
68
    /**
69
     * @param $name
70
     * @return PropertyRule
71
     */
72 10
    public function property($name)
73
    {
74 10
        return new PropertyRule($this->source, $name);
75
    }
76
77
    /**
78
     * Represent one instance
79
     *
80
     * @param $source
81
     * @return static
82
     */
83 8
    public static function one($source)
84
    {
85 8
        return new static($source, 1);
86
    }
87
88
    /**
89
     * Represent collection of instances
90
     *
91
     * @param array $array
92
     * @return static
93
     */
94 4
    public static function collection(array $array)
95
    {
96 4
        return new static($array, 2);
97
    }
98
99 4
    protected function getCollectionRepresentation()
100
    {
101 4
        if (is_array($this->source) && count($this->source) > 0) {
102
            $representation = array_map(function ($object) {
103 4
                return static::one($object)->getOneRepresentation();
104 4
            }, $this->source);
105
106 4
            if ($wrapperName = $this->collectionWrapper()) {
0 ignored issues
show
Are you sure the assignment to $wrapperName is correct as $this->collectionWrapper() (which targets einfach\representer\Repr...er::collectionWrapper()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
107 2
                return [$wrapperName => $representation];
108
            } else {
109 2
                return $representation;
110
            }
111
        }
112
    }
113
114 8 View Code Duplication
    protected function getOneRepresentation()
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...
115
    {
116 8
        $rules = $this->rules();
117 8
        if (empty($rules)) {
118
            throw new \Exception("There are rules specified in " . static::class . " representer");
119
        }
120
121 8
        $represented = [];
122
123 8
        foreach ($rules as $rule) {
124
            /** @var $rule PropertyRule */
125 8
            $resultArray = $rule->compile();
126
127 8
            reset($resultArray);
128 8
            $key = key($resultArray);
129 8
            $value = $resultArray[$key];
130
131 8
            $represented[$key] = $value;
132 8
        }
133
134 8
        return $represented;
135
    }
136
137 8
    protected function getRepresentation()
138
    {
139 8
        switch ($this->strategy) {
140 8
            case 1:
141 4
                return $this->getOneRepresentation();
142 4
            case 2:
143 4
                return $this->getCollectionRepresentation();
144
            default:
145
                throw new \Exception('Wrong representation strategy selected. Maybe you have accidentally
146
                called `toJSON` instead of `fromJSON`?');
147
        }
148
149
    }
150
151
152
    /**
153
     * @param $className
154
     * @return static
155
     */
156 5
    public static function restore($className)
157
    {
158 5
        $instance = new static(null, 3);
159 5
        $instance->setTargetClassName($className);
160 5
        return $instance;
161
    }
162
163
    /**
164
     * @param string $className
165
     * @return static
166
     */
167 2
    public static function restoreCollection($className)
168
    {
169 2
        $instance = new static(null, 4);
170 2
        $instance->setTargetClassName($className);
171 2
        return $instance;
172
    }
173
174 5
    protected function getReverseRepresentation($projection)
175
    {
176 5
        switch ($this->strategy) {
177 5
            case 3:
178 3
                return $this->getOneReverseRepresentation($projection);
179 2
            case 4:
180 2
                return $this->getCollectionReverseRepresentation($projection);
181
            default:
182
                throw new \Exception('Reverse representation strategy not defined');
183
        }
184
185
    }
186
187 5 View Code Duplication
    protected function getOneReverseRepresentation($projection)
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...
188
    {
189 5
        $rules = $this->rules();
190 5
        if (empty($rules)) {
191
            throw new \Exception("There are rules specified in " . static::class . " representer");
192
        }
193
194 5
        $target = new $this->targetClassName();
195
196 5
        foreach ($rules as $rule) {
197
            /** @var $rule PropertyRule */
198 5
            $resultArray = $rule->reverseCompile($projection);
199 5
            reset($resultArray);
200 5
            $key = key($resultArray);
201 5
            $value = $resultArray[$key];
202
203 5
            $target->$key = $value;
204 5
        }
205
206 5
        return $target;
207
    }
208
209 2
    protected function getCollectionReverseRepresentation($projectionArray)
210
    {
211 2
        if ($wrapperName = $this->collectionWrapper()) {
0 ignored issues
show
Are you sure the assignment to $wrapperName is correct as $this->collectionWrapper() (which targets einfach\representer\Repr...er::collectionWrapper()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
212 1
            if (!isset($projectionArray[$wrapperName])) {
213
                $siblingKeys = join(',', array_keys($projectionArray));
214
                throw new \Exception("Collection wrapper `{$wrapperName}` not found during restore
215
                (instead following keys found: {$siblingKeys} ). In " . static::class . " representer");
216
            }
217 1
            $projectionArray = $projectionArray[$wrapperName];
218 1
        }
219
220 2
        if (is_array($projectionArray) && count($projectionArray) > 0) {
221 2
            return array_map(function ($projection) {
222 2
                return static::restore($this->targetClassName)->getOneReverseRepresentation($projection);
223 2
            }, $projectionArray);
224
        }
225
    }
226
}
227