Total Complexity | 42 |
Total Lines | 267 |
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) |
||
83 | { |
||
84 | return $this->keep( |
||
85 | self::INDEX_FK_RELATION_NAME, |
||
86 | $innerEntity |
||
87 | ); |
||
88 | } |
||
89 | |||
90 | public function clearMap(string $innerEntity) |
||
91 | { |
||
92 | if (in_array($this->entity, $this->listOfParentsOf($innerEntity))) { |
||
93 | foreach ($this->map as $rootEntity => $meta) { |
||
94 | if ($this->entity != $rootEntity) { |
||
95 | unset($this->map[$rootEntity]); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | |||
101 | public function getPathTo(string $innerEntity = '', $nest = 0) |
||
136 | } |
||
137 | |||
138 | public function setQueryStartEntity(string $startEntity) |
||
139 | { |
||
140 | $this->setEntity($startEntity); |
||
141 | } |
||
142 | |||
143 | public function getPathToEntity(string $entityToReach) |
||
144 | { |
||
145 | $this->entitiesPath = []; |
||
146 | |||
147 | foreach ($this->getMap() as $rootEntity => $meta) { |
||
148 | if (in_array($rootEntity, $this->wrongPath)) { |
||
149 | unset($this->map[$rootEntity]); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | $return = '_embedded.' . $this->getPathTo($entityToReach); |
||
154 | |||
155 | $this->allPaths[] = $this->entitiesPath; |
||
156 | |||
157 | return $return; |
||
158 | } |
||
159 | |||
160 | public function keep($val, $innerEntity) |
||
161 | { |
||
162 | foreach ($this->getMap() as $rootEntity => $meta) { |
||
163 | foreach ($meta['relations'] as $name => $entity) { |
||
164 | if (self::INDEX_ENTITY_FIRST_CHILD == $val) { |
||
165 | return $entity; |
||
166 | } |
||
167 | |||
168 | if ($entity == $innerEntity) { |
||
169 | $return = [ |
||
170 | self::INDEX_ENTITY_PARENT => $rootEntity, |
||
171 | self::INDEX_FK_RELATION_NAME => $name, |
||
172 | ][$val]; |
||
173 | |||
174 | return $return; |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 | |||
179 | throw new Exceptions\UnespectedValueException(var_export([ |
||
180 | 'val' => self::$indeToDescriptionMap[$val], |
||
181 | 'innerEntity' => $innerEntity, |
||
182 | 'map' => $this->getMap(), |
||
183 | ], true)); |
||
184 | } |
||
185 | |||
186 | public function numberOfRelationsToEntity(string $entityToReach) |
||
192 | ); |
||
193 | } |
||
194 | |||
195 | public function listOfParentsOf(string $entityToReach) |
||
196 | { |
||
197 | return $this->mapTargetRelations( |
||
198 | $this->appendRootEntityToSubject, |
||
199 | $subject = [], |
||
200 | $entityToReach |
||
201 | ); |
||
202 | } |
||
203 | |||
204 | public function getEntitiesPath() |
||
213 | } |
||
214 | |||
215 | public function removeStep($parentToSkip) |
||
216 | { |
||
217 | $this->wrongPath[] = $parentToSkip; |
||
218 | } |
||
219 | |||
220 | public function getHashKeyForDestination(string $destination) |
||
223 | } |
||
224 | |||
225 | private function getMap() |
||
232 | } |
||
233 | |||
234 | public function addEntity(array $parents, $rootEntity) : array |
||
239 | } |
||
240 | |||
241 | public function mapTargetRelations( |
||
255 | } |
||
256 | |||
257 | public function getAllPaths() : array |
||
258 | { |
||
259 | array_multisort($this->allPaths); |
||
260 | return $this->allPaths; |
||
261 | } |
||
262 | |||
263 | public function findAllPathsTo(string $dest) |
||
277 | } |
||
278 | } |
||
279 | } |
||
280 | } |
||
281 |