Complex classes like ResourceGenerator 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ResourceGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ResourceGenerator extends AbstractGenerator |
||
29 | { |
||
30 | /** |
||
31 | * @private Filesystem |
||
32 | */ |
||
33 | private $filesystem; |
||
34 | |||
35 | /** |
||
36 | * @private HttpKernelInterface |
||
37 | */ |
||
38 | private $kernel; |
||
39 | |||
40 | /** |
||
41 | * our json file definition |
||
42 | * |
||
43 | * @var JsonDefinition|null |
||
44 | */ |
||
45 | private $json = null; |
||
46 | |||
47 | /** |
||
48 | * @var ArrayCollection |
||
49 | */ |
||
50 | protected $xmlParameters; |
||
51 | |||
52 | /** |
||
53 | * @var \DomDocument |
||
54 | */ |
||
55 | private $serviceDOM; |
||
56 | |||
57 | /** |
||
58 | * @var FieldMapper |
||
59 | */ |
||
60 | private $mapper; |
||
61 | |||
62 | /** |
||
63 | * @var boolean |
||
64 | */ |
||
65 | private $generateController = false; |
||
66 | |||
67 | /** |
||
68 | * @var ParameterBuilder |
||
69 | */ |
||
70 | private $parameterBuilder; |
||
71 | |||
72 | /** |
||
73 | * Instantiates generator object |
||
74 | * |
||
75 | * @param Filesystem $filesystem fs abstraction layer |
||
76 | * @param HttpKernelInterface $kernel app kernel |
||
77 | * @param FieldMapper $mapper field type mapper |
||
78 | * @param ParameterBuilder $parameterBuilder param builder |
||
79 | */ |
||
80 | 2 | public function __construct( |
|
92 | |||
93 | /** |
||
94 | * @param JsonDefinition $json optional JsonDefinition object |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | public function setJson(JsonDefinition $json) |
||
102 | |||
103 | /** |
||
104 | * @param boolean $generateController should the controller be generated or not |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | public function setGenerateController($generateController) |
||
112 | |||
113 | /** |
||
114 | * generate the resource with all its bits and parts |
||
115 | * |
||
116 | * @param BundleInterface $bundle bundle |
||
117 | * @param string $document document name |
||
118 | * @param string $format format of config files (please use xml) |
||
119 | * @param array $fields fields to add |
||
120 | * |
||
121 | * @return EntityGeneratorResult |
||
122 | */ |
||
123 | public function generate( |
||
124 | BundleInterface $bundle, |
||
125 | $document, |
||
126 | $format, |
||
127 | array $fields |
||
128 | ) { |
||
129 | $dir = $bundle->getPath(); |
||
130 | $basename = $this->getBundleBaseName($document); |
||
131 | $bundleNamespace = substr(get_class($bundle), 0, 0 - strlen($bundle->getName())); |
||
132 | |||
133 | if (!is_null($this->json)) { |
||
134 | $this->json->setNamespace($bundleNamespace); |
||
135 | } |
||
136 | |||
137 | // add more info to the fields array |
||
138 | $mapper = $this->mapper; |
||
139 | $fields = array_map( |
||
140 | function ($field) use ($mapper) { |
||
141 | return $mapper->map($field, $this->json); |
||
142 | }, |
||
143 | $this->mapper->buildFields($this->json) |
||
|
|||
144 | ); |
||
145 | |||
146 | $parameters = $this->parameterBuilder |
||
147 | ->setParameter('document', $document) |
||
148 | ->setParameter('base', $bundleNamespace) |
||
149 | ->setParameter('bundle', $bundle->getName()) |
||
150 | ->setParameter('format', $format) |
||
151 | ->setParameter('json', $this->json) |
||
152 | ->setParameter('fields', $fields) |
||
153 | ->setParameter('basename', $basename) |
||
154 | ->setParameter('isrecordOriginFlagSet', $this->json->isRecordOriginFlagSet()) |
||
155 | ->setParameter('recordOriginModifiable', $this->json->isRecordOriginModifiable()) |
||
156 | ->setParameter('isVersioning', $this->json->isVersionedService()) |
||
157 | ->setParameter('collection', $this->json->getServiceCollection()) |
||
158 | ->setParameter('indexes', $this->json->getIndexes()) |
||
159 | ->setParameter('textIndexes', $this->json->getAllTextIndexes()) |
||
160 | ->getParameters(); |
||
161 | |||
162 | $this->generateDocument($parameters, $dir, $document); |
||
163 | $this->generateSerializer($parameters, $dir, $document); |
||
164 | $this->generateModel($parameters, $dir, $document); |
||
165 | |||
166 | if ($this->json instanceof JsonDefinition && $this->json->hasFixtures() === true) { |
||
167 | $this->generateFixtures($parameters, $dir, $document); |
||
168 | } |
||
169 | |||
170 | if ($this->generateController) { |
||
171 | $this->generateController($parameters, $dir, $document); |
||
172 | } |
||
173 | |||
174 | $this->generateParameters($dir); |
||
175 | |||
176 | return new EntityGeneratorResult( |
||
177 | $dir . '/Document/' . $document . '.php', |
||
178 | '', |
||
179 | $dir . '/Resources/config/doctrine/' . $document . '.mongodb.yml' |
||
180 | ); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Writes the current services definition to a file. |
||
185 | * |
||
186 | * @param string $dir base bundle dir |
||
187 | * |
||
188 | * @return void |
||
189 | */ |
||
190 | 8 | protected function persistServicesXML($dir) |
|
196 | |||
197 | /** |
||
198 | * generate document part of a resource |
||
199 | * |
||
200 | * @param array $parameters twig parameters |
||
201 | * @param string $dir base bundle dir |
||
202 | * @param string $document document name |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | 6 | protected function generateDocument($parameters, $dir, $document) |
|
268 | 6 | ||
269 | 6 | /** |
|
270 | 6 | * update xml services |
|
271 | 3 | * |
|
272 | 3 | * @param array $parameters twig parameters |
|
273 | * @param string $dir base bundle dir |
||
274 | 6 | * @param string $document document name |
|
275 | 6 | * |
|
276 | 3 | * @return void |
|
277 | 3 | */ |
|
278 | protected function generateServices($parameters, $dir, $document) |
||
359 | 3 | ||
360 | 3 | /** |
|
361 | * Generates the parameters section of the services.xml file. |
||
362 | 6 | * |
|
363 | 6 | * @param string $dir base bundle dir |
|
364 | * |
||
365 | * @return void |
||
366 | */ |
||
367 | protected function generateParameters($dir) |
||
386 | 1 | ||
387 | 1 | /** |
|
388 | * Registers information to be generated to a parameter tag. |
||
389 | 2 | * |
|
390 | 2 | * @param mixed $value Content of the tag |
|
391 | * @param string $key Content of the key attribute |
||
392 | * @param string $type Type of the tag |
||
393 | * |
||
394 | * @return void |
||
395 | */ |
||
396 | protected function addXmlParameter($value, $key, $type = 'string') |
||
412 | |||
413 | 2 | /** |
|
414 | 2 | * load services.xml |
|
415 | 1 | * |
|
416 | 2 | * @param string $dir base dir |
|
417 | * |
||
418 | * @return \DOMDocument |
||
419 | */ |
||
420 | protected function loadServices($dir) |
||
431 | 4 | ||
432 | 2 | /** |
|
433 | * add param to services.xml |
||
434 | 4 | * |
|
435 | * @param \DOMDocument $dom services.xml document |
||
436 | * @param string $key parameter key |
||
437 | * @param string $value parameter value |
||
438 | * |
||
439 | * @return \DOMDocument |
||
440 | */ |
||
441 | protected function addParam(\DOMDocument $dom, $key, $value) |
||
455 | 12 | ||
456 | 6 | /** |
|
457 | * Adds a new parameter tag to parameters section reflecting the defined roles. |
||
458 | 12 | * |
|
459 | * @param \DOMDocument $dom services.xml document |
||
460 | * @param string $key parameter key |
||
461 | * @param array $values parameter value |
||
462 | * |
||
463 | * @return void |
||
464 | * |
||
465 | * @link http://symfony.com/doc/current/book/service_container.html#array-parameters |
||
466 | */ |
||
467 | protected function addCollectionParam(\DomDocument $dom, $key, array $values) |
||
486 | |||
487 | 4 | /** |
|
488 | 2 | * Determines, if the provided key attribute was already claimed by a parameter node. |
|
489 | 2 | * |
|
490 | 4 | * @param \DomDocument $dom Current document |
|
491 | * @param string $key Key to be found in document |
||
492 | * |
||
493 | * @return bool |
||
494 | */ |
||
495 | private function parameterNodeExists(\DomDocument $dom, $key) |
||
502 | 14 | ||
503 | 14 | /** |
|
504 | * add node if missing |
||
505 | 14 | * |
|
506 | * @param \DOMDocument $dom document |
||
507 | * @param string $element name for new node element |
||
508 | * @param string $insertBefore xPath query of the new node shall be added before |
||
509 | * @param string $container name of container tag |
||
510 | * |
||
511 | * @return \DOMNode new element node |
||
512 | */ |
||
513 | private function addNodeIfMissing(&$dom, $element, $insertBefore = '', $container = 'container') |
||
539 | 4 | ||
540 | /** |
||
541 | * add attribute to node if needed |
||
542 | 14 | * |
|
543 | * @param string $name attribute name |
||
544 | * @param string $value attribute value |
||
545 | * @param \DOMDocument $dom document |
||
546 | * @param \DOMElement $node parent node |
||
547 | * |
||
548 | * @return void |
||
549 | */ |
||
550 | private function addAttributeToNode($name, $value, $dom, $node) |
||
558 | 14 | ||
559 | 14 | /** |
|
560 | 14 | * add service to services.xml |
|
561 | 7 | * |
|
562 | 14 | * @param \DOMDocument $dom services.xml dom |
|
563 | * @param string $id id of new service |
||
564 | * @param string $parent parent for service |
||
565 | * @param array $calls methodCalls to add |
||
566 | * @param string $tag tag name or empty if no tag needed |
||
567 | * @param array $arguments service arguments |
||
568 | * @param string $factoryService factory service id |
||
569 | * @param string $factoryMethod factory method name |
||
570 | * @param string $className class name to override |
||
571 | * |
||
572 | * @return \DOMDocument |
||
573 | */ |
||
574 | protected function addService( |
||
642 | |||
643 | /** |
||
644 | * add calls to service |
||
645 | * |
||
646 | * @param array $calls info on calls to create |
||
647 | * @param \DOMDocument $dom current domdocument |
||
648 | * @param \DOMElement $node node to add call to |
||
649 | * |
||
650 | * @return void |
||
651 | */ |
||
652 | private function addCallsToService($calls, $dom, $node) |
||
658 | |||
659 | /** |
||
660 | * add call to service |
||
661 | * |
||
662 | * @param array $call info on call node to create |
||
663 | * @param \DOMDocument $dom current domdocument |
||
664 | * @param \DOMElement $node node to add call to |
||
665 | * |
||
666 | * @return void |
||
667 | */ |
||
668 | private function addCallToService($call, $dom, $node) |
||
690 | |||
691 | /** |
||
692 | * add arguments to servie |
||
693 | * |
||
694 | * @param array $arguments arguments to create |
||
695 | * @param \DOMDocument $dom dom document to add to |
||
696 | * @param \DOMElement $node node to use as parent |
||
697 | * |
||
698 | * @return void |
||
699 | */ |
||
700 | private function addArgumentsToService($arguments, $dom, $node) |
||
706 | |||
707 | /** |
||
708 | * add argument to service |
||
709 | * |
||
710 | * @param array $argument info on argument to create |
||
711 | * @param \DOMDocument $dom dom document to add to |
||
712 | * @param \DOMElement $node node to use as parent |
||
713 | * |
||
714 | * @return void |
||
715 | */ |
||
716 | private function addArgumentToService($argument, $dom, $node) |
||
736 | |||
737 | /** |
||
738 | * generate serializer part of a resource |
||
739 | * |
||
740 | * @param array $parameters twig parameters |
||
741 | * @param string $dir base bundle dir |
||
742 | * @param string $document document name |
||
743 | * |
||
744 | * @return void |
||
745 | */ |
||
746 | protected function generateSerializer(array $parameters, $dir, $document) |
||
797 | |||
798 | /** |
||
799 | * generate model part of a resource |
||
800 | * |
||
801 | * @param array $parameters twig parameters |
||
802 | * @param string $dir base bundle dir |
||
803 | * @param string $document document name |
||
804 | * |
||
805 | * @return void |
||
806 | */ |
||
807 | protected function generateModel(array $parameters, $dir, $document) |
||
877 | |||
878 | /** |
||
879 | * generate RESTful controllers ans service configs |
||
880 | * |
||
881 | * @param array $parameters twig parameters |
||
882 | * @param string $dir base bundle dir |
||
883 | * @param string $document document name |
||
884 | * |
||
885 | * @return void |
||
886 | */ |
||
887 | protected function generateController(array $parameters, $dir, $document) |
||
925 | |||
926 | /** |
||
927 | * generates fixtures |
||
928 | * |
||
929 | * @param array $parameters twig parameters |
||
930 | * @param string $dir base bundle dir |
||
931 | * @param string $document document name |
||
932 | * |
||
933 | * @return void |
||
934 | */ |
||
935 | protected function generateFixtures(array $parameters, $dir, $document) |
||
946 | } |
||
947 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: