| Total Complexity | 44 |
| Total Lines | 275 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like JsonPathFinder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JsonPathFinder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class JsonPathFinder |
||
| 11 | { |
||
| 12 | const INDEX_ENTITY_PARENT = 0; |
||
| 13 | |||
| 14 | const INDEX_FK_RELATION_NAME = 1; |
||
| 15 | |||
| 16 | const INDEX_ENTITY_FIRST_CHILD = 2; |
||
| 17 | |||
| 18 | private $map; |
||
| 19 | |||
| 20 | private $entity; |
||
| 21 | |||
| 22 | private $entitiesPath = []; |
||
| 23 | |||
| 24 | private $wrongPath = []; |
||
| 25 | |||
| 26 | private $mapper; |
||
| 27 | |||
| 28 | private $appendRootEntityToSubject; |
||
| 29 | |||
| 30 | private $incrementSubject; |
||
| 31 | |||
| 32 | private $allPaths = []; |
||
| 33 | |||
| 34 | private static $indeToDescriptionMap = [ |
||
| 35 | self::INDEX_ENTITY_PARENT => 'parent', |
||
| 36 | self::INDEX_FK_RELATION_NAME => 'relation', |
||
| 37 | self::INDEX_ENTITY_FIRST_CHILD => 'first child', |
||
| 38 | ]; |
||
| 39 | |||
| 40 | private $logger; |
||
| 41 | |||
| 42 | public function __construct( |
||
| 43 | DataMapper $mapper, |
||
| 44 | LoggerInterface $logger = null |
||
| 45 | ) { |
||
| 46 | $this->mapper = $mapper; |
||
| 47 | $this->logger = $logger; |
||
| 48 | |||
| 49 | $this->appendRootEntityToSubject = function($subject, $rootEntity) { |
||
| 50 | $subject[] = $rootEntity; |
||
| 51 | return $subject; |
||
| 52 | }; |
||
| 53 | |||
| 54 | $this->incrementSubject = function($subject) { |
||
| 55 | return ++$subject; |
||
| 56 | }; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function setEntity(string $entity) |
||
| 60 | { |
||
| 61 | $this->entity = $entity; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function getFirstParentOf(string $innerEntity) |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function getFirstChildOf(string $innerEntity) |
||
| 75 | { |
||
| 76 | return $this->keep( |
||
| 77 | self::INDEX_ENTITY_FIRST_CHILD, |
||
| 78 | $innerEntity |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getSourceRelation(string $innerEntity) |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function clearMap(string $innerEntity) |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | public function getPathTo(string $innerEntity = '', $nest = 0) |
||
| 139 | } |
||
| 140 | |||
| 141 | public function setQueryStartEntity(string $startEntity) |
||
| 142 | { |
||
| 143 | $this->setEntity($startEntity); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function getPathToEntity(string $entityToReach, $reloadMap = false) |
||
| 147 | { |
||
| 148 | $this->entitiesPath = []; |
||
| 149 | |||
| 150 | foreach ($this->getMap($reloadMap) as $rootEntity => $meta) { |
||
| 151 | if (in_array($rootEntity, $this->wrongPath)) { |
||
| 152 | unset($this->map[$rootEntity]); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | $return = '_embedded.' . $this->getPathTo($entityToReach); |
||
| 157 | |||
| 158 | $this->allPaths[] = $this->entitiesPath; |
||
| 159 | |||
| 160 | return $return; |
||
| 161 | } |
||
| 162 | |||
| 163 | public function keep($val, $innerEntity) |
||
| 164 | { |
||
| 165 | foreach ($this->getMap() as $rootEntity => $meta) { |
||
| 166 | foreach ($meta['relations'] as $name => $entity) { |
||
| 167 | if (self::INDEX_ENTITY_FIRST_CHILD == $val) { |
||
| 168 | return $entity; |
||
| 169 | } |
||
| 170 | |||
| 171 | if ($entity == $innerEntity) { |
||
| 172 | $return = [ |
||
| 173 | self::INDEX_ENTITY_PARENT => $rootEntity, |
||
| 174 | self::INDEX_FK_RELATION_NAME => $name, |
||
| 175 | ][$val]; |
||
| 176 | |||
| 177 | return $return; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | throw new Exceptions\UnespectedValueException(var_export([ |
||
| 183 | 'val' => self::$indeToDescriptionMap[$val], |
||
| 184 | 'innerEntity' => $innerEntity, |
||
| 185 | 'map' => $this->getMap(), |
||
| 186 | ], true)); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function numberOfRelationsToEntity(string $entityToReach) |
||
| 195 | ); |
||
| 196 | } |
||
| 197 | |||
| 198 | public function listOfParentsOf(string $entityToReach) |
||
| 199 | { |
||
| 200 | return $this->mapTargetRelations( |
||
| 201 | $this->appendRootEntityToSubject, |
||
| 202 | $subject = [], |
||
| 203 | $entityToReach |
||
| 204 | ); |
||
| 205 | } |
||
| 206 | |||
| 207 | public function getEntitiesPath() |
||
| 216 | } |
||
| 217 | |||
| 218 | public function removeStep($parentToSkip) |
||
| 219 | { |
||
| 220 | $this->wrongPath[] = $parentToSkip; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function getHashKeyForDestination(string $destination) |
||
| 226 | } |
||
| 227 | |||
| 228 | public function forceMapReloading() |
||
| 229 | { |
||
| 230 | $this->map = $this->mapper->getMap(); |
||
| 231 | } |
||
| 232 | |||
| 233 | private function getMap($reloadMap = false) |
||
| 240 | } |
||
| 241 | |||
| 242 | public function addEntity(array $parents, $rootEntity) : array |
||
| 247 | } |
||
| 248 | |||
| 249 | public function mapTargetRelations( |
||
| 263 | } |
||
| 264 | |||
| 265 | public function getAllPaths() : array |
||
| 266 | { |
||
| 267 | array_multisort($this->allPaths); |
||
| 268 | return $this->allPaths; |
||
| 269 | } |
||
| 270 | |||
| 271 | public function findAllPathsTo(string $dest) |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } |
||
| 289 |