Complex classes like EntityGenerator 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 EntityGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | class EntityGenerator |
||
48 | { |
||
49 | /** |
||
50 | * Specifies class fields should be protected. |
||
51 | */ |
||
52 | const FIELD_VISIBLE_PROTECTED = 'protected'; |
||
53 | |||
54 | /** |
||
55 | * Specifies class fields should be private. |
||
56 | */ |
||
57 | const FIELD_VISIBLE_PRIVATE = 'private'; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | protected $backupExisting = true; |
||
63 | |||
64 | /** |
||
65 | * The extension to use for written php files. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $extension = '.php'; |
||
70 | |||
71 | /** |
||
72 | * Whether or not the current ClassMetadataInfo instance is new or old. |
||
73 | * |
||
74 | * @var boolean |
||
75 | */ |
||
76 | protected $isNew = true; |
||
77 | |||
78 | /** |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $staticReflection = array(); |
||
82 | |||
83 | /** |
||
84 | * Number of spaces to use for indention in generated code. |
||
85 | */ |
||
86 | protected $numSpaces = 4; |
||
87 | |||
88 | /** |
||
89 | * The actual spaces to use for indention. |
||
90 | * |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $spaces = ' '; |
||
94 | |||
95 | /** |
||
96 | * The class all generated entities should extend. |
||
97 | * |
||
98 | * @var string |
||
99 | */ |
||
100 | protected $classToExtend; |
||
101 | |||
102 | /** |
||
103 | * Whether or not to generation annotations. |
||
104 | * |
||
105 | * @var boolean |
||
106 | */ |
||
107 | protected $generateAnnotations = false; |
||
108 | |||
109 | /** |
||
110 | * @var string |
||
111 | */ |
||
112 | protected $annotationsPrefix = ''; |
||
113 | |||
114 | /** |
||
115 | * Whether or not to generate sub methods. |
||
116 | * |
||
117 | * @var boolean |
||
118 | */ |
||
119 | protected $generateEntityStubMethods = false; |
||
120 | |||
121 | /** |
||
122 | * Whether or not to update the entity class if it exists already. |
||
123 | * |
||
124 | * @var boolean |
||
125 | */ |
||
126 | protected $updateEntityIfExists = false; |
||
127 | |||
128 | /** |
||
129 | * Whether or not to re-generate entity class if it exists already. |
||
130 | * |
||
131 | * @var boolean |
||
132 | */ |
||
133 | protected $regenerateEntityIfExists = false; |
||
134 | |||
135 | /** |
||
136 | * Visibility of the field |
||
137 | * |
||
138 | * @var string |
||
139 | */ |
||
140 | protected $fieldVisibility = 'private'; |
||
141 | |||
142 | /** |
||
143 | * Whether or not to make generated embeddables immutable. |
||
144 | * |
||
145 | * @var boolean. |
||
146 | */ |
||
147 | protected $embeddablesImmutable = false; |
||
148 | |||
149 | /** |
||
150 | * Hash-map for handle types. |
||
151 | * |
||
152 | * @var array |
||
153 | */ |
||
154 | protected $typeAlias = array( |
||
155 | Type::DATETIMETZ => '\DateTime', |
||
156 | Type::DATETIME => '\DateTime', |
||
157 | Type::DATE => '\DateTime', |
||
158 | Type::TIME => '\DateTime', |
||
159 | Type::OBJECT => '\stdClass', |
||
160 | Type::INTEGER => 'int', |
||
161 | Type::BIGINT => 'int', |
||
162 | Type::SMALLINT => 'int', |
||
163 | Type::TEXT => 'string', |
||
164 | Type::BLOB => 'string', |
||
165 | Type::DECIMAL => 'string', |
||
166 | Type::JSON_ARRAY => 'array', |
||
167 | Type::SIMPLE_ARRAY => 'array', |
||
168 | Type::BOOLEAN => 'bool', |
||
169 | ); |
||
170 | |||
171 | /** |
||
172 | * Hash-map to handle generator types string. |
||
173 | * |
||
174 | * @var array |
||
175 | */ |
||
176 | protected static $generatorStrategyMap = array( |
||
177 | ClassMetadataInfo::GENERATOR_TYPE_AUTO => 'AUTO', |
||
178 | ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE => 'SEQUENCE', |
||
179 | ClassMetadataInfo::GENERATOR_TYPE_TABLE => 'TABLE', |
||
180 | ClassMetadataInfo::GENERATOR_TYPE_IDENTITY => 'IDENTITY', |
||
181 | ClassMetadataInfo::GENERATOR_TYPE_NONE => 'NONE', |
||
182 | ClassMetadataInfo::GENERATOR_TYPE_UUID => 'UUID', |
||
183 | ClassMetadataInfo::GENERATOR_TYPE_CUSTOM => 'CUSTOM' |
||
184 | ); |
||
185 | |||
186 | /** |
||
187 | * Hash-map to handle the change tracking policy string. |
||
188 | * |
||
189 | * @var array |
||
190 | */ |
||
191 | protected static $changeTrackingPolicyMap = array( |
||
192 | ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT => 'DEFERRED_IMPLICIT', |
||
193 | ClassMetadataInfo::CHANGETRACKING_DEFERRED_EXPLICIT => 'DEFERRED_EXPLICIT', |
||
194 | ClassMetadataInfo::CHANGETRACKING_NOTIFY => 'NOTIFY', |
||
195 | ); |
||
196 | |||
197 | /** |
||
198 | * Hash-map to handle the inheritance type string. |
||
199 | * |
||
200 | * @var array |
||
201 | */ |
||
202 | protected static $inheritanceTypeMap = array( |
||
203 | ClassMetadataInfo::INHERITANCE_TYPE_NONE => 'NONE', |
||
204 | ClassMetadataInfo::INHERITANCE_TYPE_JOINED => 'JOINED', |
||
205 | ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE => 'SINGLE_TABLE', |
||
206 | ClassMetadataInfo::INHERITANCE_TYPE_TABLE_PER_CLASS => 'TABLE_PER_CLASS', |
||
207 | ); |
||
208 | |||
209 | /** |
||
210 | * @var string |
||
211 | */ |
||
212 | protected static $classTemplate = |
||
213 | '<?php |
||
214 | |||
215 | <namespace> |
||
216 | <useStatement> |
||
217 | <entityAnnotation> |
||
218 | <entityClassName> |
||
219 | { |
||
220 | <entityBody> |
||
221 | } |
||
222 | '; |
||
223 | |||
224 | /** |
||
225 | * @var string |
||
226 | */ |
||
227 | protected static $getMethodTemplate = |
||
228 | '/** |
||
229 | * <description> |
||
230 | * |
||
231 | * @return <variableType> |
||
232 | */ |
||
233 | public function <methodName>() |
||
234 | { |
||
235 | <spaces>return $this-><fieldName>; |
||
236 | }'; |
||
237 | |||
238 | /** |
||
239 | * @var string |
||
240 | */ |
||
241 | protected static $setMethodTemplate = |
||
242 | '/** |
||
243 | * <description> |
||
244 | * |
||
245 | * @param <variableType> $<variableName> |
||
246 | * |
||
247 | * @return <entity> |
||
248 | */ |
||
249 | public function <methodName>(<methodTypeHint>$<variableName><variableDefault>) |
||
250 | { |
||
251 | <spaces>$this-><fieldName> = $<variableName>; |
||
252 | |||
253 | <spaces>return $this; |
||
254 | }'; |
||
255 | |||
256 | /** |
||
257 | * @var string |
||
258 | */ |
||
259 | protected static $addMethodTemplate = |
||
260 | '/** |
||
261 | * <description> |
||
262 | * |
||
263 | * @param <variableType> $<variableName> |
||
264 | * |
||
265 | * @return <entity> |
||
266 | */ |
||
267 | public function <methodName>(<methodTypeHint>$<variableName>) |
||
268 | { |
||
269 | <spaces>$this-><fieldName>[] = $<variableName>; |
||
270 | |||
271 | <spaces>return $this; |
||
272 | }'; |
||
273 | |||
274 | /** |
||
275 | * @var string |
||
276 | */ |
||
277 | protected static $removeMethodTemplate = |
||
278 | '/** |
||
279 | * <description> |
||
280 | * |
||
281 | * @param <variableType> $<variableName> |
||
282 | * |
||
283 | * @return boolean TRUE if this collection contained the specified element, FALSE otherwise. |
||
284 | */ |
||
285 | public function <methodName>(<methodTypeHint>$<variableName>) |
||
286 | { |
||
287 | <spaces>return $this-><fieldName>->removeElement($<variableName>); |
||
288 | }'; |
||
289 | |||
290 | /** |
||
291 | * @var string |
||
292 | */ |
||
293 | protected static $lifecycleCallbackMethodTemplate = |
||
294 | '/** |
||
295 | * <lifecycleNames> |
||
296 | */ |
||
297 | public function <methodName>() |
||
298 | { |
||
299 | <spaces>// Add your code here |
||
300 | }'; |
||
301 | |||
302 | /** |
||
303 | * @var string |
||
304 | */ |
||
305 | protected static $constructorMethodTemplate = |
||
306 | '/** |
||
307 | * Constructor |
||
308 | */ |
||
309 | public function __construct() |
||
310 | { |
||
311 | <spaces><collections> |
||
312 | } |
||
313 | '; |
||
314 | |||
315 | /** |
||
316 | * @var string |
||
317 | */ |
||
318 | protected static $embeddableConstructorMethodTemplate = |
||
319 | '/** |
||
320 | * Constructor |
||
321 | * |
||
322 | * <paramTags> |
||
323 | */ |
||
324 | public function __construct(<params>) |
||
325 | { |
||
326 | <spaces><fields> |
||
327 | } |
||
328 | '; |
||
329 | |||
330 | /** |
||
331 | * Constructor. |
||
332 | */ |
||
333 | 41 | public function __construct() |
|
334 | { |
||
335 | 41 | if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) { |
|
336 | 41 | $this->annotationsPrefix = 'ORM\\'; |
|
337 | } |
||
338 | 41 | } |
|
339 | |||
340 | /** |
||
341 | * Generates and writes entity classes for the given array of ClassMetadataInfo instances. |
||
342 | * |
||
343 | * @param array $metadatas |
||
344 | * @param string $outputDirectory |
||
345 | * |
||
346 | * @return void |
||
347 | */ |
||
348 | public function generate(array $metadatas, $outputDirectory) |
||
349 | { |
||
350 | foreach ($metadatas as $metadata) { |
||
351 | $this->writeEntityClass($metadata, $outputDirectory); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Generates and writes entity class to disk for the given ClassMetadataInfo instance. |
||
357 | * |
||
358 | * @param ClassMetadataInfo $metadata |
||
359 | * @param string $outputDirectory |
||
360 | * |
||
361 | * @return void |
||
362 | * |
||
363 | * @throws \RuntimeException |
||
364 | */ |
||
365 | 31 | public function writeEntityClass(ClassMetadataInfo $metadata, $outputDirectory) |
|
366 | { |
||
367 | 31 | $path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->extension; |
|
368 | 31 | $dir = dirname($path); |
|
369 | |||
370 | 31 | if ( ! is_dir($dir)) { |
|
371 | 2 | mkdir($dir, 0775, true); |
|
372 | } |
||
373 | |||
374 | 31 | $this->isNew = !file_exists($path) || (file_exists($path) && $this->regenerateEntityIfExists); |
|
375 | |||
376 | 31 | if ( ! $this->isNew) { |
|
377 | 3 | $this->parseTokensInEntityFile(file_get_contents($path)); |
|
378 | } else { |
||
379 | 30 | $this->staticReflection[$metadata->name] = array('properties' => array(), 'methods' => array()); |
|
380 | } |
||
381 | |||
382 | 31 | if ($this->backupExisting && file_exists($path)) { |
|
383 | 3 | $backupPath = dirname($path) . DIRECTORY_SEPARATOR . basename($path) . "~"; |
|
384 | 3 | if (!copy($path, $backupPath)) { |
|
385 | throw new \RuntimeException("Attempt to backup overwritten entity file but copy operation failed."); |
||
386 | } |
||
387 | } |
||
388 | |||
389 | // If entity doesn't exist or we're re-generating the entities entirely |
||
390 | 31 | if ($this->isNew) { |
|
391 | 30 | file_put_contents($path, $this->generateEntityClass($metadata)); |
|
392 | // If entity exists and we're allowed to update the entity class |
||
393 | 3 | } elseif ( ! $this->isNew && $this->updateEntityIfExists) { |
|
394 | 3 | file_put_contents($path, $this->generateUpdatedEntityClass($metadata, $path)); |
|
395 | } |
||
396 | 31 | chmod($path, 0664); |
|
397 | 31 | } |
|
398 | |||
399 | /** |
||
400 | * Generates a PHP5 Doctrine 2 entity class from the given ClassMetadataInfo instance. |
||
401 | * |
||
402 | * @param ClassMetadataInfo $metadata |
||
403 | * |
||
404 | * @return string |
||
405 | */ |
||
406 | 31 | public function generateEntityClass(ClassMetadataInfo $metadata) |
|
428 | |||
429 | /** |
||
430 | * Generates the updated code for the given ClassMetadataInfo and entity at path. |
||
431 | * |
||
432 | * @param ClassMetadataInfo $metadata |
||
433 | * @param string $path |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | 3 | public function generateUpdatedEntityClass(ClassMetadataInfo $metadata, $path) |
|
438 | { |
||
439 | 3 | $currentCode = file_get_contents($path); |
|
440 | |||
441 | 3 | $body = $this->generateEntityBody($metadata); |
|
442 | 3 | $body = str_replace('<spaces>', $this->spaces, $body); |
|
443 | 3 | $last = strrpos($currentCode, '}'); |
|
444 | |||
445 | 3 | return substr($currentCode, 0, $last) . $body . (strlen($body) > 0 ? "\n" : '') . "}\n"; |
|
446 | } |
||
447 | |||
448 | /** |
||
449 | * Sets the number of spaces the exported class should have. |
||
450 | * |
||
451 | * @param integer $numSpaces |
||
452 | * |
||
453 | * @return void |
||
454 | */ |
||
455 | public function setNumSpaces($numSpaces) |
||
456 | { |
||
457 | $this->spaces = str_repeat(' ', $numSpaces); |
||
458 | $this->numSpaces = $numSpaces; |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Sets the extension to use when writing php files to disk. |
||
463 | * |
||
464 | * @param string $extension |
||
465 | * |
||
466 | * @return void |
||
467 | */ |
||
468 | public function setExtension($extension) |
||
472 | |||
473 | /** |
||
474 | * Sets the name of the class the generated classes should extend from. |
||
475 | * |
||
476 | * @param string $classToExtend |
||
477 | * |
||
478 | * @return void |
||
479 | */ |
||
480 | 1 | public function setClassToExtend($classToExtend) |
|
484 | |||
485 | /** |
||
486 | * Sets whether or not to generate annotations for the entity. |
||
487 | * |
||
488 | * @param bool $bool |
||
489 | * |
||
490 | * @return void |
||
491 | */ |
||
492 | 40 | public function setGenerateAnnotations($bool) |
|
496 | |||
497 | /** |
||
498 | * Sets the class fields visibility for the entity (can either be private or protected). |
||
499 | * |
||
500 | * @param bool $visibility |
||
501 | * |
||
502 | * @return void |
||
503 | * |
||
504 | * @throws \InvalidArgumentException |
||
505 | */ |
||
506 | 39 | public function setFieldVisibility($visibility) |
|
507 | { |
||
508 | 39 | if ($visibility !== static::FIELD_VISIBLE_PRIVATE && $visibility !== static::FIELD_VISIBLE_PROTECTED) { |
|
509 | throw new \InvalidArgumentException('Invalid provided visibility (only private and protected are allowed): ' . $visibility); |
||
510 | } |
||
511 | |||
512 | 39 | $this->fieldVisibility = $visibility; |
|
513 | 39 | } |
|
514 | |||
515 | /** |
||
516 | * Sets whether or not to generate immutable embeddables. |
||
517 | * |
||
518 | * @param boolean $embeddablesImmutable |
||
519 | */ |
||
520 | 1 | public function setEmbeddablesImmutable($embeddablesImmutable) |
|
524 | |||
525 | /** |
||
526 | * Sets an annotation prefix. |
||
527 | * |
||
528 | * @param string $prefix |
||
529 | * |
||
530 | * @return void |
||
531 | */ |
||
532 | 40 | public function setAnnotationPrefix($prefix) |
|
536 | |||
537 | /** |
||
538 | * Sets whether or not to try and update the entity if it already exists. |
||
539 | * |
||
540 | * @param bool $bool |
||
541 | * |
||
542 | * @return void |
||
543 | */ |
||
544 | 40 | public function setUpdateEntityIfExists($bool) |
|
548 | |||
549 | /** |
||
550 | * Sets whether or not to regenerate the entity if it exists. |
||
551 | * |
||
552 | * @param bool $bool |
||
553 | * |
||
554 | * @return void |
||
555 | */ |
||
556 | 40 | public function setRegenerateEntityIfExists($bool) |
|
560 | |||
561 | /** |
||
562 | * Sets whether or not to generate stub methods for the entity. |
||
563 | * |
||
564 | * @param bool $bool |
||
565 | * |
||
566 | * @return void |
||
567 | */ |
||
568 | 40 | public function setGenerateStubMethods($bool) |
|
572 | |||
573 | /** |
||
574 | * Should an existing entity be backed up if it already exists? |
||
575 | * |
||
576 | * @param bool $bool |
||
577 | * |
||
578 | * @return void |
||
579 | */ |
||
580 | 1 | public function setBackupExisting($bool) |
|
584 | |||
585 | /** |
||
586 | * @param string $type |
||
587 | * |
||
588 | * @return string |
||
589 | */ |
||
590 | 31 | protected function getType($type) |
|
591 | { |
||
592 | 31 | if (isset($this->typeAlias[$type])) { |
|
593 | 30 | return $this->typeAlias[$type]; |
|
594 | } |
||
595 | |||
596 | 18 | return $type; |
|
597 | } |
||
598 | |||
599 | /** |
||
600 | * @param ClassMetadataInfo $metadata |
||
601 | * |
||
602 | * @return string |
||
603 | */ |
||
604 | 31 | protected function generateEntityNamespace(ClassMetadataInfo $metadata) |
|
605 | { |
||
606 | 31 | if ($this->hasNamespace($metadata)) { |
|
607 | 31 | return 'namespace ' . $this->getNamespace($metadata) .';'; |
|
608 | } |
||
609 | 2 | } |
|
610 | |||
611 | 31 | protected function generateEntityUse() |
|
612 | { |
||
613 | 31 | if ($this->generateAnnotations) { |
|
614 | 31 | return "\n".'use Doctrine\ORM\Mapping as ORM;'."\n"; |
|
615 | } else { |
||
616 | return ""; |
||
617 | } |
||
618 | } |
||
619 | |||
620 | /** |
||
621 | * @param ClassMetadataInfo $metadata |
||
622 | * |
||
623 | * @return string |
||
624 | */ |
||
625 | 31 | protected function generateEntityClassName(ClassMetadataInfo $metadata) |
|
626 | { |
||
627 | 31 | return 'class ' . $this->getClassName($metadata) . |
|
628 | 31 | ($this->extendsClass() ? ' extends ' . $this->getClassToExtendName() : null); |
|
629 | } |
||
630 | |||
631 | /** |
||
632 | * @param ClassMetadataInfo $metadata |
||
633 | * |
||
634 | * @return string |
||
635 | */ |
||
636 | 32 | protected function generateEntityBody(ClassMetadataInfo $metadata) |
|
637 | { |
||
638 | 32 | $fieldMappingProperties = $this->generateEntityFieldMappingProperties($metadata); |
|
639 | 32 | $embeddedProperties = $this->generateEntityEmbeddedProperties($metadata); |
|
640 | 32 | $associationMappingProperties = $this->generateEntityAssociationMappingProperties($metadata); |
|
641 | 32 | $stubMethods = $this->generateEntityStubMethods ? $this->generateEntityStubMethods($metadata) : null; |
|
642 | 32 | $lifecycleCallbackMethods = $this->generateEntityLifecycleCallbackMethods($metadata); |
|
643 | |||
644 | 32 | $code = array(); |
|
645 | |||
646 | 32 | if ($fieldMappingProperties) { |
|
647 | 29 | $code[] = $fieldMappingProperties; |
|
648 | } |
||
649 | |||
650 | 32 | if ($embeddedProperties) { |
|
651 | 8 | $code[] = $embeddedProperties; |
|
652 | } |
||
653 | |||
654 | 32 | if ($associationMappingProperties) { |
|
655 | 11 | $code[] = $associationMappingProperties; |
|
656 | } |
||
657 | |||
658 | 32 | $code[] = $this->generateEntityConstructor($metadata); |
|
659 | |||
660 | 32 | if ($stubMethods) { |
|
|
|||
661 | 30 | $code[] = $stubMethods; |
|
662 | } |
||
663 | |||
664 | 32 | if ($lifecycleCallbackMethods) { |
|
665 | 12 | $code[] = $lifecycleCallbackMethods; |
|
666 | } |
||
667 | |||
668 | 32 | return implode("\n", $code); |
|
669 | } |
||
670 | |||
671 | /** |
||
672 | * @param ClassMetadataInfo $metadata |
||
673 | * |
||
674 | * @return string |
||
675 | */ |
||
676 | 32 | protected function generateEntityConstructor(ClassMetadataInfo $metadata) |
|
677 | { |
||
678 | 32 | if ($this->hasMethod('__construct', $metadata)) { |
|
679 | 2 | return ''; |
|
680 | } |
||
681 | |||
682 | 32 | if ($metadata->isEmbeddedClass && $this->embeddablesImmutable) { |
|
683 | 1 | return $this->generateEmbeddableConstructor($metadata); |
|
684 | } |
||
685 | |||
686 | 31 | $collections = array(); |
|
687 | |||
688 | 31 | foreach ($metadata->associationMappings as $mapping) { |
|
689 | 13 | if ($mapping['type'] & ClassMetadataInfo::TO_MANY) { |
|
690 | 13 | $collections[] = '$this->'.$mapping['fieldName'].' = new \Doctrine\Common\Collections\ArrayCollection();'; |
|
691 | } |
||
692 | } |
||
693 | |||
694 | 31 | if ($collections) { |
|
695 | 11 | return $this->prefixCodeWithSpaces(str_replace("<collections>", implode("\n".$this->spaces, $collections), static::$constructorMethodTemplate)); |
|
696 | } |
||
697 | |||
698 | 27 | return ''; |
|
699 | } |
||
700 | |||
701 | /** |
||
702 | * @param ClassMetadataInfo $metadata |
||
703 | * |
||
704 | * @return string |
||
705 | */ |
||
706 | 1 | private function generateEmbeddableConstructor(ClassMetadataInfo $metadata) |
|
707 | { |
||
708 | 1 | $paramTypes = array(); |
|
709 | 1 | $paramVariables = array(); |
|
710 | 1 | $params = array(); |
|
711 | 1 | $fields = array(); |
|
712 | |||
713 | // Resort fields to put optional fields at the end of the method signature. |
||
714 | 1 | $requiredFields = array(); |
|
715 | 1 | $optionalFields = array(); |
|
716 | |||
717 | 1 | foreach ($metadata->fieldMappings as $fieldMapping) { |
|
718 | 1 | if (empty($fieldMapping['nullable'])) { |
|
719 | 1 | $requiredFields[] = $fieldMapping; |
|
720 | |||
721 | 1 | continue; |
|
722 | } |
||
723 | |||
724 | 1 | $optionalFields[] = $fieldMapping; |
|
725 | } |
||
726 | |||
727 | 1 | $fieldMappings = array_merge($requiredFields, $optionalFields); |
|
728 | |||
729 | 1 | foreach ($metadata->embeddedClasses as $fieldName => $embeddedClass) { |
|
730 | 1 | $paramType = '\\' . ltrim($embeddedClass['class'], '\\'); |
|
731 | 1 | $paramVariable = '$' . $fieldName; |
|
732 | |||
733 | 1 | $paramTypes[] = $paramType; |
|
734 | 1 | $paramVariables[] = $paramVariable; |
|
735 | 1 | $params[] = $paramType . ' ' . $paramVariable; |
|
736 | 1 | $fields[] = '$this->' . $fieldName . ' = ' . $paramVariable . ';'; |
|
737 | } |
||
738 | |||
739 | 1 | foreach ($fieldMappings as $fieldMapping) { |
|
740 | 1 | if (isset($fieldMapping['declaredField']) && |
|
741 | 1 | isset($metadata->embeddedClasses[$fieldMapping['declaredField']]) |
|
742 | ) { |
||
743 | continue; |
||
744 | } |
||
745 | |||
746 | 1 | $paramTypes[] = $this->getType($fieldMapping['type']) . (!empty($fieldMapping['nullable']) ? '|null' : ''); |
|
747 | 1 | $param = '$' . $fieldMapping['fieldName']; |
|
748 | 1 | $paramVariables[] = $param; |
|
749 | |||
750 | 1 | if ($fieldMapping['type'] === 'datetime') { |
|
751 | 1 | $param = $this->getType($fieldMapping['type']) . ' ' . $param; |
|
752 | } |
||
753 | |||
754 | 1 | if (!empty($fieldMapping['nullable'])) { |
|
755 | 1 | $param .= ' = null'; |
|
756 | } |
||
757 | |||
758 | 1 | $params[] = $param; |
|
759 | |||
760 | 1 | $fields[] = '$this->' . $fieldMapping['fieldName'] . ' = $' . $fieldMapping['fieldName'] . ';'; |
|
761 | } |
||
762 | |||
763 | 1 | $maxParamTypeLength = max(array_map('strlen', $paramTypes)); |
|
764 | 1 | $paramTags = array_map( |
|
765 | function ($type, $variable) use ($maxParamTypeLength) { |
||
766 | 1 | return '@param ' . $type . str_repeat(' ', $maxParamTypeLength - strlen($type) + 1) . $variable; |
|
767 | 1 | }, |
|
768 | $paramTypes, |
||
769 | $paramVariables |
||
770 | ); |
||
771 | |||
772 | // Generate multi line constructor if the signature exceeds 120 characters. |
||
773 | 1 | if (array_sum(array_map('strlen', $params)) + count($params) * 2 + 29 > 120) { |
|
774 | 1 | $delimiter = "\n" . $this->spaces; |
|
775 | 1 | $params = $delimiter . implode(',' . $delimiter, $params) . "\n"; |
|
776 | } else { |
||
777 | 1 | $params = implode(', ', $params); |
|
778 | } |
||
779 | |||
780 | $replacements = array( |
||
781 | 1 | '<paramTags>' => implode("\n * ", $paramTags), |
|
782 | 1 | '<params>' => $params, |
|
783 | 1 | '<fields>' => implode("\n" . $this->spaces, $fields), |
|
784 | ); |
||
785 | |||
786 | 1 | $constructor = str_replace( |
|
787 | array_keys($replacements), |
||
788 | array_values($replacements), |
||
789 | 1 | static::$embeddableConstructorMethodTemplate |
|
790 | ); |
||
791 | |||
792 | 1 | return $this->prefixCodeWithSpaces($constructor); |
|
793 | } |
||
794 | |||
795 | /** |
||
796 | * @todo this won't work if there is a namespace in brackets and a class outside of it. |
||
797 | * |
||
798 | * @param string $src |
||
799 | * |
||
800 | * @return void |
||
801 | */ |
||
802 | 8 | protected function parseTokensInEntityFile($src) |
|
803 | { |
||
804 | 8 | $tokens = token_get_all($src); |
|
805 | 8 | $lastSeenNamespace = ""; |
|
806 | 8 | $lastSeenClass = false; |
|
807 | |||
808 | 8 | $inNamespace = false; |
|
809 | 8 | $inClass = false; |
|
810 | |||
811 | 8 | for ($i = 0; $i < count($tokens); $i++) { |
|
812 | 8 | $token = $tokens[$i]; |
|
813 | 8 | if (in_array($token[0], array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT))) { |
|
814 | 8 | continue; |
|
815 | } |
||
816 | |||
817 | 8 | if ($inNamespace) { |
|
818 | 8 | if ($token[0] == T_NS_SEPARATOR || $token[0] == T_STRING) { |
|
819 | 8 | $lastSeenNamespace .= $token[1]; |
|
820 | 8 | } elseif (is_string($token) && in_array($token, array(';', '{'))) { |
|
821 | 8 | $inNamespace = false; |
|
822 | } |
||
823 | } |
||
824 | |||
825 | 8 | if ($inClass) { |
|
826 | 8 | $inClass = false; |
|
827 | 8 | $lastSeenClass = $lastSeenNamespace . ($lastSeenNamespace ? '\\' : '') . $token[1]; |
|
828 | 8 | $this->staticReflection[$lastSeenClass]['properties'] = array(); |
|
829 | 8 | $this->staticReflection[$lastSeenClass]['methods'] = array(); |
|
830 | } |
||
831 | |||
832 | 8 | if ($token[0] == T_NAMESPACE) { |
|
833 | 8 | $lastSeenNamespace = ""; |
|
834 | 8 | $inNamespace = true; |
|
835 | 8 | } elseif ($token[0] == T_CLASS && $tokens[$i-1][0] != T_DOUBLE_COLON) { |
|
836 | 8 | $inClass = true; |
|
837 | 8 | } elseif ($token[0] == T_FUNCTION) { |
|
838 | 3 | if ($tokens[$i+2][0] == T_STRING) { |
|
839 | 3 | $this->staticReflection[$lastSeenClass]['methods'][] = strtolower($tokens[$i+2][1]); |
|
840 | } elseif ($tokens[$i+2] == "&" && $tokens[$i+3][0] == T_STRING) { |
||
841 | 3 | $this->staticReflection[$lastSeenClass]['methods'][] = strtolower($tokens[$i+3][1]); |
|
842 | } |
||
843 | 8 | } elseif (in_array($token[0], array(T_VAR, T_PUBLIC, T_PRIVATE, T_PROTECTED)) && $tokens[$i+2][0] != T_FUNCTION) { |
|
844 | 4 | $this->staticReflection[$lastSeenClass]['properties'][] = substr($tokens[$i+2][1], 1); |
|
845 | } |
||
846 | } |
||
847 | 8 | } |
|
848 | |||
849 | /** |
||
850 | * @param string $property |
||
851 | * @param ClassMetadataInfo $metadata |
||
852 | * |
||
853 | * @return bool |
||
854 | */ |
||
855 | 31 | protected function hasProperty($property, ClassMetadataInfo $metadata) |
|
856 | { |
||
857 | 31 | if ($this->extendsClass() || (!$this->isNew && class_exists($metadata->name))) { |
|
858 | // don't generate property if its already on the base class. |
||
859 | 2 | $reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name); |
|
860 | 2 | if ($reflClass->hasProperty($property)) { |
|
861 | 1 | return true; |
|
862 | } |
||
863 | } |
||
864 | |||
865 | // check traits for existing property |
||
866 | 30 | foreach ($this->getTraits($metadata) as $trait) { |
|
867 | 2 | if ($trait->hasProperty($property)) { |
|
868 | 2 | return true; |
|
869 | } |
||
870 | } |
||
871 | |||
872 | return ( |
||
873 | 30 | isset($this->staticReflection[$metadata->name]) && |
|
874 | 30 | in_array($property, $this->staticReflection[$metadata->name]['properties']) |
|
875 | ); |
||
876 | } |
||
877 | |||
878 | /** |
||
879 | * @param string $method |
||
880 | * @param ClassMetadataInfo $metadata |
||
881 | * |
||
882 | * @return bool |
||
883 | */ |
||
884 | 32 | protected function hasMethod($method, ClassMetadataInfo $metadata) |
|
885 | { |
||
886 | 32 | if ($this->extendsClass() || (!$this->isNew && class_exists($metadata->name))) { |
|
887 | // don't generate method if its already on the base class. |
||
888 | 2 | $reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name); |
|
889 | |||
890 | 2 | if ($reflClass->hasMethod($method)) { |
|
891 | 1 | return true; |
|
892 | } |
||
893 | } |
||
894 | |||
895 | // check traits for existing method |
||
896 | 32 | foreach ($this->getTraits($metadata) as $trait) { |
|
897 | 2 | if ($trait->hasMethod($method)) { |
|
898 | 2 | return true; |
|
899 | } |
||
900 | } |
||
901 | |||
902 | return ( |
||
903 | 32 | isset($this->staticReflection[$metadata->name]) && |
|
904 | 32 | in_array(strtolower($method), $this->staticReflection[$metadata->name]['methods']) |
|
905 | ); |
||
906 | } |
||
907 | |||
908 | /** |
||
909 | * @param ClassMetadataInfo $metadata |
||
910 | * |
||
911 | * @return array |
||
912 | */ |
||
913 | 32 | protected function getTraits(ClassMetadataInfo $metadata) |
|
914 | { |
||
915 | 32 | if (! ($metadata->reflClass !== null || class_exists($metadata->name))) { |
|
916 | 26 | return []; |
|
917 | } |
||
918 | |||
919 | 7 | $reflClass = $metadata->reflClass === null |
|
920 | 1 | ? new \ReflectionClass($metadata->name) |
|
921 | 7 | : $metadata->reflClass; |
|
922 | |||
923 | 7 | $traits = array(); |
|
924 | |||
925 | 7 | while ($reflClass !== false) { |
|
926 | 7 | $traits = array_merge($traits, $reflClass->getTraits()); |
|
927 | |||
928 | 7 | $reflClass = $reflClass->getParentClass(); |
|
929 | } |
||
930 | |||
931 | 7 | return $traits; |
|
932 | } |
||
933 | |||
934 | /** |
||
935 | * @param ClassMetadataInfo $metadata |
||
936 | * |
||
937 | * @return bool |
||
938 | */ |
||
939 | 31 | protected function hasNamespace(ClassMetadataInfo $metadata) |
|
943 | |||
944 | /** |
||
945 | * @return bool |
||
946 | */ |
||
947 | 32 | protected function extendsClass() |
|
951 | |||
952 | /** |
||
953 | * @return string |
||
954 | */ |
||
955 | 2 | protected function getClassToExtend() |
|
959 | |||
960 | /** |
||
961 | * @return string |
||
962 | */ |
||
963 | 1 | protected function getClassToExtendName() |
|
964 | { |
||
965 | 1 | $refl = new \ReflectionClass($this->getClassToExtend()); |
|
966 | |||
967 | 1 | return '\\' . $refl->getName(); |
|
968 | } |
||
969 | |||
970 | /** |
||
971 | * @param ClassMetadataInfo $metadata |
||
972 | * |
||
973 | * @return string |
||
974 | */ |
||
975 | 32 | protected function getClassName(ClassMetadataInfo $metadata) |
|
976 | { |
||
977 | 32 | return ($pos = strrpos($metadata->name, '\\')) |
|
978 | 32 | ? substr($metadata->name, $pos + 1, strlen($metadata->name)) : $metadata->name; |
|
979 | } |
||
980 | |||
981 | /** |
||
982 | * @param ClassMetadataInfo $metadata |
||
983 | * |
||
984 | * @return string |
||
985 | */ |
||
986 | 31 | protected function getNamespace(ClassMetadataInfo $metadata) |
|
990 | |||
991 | /** |
||
992 | * @param ClassMetadataInfo $metadata |
||
993 | * |
||
994 | * @return string |
||
995 | */ |
||
996 | 31 | protected function generateEntityDocBlock(ClassMetadataInfo $metadata) |
|
997 | { |
||
998 | 31 | $lines = array(); |
|
999 | 31 | $lines[] = '/**'; |
|
1000 | 31 | $lines[] = ' * ' . $this->getClassName($metadata); |
|
1001 | |||
1002 | 31 | if ($this->generateAnnotations) { |
|
1003 | 31 | $lines[] = ' *'; |
|
1004 | |||
1005 | $methods = array( |
||
1006 | 31 | 'generateTableAnnotation', |
|
1007 | 'generateInheritanceAnnotation', |
||
1008 | 'generateDiscriminatorColumnAnnotation', |
||
1009 | 'generateDiscriminatorMapAnnotation', |
||
1010 | 'generateEntityAnnotation', |
||
1011 | ); |
||
1012 | |||
1013 | 31 | foreach ($methods as $method) { |
|
1014 | 31 | if ($code = $this->$method($metadata)) { |
|
1015 | 31 | $lines[] = ' * ' . $code; |
|
1016 | } |
||
1017 | } |
||
1018 | |||
1019 | 31 | if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) { |
|
1020 | 12 | $lines[] = ' * @' . $this->annotationsPrefix . 'HasLifecycleCallbacks'; |
|
1021 | } |
||
1022 | } |
||
1023 | |||
1024 | 31 | $lines[] = ' */'; |
|
1025 | |||
1026 | 31 | return implode("\n", $lines); |
|
1027 | } |
||
1028 | |||
1029 | /** |
||
1030 | * @param ClassMetadataInfo $metadata |
||
1031 | * |
||
1032 | * @return string |
||
1033 | */ |
||
1034 | 31 | protected function generateEntityAnnotation(ClassMetadataInfo $metadata) |
|
1048 | |||
1049 | /** |
||
1050 | * @param ClassMetadataInfo $metadata |
||
1051 | * |
||
1052 | * @return string |
||
1053 | */ |
||
1054 | 31 | protected function generateTableAnnotation($metadata) |
|
1055 | { |
||
1056 | 31 | if ($metadata->isEmbeddedClass) { |
|
1057 | 9 | return ''; |
|
1058 | } |
||
1059 | |||
1060 | 29 | $table = array(); |
|
1061 | |||
1062 | 29 | if (isset($metadata->table['schema'])) { |
|
1063 | $table[] = 'schema="' . $metadata->table['schema'] . '"'; |
||
1064 | } |
||
1065 | |||
1066 | 29 | if (isset($metadata->table['name'])) { |
|
1086 | |||
1087 | /** |
||
1088 | * @param string $constraintName |
||
1089 | * @param array $constraints |
||
1090 | * |
||
1091 | * @return string |
||
1092 | */ |
||
1093 | 9 | protected function generateTableConstraints($constraintName, $constraints) |
|
1106 | |||
1107 | /** |
||
1108 | * @param ClassMetadataInfo $metadata |
||
1109 | * |
||
1110 | * @return string |
||
1111 | */ |
||
1112 | 31 | protected function generateInheritanceAnnotation($metadata) |
|
1118 | |||
1119 | /** |
||
1120 | * @param ClassMetadataInfo $metadata |
||
1121 | * |
||
1122 | * @return string |
||
1123 | */ |
||
1124 | 31 | protected function generateDiscriminatorColumnAnnotation($metadata) |
|
1135 | |||
1136 | /** |
||
1137 | * @param ClassMetadataInfo $metadata |
||
1138 | * |
||
1139 | * @return string |
||
1140 | */ |
||
1141 | 31 | protected function generateDiscriminatorMapAnnotation($metadata) |
|
1153 | |||
1154 | /** |
||
1155 | * @param ClassMetadataInfo $metadata |
||
1156 | * |
||
1157 | * @return string |
||
1158 | */ |
||
1159 | 31 | protected function generateEntityStubMethods(ClassMetadataInfo $metadata) |
|
1225 | |||
1226 | /** |
||
1227 | * @param array $associationMapping |
||
1228 | * |
||
1229 | * @return bool |
||
1230 | */ |
||
1231 | 11 | protected function isAssociationIsNullable($associationMapping) |
|
1252 | |||
1253 | /** |
||
1254 | * @param ClassMetadataInfo $metadata |
||
1255 | * |
||
1256 | * @return string |
||
1257 | */ |
||
1258 | 32 | protected function generateEntityLifecycleCallbackMethods(ClassMetadataInfo $metadata) |
|
1272 | |||
1273 | /** |
||
1274 | * @param ClassMetadataInfo $metadata |
||
1275 | * |
||
1276 | * @return string |
||
1277 | */ |
||
1278 | 32 | protected function generateEntityAssociationMappingProperties(ClassMetadataInfo $metadata) |
|
1294 | |||
1295 | /** |
||
1296 | * @param ClassMetadataInfo $metadata |
||
1297 | * |
||
1298 | * @return string |
||
1299 | */ |
||
1300 | 32 | protected function generateEntityFieldMappingProperties(ClassMetadataInfo $metadata) |
|
1322 | |||
1323 | /** |
||
1324 | * @param ClassMetadataInfo $metadata |
||
1325 | * |
||
1326 | * @return string |
||
1327 | */ |
||
1328 | 32 | protected function generateEntityEmbeddedProperties(ClassMetadataInfo $metadata) |
|
1343 | |||
1344 | /** |
||
1345 | * @param ClassMetadataInfo $metadata |
||
1346 | * @param string $type |
||
1347 | * @param string $fieldName |
||
1348 | * @param string|null $typeHint |
||
1349 | * @param string|null $defaultValue |
||
1350 | * |
||
1351 | * @return string |
||
1352 | */ |
||
1353 | 30 | protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) |
|
1398 | |||
1399 | /** |
||
1400 | * @param string[] $lifecycleNames |
||
1401 | * @param string $methodName |
||
1402 | * @param ClassMetadataInfo $metadata |
||
1403 | * |
||
1404 | * @return string |
||
1405 | */ |
||
1406 | 12 | protected function generateLifecycleCallbacks($lifecycleNames, $methodName, $metadata) |
|
1436 | |||
1437 | /** |
||
1438 | * @param array $joinColumn |
||
1439 | * |
||
1440 | * @return string |
||
1441 | */ |
||
1442 | 11 | protected function generateJoinColumnAnnotation(array $joinColumn) |
|
1472 | |||
1473 | /** |
||
1474 | * @param array $associationMapping |
||
1475 | * @param ClassMetadataInfo $metadata |
||
1476 | * |
||
1477 | * @return string |
||
1478 | */ |
||
1479 | 11 | protected function generateAssociationMappingPropertyDocBlock(array $associationMapping, ClassMetadataInfo $metadata) |
|
1624 | |||
1625 | /** |
||
1626 | * @param array $fieldMapping |
||
1627 | * @param ClassMetadataInfo $metadata |
||
1628 | * |
||
1629 | * @return string |
||
1630 | */ |
||
1631 | 29 | protected function generateFieldMappingPropertyDocBlock(array $fieldMapping, ClassMetadataInfo $metadata) |
|
1720 | |||
1721 | /** |
||
1722 | * @param array $embeddedClass |
||
1723 | * |
||
1724 | * @return string |
||
1725 | */ |
||
1726 | 8 | protected function generateEmbeddedPropertyDocBlock(array $embeddedClass) |
|
1749 | |||
1750 | /** |
||
1751 | * @param string $code |
||
1752 | * @param int $num |
||
1753 | * |
||
1754 | * @return string |
||
1755 | */ |
||
1756 | 31 | protected function prefixCodeWithSpaces($code, $num = 1) |
|
1768 | |||
1769 | /** |
||
1770 | * @param integer $type The inheritance type used by the class and its subclasses. |
||
1771 | * |
||
1772 | * @return string The literal string for the inheritance type. |
||
1773 | * |
||
1774 | * @throws \InvalidArgumentException When the inheritance type does not exist. |
||
1775 | */ |
||
1776 | 1 | protected function getInheritanceTypeString($type) |
|
1784 | |||
1785 | /** |
||
1786 | * @param integer $type The policy used for change-tracking for the mapped class. |
||
1787 | * |
||
1788 | * @return string The literal string for the change-tracking type. |
||
1789 | * |
||
1790 | * @throws \InvalidArgumentException When the change-tracking type does not exist. |
||
1791 | */ |
||
1792 | 1 | protected function getChangeTrackingPolicyString($type) |
|
1800 | |||
1801 | /** |
||
1802 | * @param integer $type The generator to use for the mapped class. |
||
1803 | * |
||
1804 | * @return string The literal string for the generator type. |
||
1805 | * |
||
1806 | * @throws \InvalidArgumentException When the generator type does not exist. |
||
1807 | */ |
||
1808 | 28 | protected function getIdGeneratorTypeString($type) |
|
1816 | |||
1817 | /** |
||
1818 | * Exports (nested) option elements. |
||
1819 | * |
||
1820 | * @param array $options |
||
1821 | * |
||
1822 | * @return string |
||
1823 | */ |
||
1824 | 1 | private function exportTableOptions(array $options) |
|
1838 | |||
1839 | /** |
||
1840 | * Get lifecycle callback methods grouped by method name |
||
1841 | * |
||
1842 | * @param ClassMetadataInfo $metadata |
||
1843 | * |
||
1844 | * @return array |
||
1845 | */ |
||
1846 | 32 | private function getLifecycleCallbackMethods(ClassMetadataInfo $metadata) |
|
1862 | } |
||
1863 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: