Issues (114)

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/System/EntityBuilder.php (5 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 Analogue\ORM\System;
4
5
use Analogue\ORM\System\Wrappers\Factory;
6
use Analogue\ORM\System\Proxies\EntityProxy;
7
use Analogue\ORM\System\Proxies\CollectionProxy;
8
9
/**
10
 * This class builds an array of Entity object(s) from a result set.
11
 */
12
class EntityBuilder
13
{
14
    /**
15
     * The mapper for the entity to build
16
     * @var \Analogue\ORM\System\Mapper
17
     */
18
    protected $mapper;
19
20
    /**
21
     * The Entity Map for the entity to build.
22
     *
23
     * @var \Analogue\ORM\EntityMap
24
     */
25
    protected $entityMap;
26
27
    /**
28
     * Relations that will be eager loaded on this query
29
     *
30
     * @var array
31
     */
32
    protected $eagerLoads;
33
34
    /**
35
     * Relations that will be lazy loaded on this query
36
     *
37
     * @var array
38
     */
39
    protected $lazyLoads;
40
41
    /**
42
     * Entity Wrapper Factory
43
     * @var \Analogue\ORM\System\Wrappers\Factory
44
     */
45
    protected $factory;
46
47
    /**
48
     * EntityBuilder constructor.
49
     * @param Mapper $mapper
50
     * @param array  $eagerLoads
51
     */
52
    public function __construct(Mapper $mapper, array $eagerLoads)
53
    {
54
        $this->mapper = $mapper;
55
56
        $this->entityMap = $mapper->getEntityMap();
57
58
        $this->eagerLoads = $eagerLoads;
59
60
        $this->lazyLoads = $this->prepareLazyLoading();
61
62
        $this->entityMap = $mapper->getEntityMap();
63
64
        $this->factory = new Factory;
65
    }
66
67
    /**
68
     * Convert a result set into an array of entities
69
     *
70
     * @param  array $results
71
     * @return array
72
     */
73
    public function build(array $results)
74
    {
75
        $entities = [];
76
77
        //$prototype = $this->getWrapperPrototype();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
        //$prototype = $this->mapper->newInstance();
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
80
        $keyName = $this->entityMap->getKeyName();
81
82
        $tmpCache = [];
83
84
        foreach ($results as $result) {
85
            //$instance = clone $prototype;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
            $instance = $this->getWrapperInstance();
87
88
            $resultArray = (array) $result;
89
90
            $tmpCache[$resultArray[$keyName]] = $resultArray;
91
92
            // Hydrate any embedded Value Object
93
            $this->hydrateValueObjects($resultArray);
94
95
            $instance->setEntityAttributes($resultArray);
96
97
            // Hydrate relation attributes with lazyloading proxies
98
            if (count($this->lazyLoads) > 0) {
99
                $proxies = $this->getLazyLoadingProxies($instance);
100
                $instance->setEntityAttributes($resultArray + $proxies);
101
            }
102
103
            // Directly Unwrap the entity now that it has been hydrated
104
            $entities[] = $instance->getObject();
105
        }
106
107
        $this->mapper->getEntityCache()->add($tmpCache);
108
109
        return $entities;
110
    }
111
112
    /**
113
     * Get the correct wrapper prototype corresponding to the object type
114
     *
115
     * @throws \Analogue\ORM\Exceptions\MappingException
116
     * @return InternallyMappable
117
     */
118
    protected function getWrapperInstance()
119
    {
120
        return $this->factory->make($this->mapper->newInstance());
121
    }
122
123
    /**
124
     * Hydrate value object embedded in this entity
125
     *
126
     * @param  array $attributes
127
     * @throws \Analogue\ORM\Exceptions\MappingException
128
     * @return void
129
     */
130
    protected function hydrateValueObjects(& $attributes)
131
    {
132
        foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
133
            $this->hydrateValueObject($attributes, $localKey, $valueClass);
134
        }
135
    }
136
137
    /**
138
     * Hydrate a single value object
139
     *
140
     * @param  array  $attributes
141
     * @param  string $localKey
142
     * @param  string $valueClass
143
     * @throws \Analogue\ORM\Exceptions\MappingException
144
     * @return void
145
     */
146
    protected function hydrateValueObject(& $attributes, $localKey, $valueClass)
147
    {
148
        $map = $this->mapper->getManager()->getValueMap($valueClass);
149
150
        $embeddedAttributes = $map->getAttributes();
151
152
        $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
153
154
        foreach ($embeddedAttributes as $key) {
155
            $prefix = snake_case(class_basename($valueClass)) . '_';
156
157
            $voWrapper = $this->factory->make($valueObject);
158
159
            $voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]);
160
            
161
            unset($attributes[$prefix . $key]);
162
        }
163
        
164
        $attributes[$localKey] = $valueObject;
165
    }
166
167
    /**
168
     * Deduce the relationships that will be lazy loaded from the eagerLoads array
169
     *
170
     * @return array
171
     */
172
    protected function prepareLazyLoading()
173
    {
174
        $relations = $this->entityMap->getRelationships();
175
       
176
        return array_diff($relations, $this->eagerLoads);
177
    }
178
179
    /**
180
     * Build lazy loading proxies for the current entity
181
     *
182
     * @param InternallyMappable $entity
183
     *
184
     * @return array
185
     */
186
    protected function getLazyLoadingProxies(InternallyMappable $entity)
187
    {
188
        $proxies = [];
189
190
        $singleRelations = $this->entityMap->getSingleRelationships();
191
        $manyRelations = $this->entityMap->getManyRelationships();
192
193
        foreach ($this->lazyLoads as $relation) {
194
            if (in_array($relation, $singleRelations)) {
195
                $proxies[$relation] = new EntityProxy($entity->getObject(), $relation);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Analogue\ORM\System\InternallyMappable as the method getObject() does only exist in the following implementations of said interface: Analogue\ORM\System\Wrappers\EntityWrapper, Analogue\ORM\System\Wrappers\PlainObjectWrapper, Analogue\ORM\System\Wrappers\Wrapper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
196
            }
197
            if (in_array($relation, $manyRelations)) {
198
                $proxies[$relation] = new CollectionProxy($entity->getObject(), $relation);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Analogue\ORM\System\InternallyMappable as the method getObject() does only exist in the following implementations of said interface: Analogue\ORM\System\Wrappers\EntityWrapper, Analogue\ORM\System\Wrappers\PlainObjectWrapper, Analogue\ORM\System\Wrappers\Wrapper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
199
            }
200
        }
201
        
202
        return $proxies;
203
    }
204
}
205