| Conditions | 22 |
| Paths | 280 |
| Total Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 67 | public function loadEntities(array $entities, ?array $propertyMap = null) |
||
| 68 | { |
||
| 69 | if ($propertyMap === null) { |
||
| 70 | $propertyMap = $this->defaultPropertiesToLoad; |
||
| 71 | } |
||
| 72 | //build groups for each type |
||
| 73 | $toDoEntityIds = []; |
||
| 74 | $done = []; |
||
| 75 | foreach ($entities as $entity) { |
||
| 76 | if (!array_key_exists($entity->getEntityId(), $done)) { |
||
| 77 | $done[$entity->getEntityId()] = true; |
||
| 78 | $class = $this->keyOfPropertyMap($entity, $propertyMap); |
||
| 79 | if ($class !== null) { |
||
| 80 | if (!array_key_exists($class, $toDoEntityIds)) { |
||
| 81 | $toDoEntityIds[$class] = []; |
||
| 82 | } |
||
| 83 | $toDoEntityIds[$class][] = $entity; |
||
| 84 | break; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | while (count($toDoEntityIds) > 0) { |
||
| 89 | $entities = reset($toDoEntityIds); |
||
| 90 | |||
| 91 | $class = key($toDoEntityIds); |
||
| 92 | unset($toDoEntityIds[$class]); |
||
| 93 | foreach ($propertyMap[$class] as $properties) { |
||
| 94 | //eliminate entities which have already loaded the properties |
||
| 95 | $ids = array_map(function (IdAble $e) { |
||
| 96 | return $e->getEntityId(); |
||
| 97 | }, array_filter($entities, function (IdAble $e) use ($properties) { |
||
| 98 | foreach ($properties as $property) { |
||
| 99 | $getter = "get" . ucfirst($property); |
||
| 100 | $object = $e->$getter(); |
||
| 101 | if ($object === null) { |
||
| 102 | return true; |
||
| 103 | } |
||
| 104 | if ($object instanceof AbstractLazyCollection) { |
||
| 105 | /** @var $object AbstractLazyCollection */ |
||
| 106 | if (!$object->isInitialized()) { |
||
| 107 | return true; |
||
| 108 | } |
||
| 109 | } else if (!property_exists($object, '__isInitialized__')) { |
||
| 110 | return true; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | return false; |
||
| 114 | })); |
||
| 115 | if (count($ids) > 0) { |
||
| 116 | $this->loadProperties($ids, $class, $properties); |
||
| 117 | } |
||
| 118 | //check loaded subproperties if they also have subproperties which need to get loaded |
||
| 119 | foreach ($entities as $entity) { |
||
| 120 | foreach ($properties as $property) { |
||
| 121 | $getter = "get" . ucfirst($property); |
||
| 122 | $object = $entity->$getter(); |
||
| 123 | if ($object !== null) { |
||
| 124 | if (!$object instanceof Collection) { |
||
| 125 | $object = new ArrayCollection([$object]); |
||
| 126 | } |
||
| 127 | /** @var $object Collection|IdAble[] */ |
||
| 128 | foreach ($object as $subObject) { |
||
| 129 | if (!array_key_exists($subObject->getEntityId(), $done)) { |
||
| 130 | $done[$subObject->getEntityId()] = true; |
||
| 131 | $subClass = $this->keyOfPropertyMap($subObject, $propertyMap); |
||
| 132 | if ($subClass !== null) { |
||
| 133 | if (!array_key_exists($subClass, $toDoEntityIds)) { |
||
| 134 | $toDoEntityIds[$subClass] = []; |
||
| 135 | } |
||
| 136 | $toDoEntityIds[$subClass][] = $subObject; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | //</editor-fold desc="Public Methods"> |
||
| 187 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.