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 Oro\Bundle\AddressBundle\Provider; |
||
4 | |||
5 | use Doctrine\Common\Collections\Collection; |
||
6 | use Doctrine\Common\Util\ClassUtils; |
||
7 | |||
8 | use Symfony\Component\PropertyAccess\PropertyAccess; |
||
9 | |||
10 | use Oro\Bundle\EntityConfigBundle\Config\Id\FieldConfigId; |
||
11 | use Oro\Bundle\EntityConfigBundle\Provider\ConfigProvider; |
||
12 | |||
13 | /** |
||
14 | * The aim of this class is to help getting a phone number from an object. |
||
15 | * The following algorithm is used to get a phone number: |
||
16 | * 1. check if an object has own phone number |
||
17 | * 2. loop through registered target entities ordered by priority and check if they have a phone number |
||
18 | * |
||
19 | * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) |
||
20 | */ |
||
21 | class PhoneProvider implements PhoneProviderInterface |
||
22 | { |
||
23 | const GET_PHONE_METHOD = 'getPhone'; |
||
24 | |||
25 | /** @var ConfigProvider */ |
||
26 | protected $extendConfigProvider; |
||
27 | |||
28 | /** |
||
29 | * @var string[] |
||
30 | */ |
||
31 | protected $targetEntities = []; |
||
32 | |||
33 | /** |
||
34 | * @var string[] |
||
35 | */ |
||
36 | protected $sortedTargetEntities; |
||
37 | |||
38 | /** |
||
39 | * @var array |
||
40 | * key = class name, value = PhoneProviderInterface[] |
||
41 | */ |
||
42 | protected $phoneProviders = []; |
||
43 | |||
44 | /** |
||
45 | * @param ConfigProvider $extendConfigProvider |
||
46 | */ |
||
47 | public function __construct(ConfigProvider $extendConfigProvider) |
||
48 | { |
||
49 | $this->extendConfigProvider = $extendConfigProvider; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Registers the entity in supported target entities list |
||
54 | * |
||
55 | * @param string $className |
||
56 | * @param integer $priority |
||
57 | */ |
||
58 | public function addTargetEntity($className, $priority = 0) |
||
59 | { |
||
60 | $this->targetEntities[$priority][] = $className; |
||
61 | $this->sortedTargetEntities = null; |
||
0 ignored issues
–
show
|
|||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Registers the phone number provider for the given class |
||
66 | * |
||
67 | * @param string $className |
||
68 | * @param PhoneProviderInterface $provider |
||
69 | */ |
||
70 | public function addPhoneProvider($className, PhoneProviderInterface $provider) |
||
71 | { |
||
72 | if ($provider instanceof RootPhoneProviderAwareInterface) { |
||
73 | $provider->setRootProvider($this); |
||
74 | } |
||
75 | $this->phoneProviders[$className][] = $provider; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Gets the phone number of the given object |
||
80 | * |
||
81 | * @param object $object |
||
82 | * |
||
83 | * @return string|null The phone number or null if the object has no phone |
||
84 | */ |
||
85 | public function getPhoneNumber($object) |
||
86 | { |
||
87 | if (!is_object($object)) { |
||
88 | return null; |
||
89 | } |
||
90 | |||
91 | // check if an object has own phone number |
||
92 | $phoneProviders = $this->getPhoneProviders($object); |
||
93 | if ($phoneProviders) { |
||
94 | $phone = null; |
||
95 | /** @var PhoneProviderInterface $provider */ |
||
96 | foreach ($phoneProviders as $provider) { |
||
97 | $phone = $provider->getPhoneNumber($object); |
||
98 | if (!empty($phone)) { |
||
99 | break; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | return $phone; |
||
104 | } |
||
105 | |||
106 | if (method_exists($object, self::GET_PHONE_METHOD)) { |
||
107 | $phone = $object->getPhone(); |
||
108 | if (!is_object($phone)) { |
||
109 | return $phone; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | // check if an object has related object with a phone number |
||
114 | return $this->getPhoneNumberFromRelatedObject($object); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Gets all available phone numbers of the given object |
||
119 | * |
||
120 | * @param object $object |
||
121 | * |
||
122 | * @return array of phone number, phone owner |
||
123 | */ |
||
124 | public function getPhoneNumbers($object) |
||
125 | { |
||
126 | if (!is_object($object)) { |
||
127 | return []; |
||
128 | } |
||
129 | |||
130 | // check if an object has own phone number |
||
131 | $phoneProviders = $this->getPhoneProviders($object); |
||
132 | if ($phoneProviders) { |
||
133 | $phones = []; |
||
134 | /** @var PhoneProviderInterface $provider */ |
||
135 | foreach ($phoneProviders as $provider) { |
||
136 | $phones = $this->mergePhoneNumbers($phones, $provider->getPhoneNumbers($object)); |
||
137 | } |
||
138 | |||
139 | return $phones; |
||
140 | } |
||
141 | |||
142 | if (method_exists($object, self::GET_PHONE_METHOD)) { |
||
143 | $phone = $object->{self::GET_PHONE_METHOD}(); |
||
144 | if ($phone && !is_object($phone)) { |
||
145 | return [[$phone, $object]]; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | // check if an object has related object with a phone number |
||
150 | return $this->getPhoneNumbersFromRelatedObject($object); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param object $object |
||
155 | * |
||
156 | * @return PhoneProviderInterface[] |
||
157 | */ |
||
158 | protected function getPhoneProviders($object) |
||
159 | { |
||
160 | $className = ClassUtils::getClass($object); |
||
161 | $result = isset($this->phoneProviders[$className]) ? $this->phoneProviders[$className] : []; |
||
162 | foreach ($this->phoneProviders as $class => $providers) { |
||
163 | if (is_subclass_of($className, $class)) { |
||
164 | $result = array_merge($result, $providers); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | return $result; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param object $object |
||
173 | * |
||
174 | * @return string|null |
||
175 | */ |
||
176 | View Code Duplication | protected function getPhoneNumberFromRelatedObject($object) |
|
177 | { |
||
178 | $applicableRelations = $this->getApplicableRelations($object); |
||
179 | if (empty($applicableRelations)) { |
||
180 | return null; |
||
181 | } |
||
182 | |||
183 | $targetEntities = $this->getTargetEntities(); |
||
184 | $propertyAccessor = PropertyAccess::createPropertyAccessor(); |
||
185 | foreach ($targetEntities as $className) { |
||
186 | if (!isset($applicableRelations[$className])) { |
||
187 | continue; |
||
188 | } |
||
189 | foreach ($applicableRelations[$className] as $fieldName) { |
||
190 | return $this->getPhoneNumber($propertyAccessor->getValue($object, $fieldName)); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | return null; |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * @param object $object |
||
199 | * |
||
200 | * @return array of phone number, phone owner |
||
201 | */ |
||
202 | protected function getPhoneNumbersFromRelatedObject($object) |
||
203 | { |
||
204 | $applicableRelations = $this->getApplicableRelations($object, true); |
||
205 | if (empty($applicableRelations)) { |
||
206 | return []; |
||
207 | } |
||
208 | |||
209 | $result = []; |
||
210 | $targetEntities = $this->getTargetEntities(); |
||
211 | $propertyAccessor = PropertyAccess::createPropertyAccessor(); |
||
212 | foreach ($targetEntities as $className) { |
||
213 | if (!isset($applicableRelations[$className])) { |
||
214 | continue; |
||
215 | } |
||
216 | foreach ($applicableRelations[$className] as $fieldName) { |
||
217 | $value = $propertyAccessor->getValue($object, $fieldName); |
||
218 | if (is_array($value) || $value instanceof Collection) { |
||
219 | foreach ($value as $val) { |
||
220 | $result = $this->mergePhoneNumbers($result, $this->getPhoneNumbers($val)); |
||
221 | } |
||
222 | } else { |
||
223 | $result = $this->mergePhoneNumbers($result, $this->getPhoneNumbers($value)); |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | |||
228 | return $result; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param object $object |
||
233 | * @param bool $withMultiValue |
||
234 | * |
||
235 | * @return array |
||
236 | */ |
||
237 | protected function getApplicableRelations($object, $withMultiValue = false) |
||
238 | { |
||
239 | $result = []; |
||
240 | |||
241 | $className = ClassUtils::getClass($object); |
||
242 | if (!$this->extendConfigProvider->hasConfig($className)) { |
||
243 | return $result; |
||
244 | } |
||
245 | $extendConfig = $this->extendConfigProvider->getConfig($className); |
||
246 | $relations = $extendConfig->get('relation'); |
||
247 | if (empty($relations)) { |
||
248 | return $result; |
||
249 | } |
||
250 | |||
251 | $targetEntities = $this->getTargetEntities(); |
||
252 | foreach ($relations as $relation) { |
||
253 | if (empty($relation['owner'])) { |
||
254 | continue; |
||
255 | } |
||
256 | /** @var FieldConfigId $fieldId */ |
||
257 | $fieldId = $relation['field_id']; |
||
258 | |||
259 | $isApplicableRelationType = |
||
260 | $fieldId->getFieldType() === 'manyToOne' |
||
261 | || ($withMultiValue && $fieldId->getFieldType() === 'manyToMany'); |
||
262 | if (!$isApplicableRelationType) { |
||
263 | continue; |
||
264 | } |
||
265 | $relatedEntityClass = $relation['target_entity']; |
||
266 | if (!in_array($relatedEntityClass, $targetEntities)) { |
||
267 | continue; |
||
268 | } |
||
269 | if (!isset($result[$relatedEntityClass])) { |
||
270 | $result[$relatedEntityClass] = []; |
||
271 | } |
||
272 | $result[$relatedEntityClass][] = $fieldId->getFieldName(); |
||
273 | } |
||
274 | |||
275 | return $result; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Sorts the internal list of target entities by priority. |
||
280 | * |
||
281 | * @return string[] |
||
282 | */ |
||
283 | protected function getTargetEntities() |
||
284 | { |
||
285 | if (null === $this->sortedTargetEntities) { |
||
286 | ksort($this->targetEntities); |
||
287 | $this->sortedTargetEntities = !empty($this->targetEntities) |
||
0 ignored issues
–
show
It seems like
!empty($this->targetEnti...rgetEntities) : array() of type * is incompatible with the declared type array<integer,string> of property $sortedTargetEntities .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..
Loading history...
|
|||
288 | ? call_user_func_array('array_merge', $this->targetEntities) |
||
289 | : []; |
||
290 | } |
||
291 | |||
292 | return $this->sortedTargetEntities; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * @param array $arr1 |
||
297 | * @param array $arr2 |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | protected function mergePhoneNumbers(array $arr1, array $arr2) |
||
302 | { |
||
303 | foreach ($arr2 as $val) { |
||
304 | if (!$this->isPhoneNumberExist($arr1, $val)) { |
||
305 | $arr1[] = $val; |
||
306 | } |
||
307 | } |
||
308 | |||
309 | return $arr1; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param array $arr |
||
314 | * @param array $value |
||
315 | * |
||
316 | * @return bool |
||
317 | */ |
||
318 | public function isPhoneNumberExist(array $arr, array $value) |
||
319 | { |
||
320 | foreach ($arr as $val) { |
||
321 | if ($val[0] === $value[0] && $val[1] === $value[1]) { |
||
322 | return true; |
||
323 | } |
||
324 | } |
||
325 | |||
326 | return false; |
||
327 | } |
||
328 | } |
||
329 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..