This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
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
|
|||
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. ![]() |
|||
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. ![]() |
|||
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
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
![]() |
|||
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
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
![]() |
|||
199 | } |
||
200 | } |
||
201 | |||
202 | return $proxies; |
||
203 | } |
||
204 | } |
||
205 |
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.