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 | * Whether or not to add PHP strict types. |
||
151 | * |
||
152 | * @var boolean |
||
153 | */ |
||
154 | protected $strictTypes = false; |
||
155 | |||
156 | /** |
||
157 | * Whether or not to force type hinting for method result and scalar parameters. |
||
158 | * |
||
159 | * @var boolean |
||
160 | */ |
||
161 | protected $generateMethodsTypeHinting = false; |
||
162 | |||
163 | /** |
||
164 | * Hash-map for handle types. |
||
165 | * |
||
166 | * @var array |
||
167 | */ |
||
168 | protected $typeAlias = array( |
||
169 | Type::DATETIMETZ => '\DateTime', |
||
170 | Type::DATETIME => '\DateTime', |
||
171 | Type::DATE => '\DateTime', |
||
172 | Type::TIME => '\DateTime', |
||
173 | Type::OBJECT => '\stdClass', |
||
174 | Type::INTEGER => 'int', |
||
175 | Type::BIGINT => 'int', |
||
176 | Type::SMALLINT => 'int', |
||
177 | Type::TEXT => 'string', |
||
178 | Type::BLOB => 'string', |
||
179 | Type::DECIMAL => 'string', |
||
180 | Type::JSON_ARRAY => 'array', |
||
181 | Type::SIMPLE_ARRAY => 'array', |
||
182 | Type::BOOLEAN => 'bool', |
||
183 | ); |
||
184 | |||
185 | /** |
||
186 | * Hash-map for handle strict types |
||
187 | * |
||
188 | * @var array |
||
189 | */ |
||
190 | protected $typeHintingAlias = array( |
||
191 | Type::TARRAY => 'array', |
||
192 | Type::SIMPLE_ARRAY => 'array', |
||
193 | Type::JSON_ARRAY => 'array', |
||
194 | Type::STRING => 'string', |
||
195 | Type::FLOAT => 'float', |
||
196 | Type::GUID => 'string', |
||
197 | ); |
||
198 | |||
199 | /** |
||
200 | * Hash-map to handle generator types string. |
||
201 | * |
||
202 | * @var array |
||
203 | */ |
||
204 | protected static $generatorStrategyMap = array( |
||
205 | ClassMetadataInfo::GENERATOR_TYPE_AUTO => 'AUTO', |
||
206 | ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE => 'SEQUENCE', |
||
207 | ClassMetadataInfo::GENERATOR_TYPE_TABLE => 'TABLE', |
||
208 | ClassMetadataInfo::GENERATOR_TYPE_IDENTITY => 'IDENTITY', |
||
209 | ClassMetadataInfo::GENERATOR_TYPE_NONE => 'NONE', |
||
210 | ClassMetadataInfo::GENERATOR_TYPE_UUID => 'UUID', |
||
211 | ClassMetadataInfo::GENERATOR_TYPE_CUSTOM => 'CUSTOM' |
||
212 | ); |
||
213 | |||
214 | /** |
||
215 | * Hash-map to handle the change tracking policy string. |
||
216 | * |
||
217 | * @var array |
||
218 | */ |
||
219 | protected static $changeTrackingPolicyMap = array( |
||
220 | ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT => 'DEFERRED_IMPLICIT', |
||
221 | ClassMetadataInfo::CHANGETRACKING_DEFERRED_EXPLICIT => 'DEFERRED_EXPLICIT', |
||
222 | ClassMetadataInfo::CHANGETRACKING_NOTIFY => 'NOTIFY', |
||
223 | ); |
||
224 | |||
225 | /** |
||
226 | * Hash-map to handle the inheritance type string. |
||
227 | * |
||
228 | * @var array |
||
229 | */ |
||
230 | protected static $inheritanceTypeMap = array( |
||
231 | ClassMetadataInfo::INHERITANCE_TYPE_NONE => 'NONE', |
||
232 | ClassMetadataInfo::INHERITANCE_TYPE_JOINED => 'JOINED', |
||
233 | ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE => 'SINGLE_TABLE', |
||
234 | ClassMetadataInfo::INHERITANCE_TYPE_TABLE_PER_CLASS => 'TABLE_PER_CLASS', |
||
235 | ); |
||
236 | |||
237 | /** |
||
238 | * @var string |
||
239 | */ |
||
240 | protected static $classTemplate = |
||
241 | '<?php |
||
242 | <strictTypes> |
||
243 | <namespace> |
||
244 | <useStatement> |
||
245 | <entityAnnotation> |
||
246 | <entityClassName> |
||
247 | { |
||
248 | <entityBody> |
||
249 | } |
||
250 | '; |
||
251 | |||
252 | /** |
||
253 | * @var string |
||
254 | */ |
||
255 | protected static $getMethodTemplate = |
||
256 | '/** |
||
257 | * <description> |
||
258 | * |
||
259 | * @return <variableType> |
||
260 | */ |
||
261 | public function <methodName>() <methodTypeReturn> |
||
262 | { |
||
263 | <spaces>return $this-><fieldName>; |
||
264 | }'; |
||
265 | |||
266 | /** |
||
267 | * @var string |
||
268 | */ |
||
269 | protected static $setMethodTemplate = |
||
270 | '/** |
||
271 | * <description> |
||
272 | * |
||
273 | * @param <variableType> $<variableName> |
||
274 | * |
||
275 | * @return <entity> |
||
276 | */ |
||
277 | public function <methodName>(<methodTypeHint>$<variableName><variableDefault>) <methodTypeReturn> |
||
278 | { |
||
279 | <spaces>$this-><fieldName> = $<variableName>; |
||
280 | |||
281 | <spaces>return $this; |
||
282 | }'; |
||
283 | |||
284 | /** |
||
285 | * @var string |
||
286 | */ |
||
287 | protected static $addMethodTemplate = |
||
288 | '/** |
||
289 | * <description> |
||
290 | * |
||
291 | * @param <variableType> $<variableName> |
||
292 | * |
||
293 | * @return <entity> |
||
294 | */ |
||
295 | public function <methodName>(<methodTypeHint>$<variableName>) <methodTypeReturn> |
||
296 | { |
||
297 | <spaces>$this-><fieldName>[] = $<variableName>; |
||
298 | |||
299 | <spaces>return $this; |
||
300 | }'; |
||
301 | |||
302 | /** |
||
303 | * @var string |
||
304 | */ |
||
305 | protected static $removeMethodTemplate = |
||
306 | '/** |
||
307 | * <description> |
||
308 | * |
||
309 | * @param <variableType> $<variableName> |
||
310 | * |
||
311 | * @return boolean TRUE if this collection contained the specified element, FALSE otherwise. |
||
312 | */ |
||
313 | public function <methodName>(<methodTypeHint>$<variableName>) <methodTypeReturn> |
||
314 | { |
||
315 | <spaces>return $this-><fieldName>->removeElement($<variableName>); |
||
316 | }'; |
||
317 | |||
318 | /** |
||
319 | * @var string |
||
320 | */ |
||
321 | protected static $lifecycleCallbackMethodTemplate = |
||
322 | '/** |
||
323 | * @<name> |
||
324 | */ |
||
325 | public function <methodName>() |
||
326 | { |
||
327 | <spaces>// Add your code here |
||
328 | }'; |
||
329 | |||
330 | /** |
||
331 | * @var string |
||
332 | */ |
||
333 | 39 | protected static $constructorMethodTemplate = |
|
334 | '/** |
||
335 | 39 | * Constructor |
|
336 | 39 | */ |
|
337 | public function __construct() |
||
338 | 39 | { |
|
339 | <spaces><collections> |
||
340 | } |
||
341 | '; |
||
342 | |||
343 | /** |
||
344 | * @var string |
||
345 | */ |
||
346 | protected static $embeddableConstructorMethodTemplate = |
||
347 | '/** |
||
348 | * Constructor |
||
349 | * |
||
350 | * <paramTags> |
||
351 | */ |
||
352 | public function __construct(<params>) |
||
353 | { |
||
354 | <spaces><fields> |
||
355 | } |
||
356 | '; |
||
357 | |||
358 | /** |
||
359 | * Constructor. |
||
360 | */ |
||
361 | public function __construct() |
||
362 | { |
||
363 | if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) { |
||
364 | $this->annotationsPrefix = 'ORM\\'; |
||
365 | 29 | } |
|
366 | } |
||
367 | 29 | ||
368 | 29 | /** |
|
369 | * Generates and writes entity classes for the given array of ClassMetadataInfo instances. |
||
370 | 29 | * |
|
371 | 2 | * @param array $metadatas |
|
372 | * @param string $outputDirectory |
||
373 | * |
||
374 | 29 | * @return void |
|
375 | */ |
||
376 | 29 | public function generate(array $metadatas, $outputDirectory) |
|
377 | 3 | { |
|
378 | foreach ($metadatas as $metadata) { |
||
379 | 28 | $this->writeEntityClass($metadata, $outputDirectory); |
|
380 | } |
||
381 | } |
||
382 | 29 | ||
383 | 3 | /** |
|
384 | 3 | * Generates and writes entity class to disk for the given ClassMetadataInfo instance. |
|
385 | * |
||
386 | * @param ClassMetadataInfo $metadata |
||
387 | * @param string $outputDirectory |
||
388 | * |
||
389 | * @return void |
||
390 | 29 | * |
|
391 | 28 | * @throws \RuntimeException |
|
392 | */ |
||
393 | 3 | public function writeEntityClass(ClassMetadataInfo $metadata, $outputDirectory) |
|
394 | 3 | { |
|
395 | $path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->extension; |
||
396 | 29 | $dir = dirname($path); |
|
397 | 29 | ||
398 | if ( ! is_dir($dir)) { |
||
399 | mkdir($dir, 0775, true); |
||
400 | } |
||
401 | |||
402 | $this->isNew = !file_exists($path) || (file_exists($path) && $this->regenerateEntityIfExists); |
||
403 | |||
404 | if ( ! $this->isNew) { |
||
405 | $this->parseTokensInEntityFile(file_get_contents($path)); |
||
406 | 29 | } else { |
|
407 | $this->staticReflection[$metadata->name] = array('properties' => array(), 'methods' => array()); |
||
408 | } |
||
409 | 29 | ||
410 | if ($this->backupExisting && file_exists($path)) { |
||
411 | $backupPath = dirname($path) . DIRECTORY_SEPARATOR . basename($path) . "~"; |
||
412 | if (!copy($path, $backupPath)) { |
||
413 | throw new \RuntimeException("Attempt to backup overwritten entity file but copy operation failed."); |
||
414 | } |
||
415 | } |
||
416 | |||
417 | 29 | // If entity doesn't exist or we're re-generating the entities entirely |
|
418 | 29 | if ($this->isNew) { |
|
419 | 29 | file_put_contents($path, $this->generateEntityClass($metadata)); |
|
420 | 29 | // If entity exists and we're allowed to update the entity class |
|
421 | 29 | } elseif ( ! $this->isNew && $this->updateEntityIfExists) { |
|
422 | file_put_contents($path, $this->generateUpdatedEntityClass($metadata, $path)); |
||
423 | } |
||
424 | 29 | chmod($path, 0664); |
|
425 | } |
||
426 | 29 | ||
427 | /** |
||
428 | * Generates a PHP5 Doctrine 2 entity class from the given ClassMetadataInfo instance. |
||
429 | * |
||
430 | * @param ClassMetadataInfo $metadata |
||
431 | * |
||
432 | * @return string |
||
433 | */ |
||
434 | public function generateEntityClass(ClassMetadataInfo $metadata) |
||
435 | { |
||
436 | $placeHolders = array( |
||
437 | 3 | '<strictTypes>', |
|
438 | '<namespace>', |
||
439 | 3 | '<useStatement>', |
|
440 | '<entityAnnotation>', |
||
441 | 3 | '<entityClassName>', |
|
442 | 3 | '<entityBody>' |
|
443 | 3 | ); |
|
444 | |||
445 | 3 | $replacements = array( |
|
446 | $this->generateEntityStrictTypes(), |
||
447 | $this->generateEntityNamespace($metadata), |
||
448 | $this->generateEntityUse(), |
||
449 | $this->generateEntityDocBlock($metadata), |
||
450 | $this->generateEntityClassName($metadata), |
||
451 | $this->generateEntityBody($metadata) |
||
452 | ); |
||
453 | |||
454 | $code = str_replace($placeHolders, $replacements, static::$classTemplate) . "\n"; |
||
455 | |||
456 | return str_replace('<spaces>', $this->spaces, $code); |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * Generates the updated code for the given ClassMetadataInfo and entity at path. |
||
461 | * |
||
462 | * @param ClassMetadataInfo $metadata |
||
463 | * @param string $path |
||
464 | * |
||
465 | * @return string |
||
466 | */ |
||
467 | public function generateUpdatedEntityClass(ClassMetadataInfo $metadata, $path) |
||
468 | { |
||
469 | $currentCode = file_get_contents($path); |
||
470 | |||
471 | $body = $this->generateEntityBody($metadata); |
||
472 | $body = str_replace('<spaces>', $this->spaces, $body); |
||
473 | $last = strrpos($currentCode, '}'); |
||
474 | |||
475 | return substr($currentCode, 0, $last) . $body . (strlen($body) > 0 ? "\n" : '') . "}\n"; |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * Sets the number of spaces the exported class should have. |
||
480 | 1 | * |
|
481 | * @param integer $numSpaces |
||
482 | 1 | * |
|
483 | 1 | * @return void |
|
484 | */ |
||
485 | public function setNumSpaces($numSpaces) |
||
486 | { |
||
487 | $this->spaces = str_repeat(' ', $numSpaces); |
||
488 | $this->numSpaces = $numSpaces; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | 38 | * Sets the extension to use when writing php files to disk. |
|
493 | * |
||
494 | 38 | * @param string $extension |
|
495 | 38 | * |
|
496 | * @return void |
||
497 | */ |
||
498 | public function setExtension($extension) |
||
499 | { |
||
500 | $this->extension = $extension; |
||
501 | } |
||
502 | |||
503 | /** |
||
504 | * Sets the name of the class the generated classes should extend from. |
||
505 | * |
||
506 | 37 | * @param string $classToExtend |
|
507 | * |
||
508 | 37 | * @return void |
|
509 | */ |
||
510 | public function setClassToExtend($classToExtend) |
||
511 | { |
||
512 | 37 | $this->classToExtend = $classToExtend; |
|
513 | 37 | } |
|
514 | |||
515 | /** |
||
516 | * Sets whether or not to generate annotations for the entity. |
||
517 | * |
||
518 | * @param bool $bool |
||
519 | * |
||
520 | 1 | * @return void |
|
521 | */ |
||
522 | 1 | public function setGenerateAnnotations($bool) |
|
523 | 1 | { |
|
524 | $this->generateAnnotations = $bool; |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * Sets the class fields visibility for the entity (can either be private or protected). |
||
529 | * |
||
530 | * @param bool $visibility |
||
531 | * |
||
532 | 38 | * @return void |
|
533 | * |
||
534 | 38 | * @throws \InvalidArgumentException |
|
535 | 38 | */ |
|
536 | public function setFieldVisibility($visibility) |
||
537 | { |
||
538 | if ($visibility !== static::FIELD_VISIBLE_PRIVATE && $visibility !== static::FIELD_VISIBLE_PROTECTED) { |
||
539 | throw new \InvalidArgumentException('Invalid provided visibility (only private and protected are allowed): ' . $visibility); |
||
540 | } |
||
541 | |||
542 | $this->fieldVisibility = $visibility; |
||
543 | } |
||
544 | 38 | ||
545 | /** |
||
546 | 38 | * Sets whether or not to generate immutable embeddables. |
|
547 | 38 | * |
|
548 | * @param boolean $embeddablesImmutable |
||
549 | */ |
||
550 | public function setEmbeddablesImmutable($embeddablesImmutable) |
||
551 | { |
||
552 | $this->embeddablesImmutable = (boolean) $embeddablesImmutable; |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | 38 | * Sets an annotation prefix. |
|
557 | * |
||
558 | 38 | * @param string $prefix |
|
559 | 38 | * |
|
560 | * @return void |
||
561 | */ |
||
562 | public function setAnnotationPrefix($prefix) |
||
563 | { |
||
564 | $this->annotationsPrefix = $prefix; |
||
565 | } |
||
566 | |||
567 | /** |
||
568 | 38 | * Sets whether or not to try and update the entity if it already exists. |
|
569 | * |
||
570 | 38 | * @param bool $bool |
|
571 | 38 | * |
|
572 | * @return void |
||
573 | */ |
||
574 | public function setUpdateEntityIfExists($bool) |
||
575 | { |
||
576 | $this->updateEntityIfExists = $bool; |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | 1 | * Sets whether or not to regenerate the entity if it exists. |
|
581 | * |
||
582 | 1 | * @param bool $bool |
|
583 | 1 | * |
|
584 | * @return void |
||
585 | */ |
||
586 | public function setRegenerateEntityIfExists($bool) |
||
587 | { |
||
588 | $this->regenerateEntityIfExists = $bool; |
||
589 | } |
||
590 | 29 | ||
591 | /** |
||
592 | 29 | * Sets whether or not to generate stub methods for the entity. |
|
593 | 28 | * |
|
594 | * @param bool $bool |
||
595 | * |
||
596 | 18 | * @return void |
|
597 | */ |
||
598 | public function setGenerateStubMethods($bool) |
||
599 | { |
||
600 | $this->generateEntityStubMethods = $bool; |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | 29 | * Should an existing entity be backed up if it already exists? |
|
605 | * |
||
606 | 29 | * @param bool $bool |
|
607 | 29 | * |
|
608 | * @return void |
||
609 | 2 | */ |
|
610 | public function setBackupExisting($bool) |
||
611 | 29 | { |
|
612 | $this->backupExisting = $bool; |
||
613 | 29 | } |
|
614 | 29 | ||
615 | /** |
||
616 | * Set whether or not to add PHP strict types. |
||
617 | * |
||
618 | * @param bool $bool |
||
619 | * |
||
620 | * @return void |
||
621 | */ |
||
622 | public function setStrictTypes($bool) |
||
623 | { |
||
624 | $this->strictTypes = $bool; |
||
625 | 29 | } |
|
626 | |||
627 | 29 | /** |
|
628 | 29 | * Set whether or not to force type hinting for method result and scalar parameters. |
|
629 | * |
||
630 | * @param bool $bool |
||
631 | * |
||
632 | * @param void |
||
633 | */ |
||
634 | public function setGenerateMethodsTypeHinting($bool) |
||
635 | { |
||
636 | 30 | $this->generateMethodsTypeHinting = $bool; |
|
637 | } |
||
638 | 30 | ||
639 | 30 | /** |
|
640 | 30 | * @param string $type |
|
641 | 30 | * |
|
642 | 30 | * @return string |
|
643 | */ |
||
644 | 30 | protected function getType($type) |
|
645 | { |
||
646 | 30 | if (isset($this->typeAlias[$type])) { |
|
647 | 27 | return $this->typeAlias[$type]; |
|
648 | } |
||
649 | |||
650 | 30 | return $type; |
|
651 | 8 | } |
|
652 | |||
653 | /** |
||
654 | 30 | * @return string |
|
655 | 11 | */ |
|
656 | protected function generateEntityStrictTypes() |
||
657 | { |
||
658 | 30 | if ($this->strictTypes) { |
|
659 | return "\n".'declare(strict_types=1);'."\n"; |
||
660 | 30 | } |
|
661 | 28 | ||
662 | return ''; |
||
663 | } |
||
664 | 30 | ||
665 | 10 | /** |
|
666 | * @param ClassMetadataInfo $metadata |
||
667 | * |
||
668 | 30 | * @return string |
|
669 | */ |
||
670 | protected function generateEntityNamespace(ClassMetadataInfo $metadata) |
||
671 | { |
||
672 | if ($this->hasNamespace($metadata)) { |
||
673 | return 'namespace ' . $this->getNamespace($metadata) .';'; |
||
674 | } |
||
675 | } |
||
676 | 30 | ||
677 | protected function generateEntityUse() |
||
678 | 30 | { |
|
679 | 2 | if ($this->generateAnnotations) { |
|
680 | return "\n".'use Doctrine\ORM\Mapping as ORM;'."\n"; |
||
681 | } else { |
||
682 | 30 | return ""; |
|
683 | 1 | } |
|
684 | } |
||
685 | |||
686 | 29 | /** |
|
687 | * @param ClassMetadataInfo $metadata |
||
688 | 29 | * |
|
689 | 13 | * @return string |
|
690 | 13 | */ |
|
691 | protected function generateEntityClassName(ClassMetadataInfo $metadata) |
||
692 | { |
||
693 | return 'class ' . $this->getClassName($metadata) . |
||
694 | 29 | ($this->extendsClass() ? ' extends ' . $this->getClassToExtendName() : null); |
|
695 | 11 | } |
|
696 | |||
697 | /** |
||
698 | 25 | * @param ClassMetadataInfo $metadata |
|
699 | * |
||
700 | * @return string |
||
701 | */ |
||
702 | protected function generateEntityBody(ClassMetadataInfo $metadata) |
||
736 | 1 | ||
737 | /** |
||
738 | * @param ClassMetadataInfo $metadata |
||
739 | 1 | * |
|
740 | 1 | * @return string |
|
741 | 1 | */ |
|
742 | protected function generateEntityConstructor(ClassMetadataInfo $metadata) |
||
743 | { |
||
744 | if ($this->hasMethod('__construct', $metadata)) { |
||
745 | return ''; |
||
746 | 1 | } |
|
747 | 1 | ||
748 | 1 | if ($metadata->isEmbeddedClass && $this->embeddablesImmutable) { |
|
749 | return $this->generateEmbeddableConstructor($metadata); |
||
750 | 1 | } |
|
751 | 1 | ||
752 | $collections = array(); |
||
753 | |||
754 | 1 | foreach ($metadata->associationMappings as $mapping) { |
|
755 | 1 | if ($mapping['type'] & ClassMetadataInfo::TO_MANY) { |
|
756 | $collections[] = '$this->'.$mapping['fieldName'].' = new \Doctrine\Common\Collections\ArrayCollection();'; |
||
757 | } |
||
758 | 1 | } |
|
759 | |||
760 | 1 | if ($collections) { |
|
761 | return $this->prefixCodeWithSpaces(str_replace("<collections>", implode("\n".$this->spaces, $collections), static::$constructorMethodTemplate)); |
||
762 | } |
||
763 | 1 | ||
764 | 1 | return ''; |
|
765 | 1 | } |
|
766 | 1 | ||
767 | 1 | /** |
|
768 | * @param ClassMetadataInfo $metadata |
||
769 | * |
||
770 | * @return string |
||
771 | */ |
||
772 | private function generateEmbeddableConstructor(ClassMetadataInfo $metadata) |
||
773 | 1 | { |
|
774 | 1 | $paramTypes = array(); |
|
775 | 1 | $paramVariables = array(); |
|
776 | $params = array(); |
||
777 | 1 | $fields = array(); |
|
778 | |||
779 | // Resort fields to put optional fields at the end of the method signature. |
||
780 | $requiredFields = array(); |
||
781 | 1 | $optionalFields = array(); |
|
782 | 1 | ||
783 | 1 | foreach ($metadata->fieldMappings as $fieldMapping) { |
|
784 | if (empty($fieldMapping['nullable'])) { |
||
785 | $requiredFields[] = $fieldMapping; |
||
786 | 1 | ||
787 | continue; |
||
788 | } |
||
789 | 1 | ||
790 | $optionalFields[] = $fieldMapping; |
||
791 | } |
||
792 | 1 | ||
793 | $fieldMappings = array_merge($requiredFields, $optionalFields); |
||
794 | |||
795 | foreach ($metadata->embeddedClasses as $fieldName => $embeddedClass) { |
||
796 | $paramType = '\\' . ltrim($embeddedClass['class'], '\\'); |
||
797 | $paramVariable = '$' . $fieldName; |
||
798 | |||
799 | $paramTypes[] = $paramType; |
||
800 | $paramVariables[] = $paramVariable; |
||
801 | $params[] = $paramType . ' ' . $paramVariable; |
||
802 | 8 | $fields[] = '$this->' . $fieldName . ' = ' . $paramVariable . ';'; |
|
803 | } |
||
804 | 8 | ||
805 | 8 | foreach ($fieldMappings as $fieldMapping) { |
|
806 | 8 | if (isset($fieldMapping['declaredField']) && |
|
807 | isset($metadata->embeddedClasses[$fieldMapping['declaredField']]) |
||
808 | 8 | ) { |
|
809 | 8 | continue; |
|
810 | } |
||
811 | 8 | ||
812 | 8 | $paramTypes[] = $this->getType($fieldMapping['type']) . (!empty($fieldMapping['nullable']) ? '|null' : ''); |
|
813 | 8 | $param = '$' . $fieldMapping['fieldName']; |
|
814 | 8 | $paramVariables[] = $param; |
|
815 | |||
816 | if ($fieldMapping['type'] === 'datetime') { |
||
817 | 8 | $param = $this->getType($fieldMapping['type']) . ' ' . $param; |
|
818 | 8 | } |
|
819 | 8 | ||
820 | 8 | if (!empty($fieldMapping['nullable'])) { |
|
821 | 8 | $param .= ' = null'; |
|
822 | } |
||
823 | |||
824 | $params[] = $param; |
||
825 | 8 | ||
826 | 8 | $fields[] = '$this->' . $fieldMapping['fieldName'] . ' = $' . $fieldMapping['fieldName'] . ';'; |
|
827 | 8 | } |
|
828 | 8 | ||
829 | 8 | $maxParamTypeLength = max(array_map('strlen', $paramTypes)); |
|
830 | $paramTags = array_map( |
||
831 | function ($type, $variable) use ($maxParamTypeLength) { |
||
832 | 8 | return '@param ' . $type . str_repeat(' ', $maxParamTypeLength - strlen($type) + 1) . $variable; |
|
833 | 8 | }, |
|
834 | 8 | $paramTypes, |
|
835 | 8 | $paramVariables |
|
836 | 8 | ); |
|
837 | 8 | ||
838 | 3 | // Generate multi line constructor if the signature exceeds 120 characters. |
|
839 | 3 | if (array_sum(array_map('strlen', $params)) + count($params) * 2 + 29 > 120) { |
|
840 | $delimiter = "\n" . $this->spaces; |
||
841 | 3 | $params = $delimiter . implode(',' . $delimiter, $params) . "\n"; |
|
842 | } else { |
||
843 | 8 | $params = implode(', ', $params); |
|
844 | 4 | } |
|
845 | |||
846 | $replacements = array( |
||
847 | 8 | '<paramTags>' => implode("\n * ", $paramTags), |
|
848 | '<params>' => $params, |
||
849 | '<fields>' => implode("\n" . $this->spaces, $fields), |
||
850 | ); |
||
851 | |||
852 | $constructor = str_replace( |
||
853 | array_keys($replacements), |
||
854 | array_values($replacements), |
||
855 | 29 | static::$embeddableConstructorMethodTemplate |
|
856 | ); |
||
857 | 29 | ||
858 | return $this->prefixCodeWithSpaces($constructor); |
||
859 | 2 | } |
|
860 | 2 | ||
861 | 1 | /** |
|
862 | * @todo this won't work if there is a namespace in brackets and a class outside of it. |
||
863 | * |
||
864 | * @param string $src |
||
865 | * |
||
866 | 28 | * @return void |
|
867 | 2 | */ |
|
868 | 2 | protected function parseTokensInEntityFile($src) |
|
869 | { |
||
870 | $tokens = token_get_all($src); |
||
871 | $lastSeenNamespace = ""; |
||
872 | $lastSeenClass = false; |
||
873 | 28 | ||
874 | 28 | $inNamespace = false; |
|
875 | $inClass = false; |
||
876 | |||
877 | for ($i = 0; $i < count($tokens); $i++) { |
||
878 | $token = $tokens[$i]; |
||
879 | if (in_array($token[0], array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT))) { |
||
880 | continue; |
||
881 | } |
||
882 | |||
883 | if ($inNamespace) { |
||
884 | 30 | if ($token[0] == T_NS_SEPARATOR || $token[0] == T_STRING) { |
|
885 | $lastSeenNamespace .= $token[1]; |
||
886 | 30 | } elseif (is_string($token) && in_array($token, array(';', '{'))) { |
|
887 | $inNamespace = false; |
||
888 | 2 | } |
|
889 | } |
||
890 | 2 | ||
891 | 1 | if ($inClass) { |
|
892 | $inClass = false; |
||
893 | $lastSeenClass = $lastSeenNamespace . ($lastSeenNamespace ? '\\' : '') . $token[1]; |
||
894 | $this->staticReflection[$lastSeenClass]['properties'] = array(); |
||
895 | $this->staticReflection[$lastSeenClass]['methods'] = array(); |
||
896 | 30 | } |
|
897 | 2 | ||
898 | 2 | if ($token[0] == T_NAMESPACE) { |
|
899 | $lastSeenNamespace = ""; |
||
900 | $inNamespace = true; |
||
901 | } elseif ($token[0] == T_CLASS && $tokens[$i-1][0] != T_DOUBLE_COLON) { |
||
902 | $inClass = true; |
||
903 | 30 | } elseif ($token[0] == T_FUNCTION) { |
|
904 | 30 | if ($tokens[$i+2][0] == T_STRING) { |
|
905 | $this->staticReflection[$lastSeenClass]['methods'][] = strtolower($tokens[$i+2][1]); |
||
906 | } elseif ($tokens[$i+2] == "&" && $tokens[$i+3][0] == T_STRING) { |
||
907 | $this->staticReflection[$lastSeenClass]['methods'][] = strtolower($tokens[$i+3][1]); |
||
908 | } |
||
909 | } elseif (in_array($token[0], array(T_VAR, T_PUBLIC, T_PRIVATE, T_PROTECTED)) && $tokens[$i+2][0] != T_FUNCTION) { |
||
910 | $this->staticReflection[$lastSeenClass]['properties'][] = substr($tokens[$i+2][1], 1); |
||
911 | } |
||
912 | } |
||
913 | 30 | } |
|
914 | |||
915 | 30 | /** |
|
916 | 24 | * @param string $property |
|
917 | * @param ClassMetadataInfo $metadata |
||
918 | * |
||
919 | 7 | * @return bool |
|
920 | 1 | */ |
|
921 | 7 | protected function hasProperty($property, ClassMetadataInfo $metadata) |
|
922 | { |
||
923 | 7 | if ($this->extendsClass() || (!$this->isNew && class_exists($metadata->name))) { |
|
924 | // don't generate property if its already on the base class. |
||
925 | 7 | $reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name); |
|
926 | 7 | if ($reflClass->hasProperty($property)) { |
|
927 | return true; |
||
928 | 7 | } |
|
929 | } |
||
930 | |||
931 | 7 | // check traits for existing property |
|
932 | foreach ($this->getTraits($metadata) as $trait) { |
||
933 | if ($trait->hasProperty($property)) { |
||
934 | return true; |
||
935 | } |
||
936 | } |
||
937 | |||
938 | return ( |
||
939 | 29 | isset($this->staticReflection[$metadata->name]) && |
|
940 | in_array($property, $this->staticReflection[$metadata->name]['properties']) |
||
941 | 29 | ); |
|
942 | } |
||
943 | |||
944 | /** |
||
945 | * @param string $method |
||
946 | * @param ClassMetadataInfo $metadata |
||
947 | 30 | * |
|
948 | * @return bool |
||
949 | 30 | */ |
|
950 | protected function hasMethod($method, ClassMetadataInfo $metadata) |
||
951 | { |
||
952 | if ($this->extendsClass() || (!$this->isNew && class_exists($metadata->name))) { |
||
953 | // don't generate method if its already on the base class. |
||
954 | $reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name); |
||
955 | 2 | ||
956 | if ($reflClass->hasMethod($method)) { |
||
957 | 2 | return true; |
|
958 | } |
||
959 | } |
||
960 | |||
961 | // check traits for existing method |
||
962 | foreach ($this->getTraits($metadata) as $trait) { |
||
963 | 1 | if ($trait->hasMethod($method)) { |
|
964 | return true; |
||
965 | 1 | } |
|
966 | } |
||
967 | 1 | ||
968 | return ( |
||
969 | isset($this->staticReflection[$metadata->name]) && |
||
970 | in_array(strtolower($method), $this->staticReflection[$metadata->name]['methods']) |
||
971 | ); |
||
972 | } |
||
973 | |||
974 | /** |
||
975 | 30 | * @param ClassMetadataInfo $metadata |
|
976 | * |
||
977 | 30 | * @return array |
|
978 | 30 | */ |
|
979 | protected function getTraits(ClassMetadataInfo $metadata) |
||
980 | { |
||
981 | if (! ($metadata->reflClass !== null || class_exists($metadata->name))) { |
||
982 | return []; |
||
983 | } |
||
984 | |||
985 | $reflClass = $metadata->reflClass === null |
||
986 | 29 | ? new \ReflectionClass($metadata->name) |
|
987 | : $metadata->reflClass; |
||
988 | 29 | ||
989 | $traits = array(); |
||
990 | |||
991 | while ($reflClass !== false) { |
||
992 | $traits = array_merge($traits, $reflClass->getTraits()); |
||
993 | |||
994 | $reflClass = $reflClass->getParentClass(); |
||
995 | } |
||
996 | 29 | ||
997 | return $traits; |
||
998 | 29 | } |
|
999 | 29 | ||
1000 | 29 | /** |
|
1001 | * @param ClassMetadataInfo $metadata |
||
1002 | 29 | * |
|
1003 | 29 | * @return bool |
|
1004 | */ |
||
1005 | protected function hasNamespace(ClassMetadataInfo $metadata) |
||
1006 | 29 | { |
|
1007 | return strpos($metadata->name, '\\') ? true : false; |
||
1008 | } |
||
1009 | |||
1010 | /** |
||
1011 | * @return bool |
||
1012 | */ |
||
1013 | 29 | protected function extendsClass() |
|
1014 | 29 | { |
|
1015 | 29 | return $this->classToExtend ? true : false; |
|
1016 | } |
||
1017 | |||
1018 | /** |
||
1019 | 29 | * @return string |
|
1020 | 10 | */ |
|
1021 | protected function getClassToExtend() |
||
1022 | { |
||
1023 | return $this->classToExtend; |
||
1024 | 29 | } |
|
1025 | |||
1026 | 29 | /** |
|
1027 | * @return string |
||
1028 | */ |
||
1029 | protected function getClassToExtendName() |
||
1030 | { |
||
1031 | $refl = new \ReflectionClass($this->getClassToExtend()); |
||
1032 | |||
1033 | return '\\' . $refl->getName(); |
||
1034 | 29 | } |
|
1035 | |||
1036 | 29 | /** |
|
1037 | * @param ClassMetadataInfo $metadata |
||
1038 | 29 | * |
|
1039 | 9 | * @return string |
|
1040 | */ |
||
1041 | protected function getClassName(ClassMetadataInfo $metadata) |
||
1046 | 27 | ||
1047 | /** |
||
1048 | * @param ClassMetadataInfo $metadata |
||
1049 | * |
||
1050 | * @return string |
||
1051 | */ |
||
1052 | protected function getNamespace(ClassMetadataInfo $metadata) |
||
1053 | { |
||
1054 | 29 | return substr($metadata->name, 0, strrpos($metadata->name, '\\')); |
|
1055 | } |
||
1056 | 29 | ||
1057 | 9 | /** |
|
1058 | * @param ClassMetadataInfo $metadata |
||
1059 | * |
||
1060 | 27 | * @return string |
|
1061 | */ |
||
1062 | 27 | protected function generateEntityDocBlock(ClassMetadataInfo $metadata) |
|
1063 | { |
||
1064 | $lines = array(); |
||
1065 | $lines[] = '/**'; |
||
1066 | 27 | $lines[] = ' * ' . $this->getClassName($metadata); |
|
1067 | 24 | ||
1068 | if ($this->generateAnnotations) { |
||
1069 | $lines[] = ' *'; |
||
1070 | 27 | ||
1071 | 1 | $methods = array( |
|
1072 | 'generateTableAnnotation', |
||
1073 | 'generateInheritanceAnnotation', |
||
1074 | 27 | 'generateDiscriminatorColumnAnnotation', |
|
1075 | 9 | 'generateDiscriminatorMapAnnotation', |
|
1076 | 9 | 'generateEntityAnnotation', |
|
1077 | ); |
||
1078 | |||
1079 | 27 | foreach ($methods as $method) { |
|
1080 | 9 | if ($code = $this->$method($metadata)) { |
|
1094 | |||
1095 | 9 | /** |
|
1096 | 9 | * @param ClassMetadataInfo $metadata |
|
1097 | 9 | * |
|
1098 | 9 | * @return string |
|
1099 | 9 | */ |
|
1100 | protected function generateEntityAnnotation(ClassMetadataInfo $metadata) |
||
1114 | 29 | ||
1115 | /** |
||
1116 | * @param ClassMetadataInfo $metadata |
||
1117 | 29 | * |
|
1118 | * @return string |
||
1119 | */ |
||
1120 | protected function generateTableAnnotation($metadata) |
||
1152 | 29 | ||
1153 | /** |
||
1154 | * @param string $constraintName |
||
1155 | * @param array $constraints |
||
1156 | * |
||
1157 | * @return string |
||
1158 | */ |
||
1159 | 29 | protected function generateTableConstraints($constraintName, $constraints) |
|
1172 | 28 | ||
1173 | 28 | /** |
|
1174 | * @param ClassMetadataInfo $metadata |
||
1175 | 25 | * |
|
1176 | 25 | * @return string |
|
1177 | */ |
||
1178 | protected function generateInheritanceAnnotation($metadata) |
||
1184 | |||
1185 | 29 | /** |
|
1186 | 8 | * @param ClassMetadataInfo $metadata |
|
1187 | 1 | * |
|
1188 | * @return string |
||
1189 | */ |
||
1190 | 8 | protected function generateDiscriminatorColumnAnnotation($metadata) |
|
1201 | 29 | ||
1202 | 12 | /** |
|
1203 | 11 | * @param ClassMetadataInfo $metadata |
|
1204 | 11 | * |
|
1205 | 9 | * @return string |
|
1206 | */ |
||
1207 | 11 | protected function generateDiscriminatorMapAnnotation($metadata) |
|
1219 | |||
1220 | /** |
||
1221 | * @param ClassMetadataInfo $metadata |
||
1222 | * |
||
1223 | 29 | * @return string |
|
1224 | */ |
||
1225 | protected function generateEntityStubMethods(ClassMetadataInfo $metadata) |
||
1291 | 11 | ||
1292 | 11 | /** |
|
1293 | 11 | * @param array $associationMapping |
|
1294 | * |
||
1295 | * @return bool |
||
1296 | 30 | */ |
|
1297 | protected function isAssociationIsNullable($associationMapping) |
||
1318 | |||
1319 | 27 | /** |
|
1320 | 27 | * @param ClassMetadataInfo $metadata |
|
1321 | 27 | * |
|
1322 | * @return string |
||
1323 | */ |
||
1324 | 30 | protected function generateEntityLifecycleCallbackMethods(ClassMetadataInfo $metadata) |
|
1342 | 8 | ||
1343 | /** |
||
1344 | * @param ClassMetadataInfo $metadata |
||
1345 | 30 | * |
|
1346 | * @return string |
||
1347 | */ |
||
1348 | protected function generateEntityAssociationMappingProperties(ClassMetadataInfo $metadata) |
||
1364 | |||
1365 | /** |
||
1366 | 28 | * @param ClassMetadataInfo $metadata |
|
1367 | 5 | * |
|
1368 | * @return string |
||
1369 | 28 | */ |
|
1370 | protected function generateEntityFieldMappingProperties(ClassMetadataInfo $metadata) |
||
1392 | |||
1393 | /** |
||
1394 | 28 | * @param ClassMetadataInfo $metadata |
|
1395 | * |
||
1396 | * @return string |
||
1397 | */ |
||
1398 | protected function generateEntityEmbeddedProperties(ClassMetadataInfo $metadata) |
||
1413 | 2 | ||
1414 | /** |
||
1415 | 10 | * @param ClassMetadataInfo $metadata |
|
1416 | * @param string $type |
||
1417 | * @param string $fieldName |
||
1418 | 10 | * @param string|null $typeHint |
|
1419 | 10 | * @param string|null $defaultValue |
|
1420 | * |
||
1421 | * @return string |
||
1422 | 10 | */ |
|
1423 | protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) |
||
1479 | 11 | ||
1480 | /** |
||
1481 | 10 | * @param string $name |
|
1482 | * @param string $methodName |
||
1483 | * @param ClassMetadataInfo $metadata |
||
1484 | 11 | * |
|
1485 | 11 | * @return string |
|
1486 | */ |
||
1487 | 11 | protected function generateLifecycleCallbackMethod($name, $methodName, $metadata) |
|
1507 | 11 | ||
1508 | 11 | /** |
|
1509 | * @param array $joinColumn |
||
1510 | 11 | * |
|
1511 | * @return string |
||
1512 | 11 | */ |
|
1513 | 11 | protected function generateJoinColumnAnnotation(array $joinColumn) |
|
1543 | |||
1544 | 11 | /** |
|
1545 | * @param array $associationMapping |
||
1546 | 10 | * @param ClassMetadataInfo $metadata |
|
1547 | 10 | * |
|
1548 | * @return string |
||
1549 | */ |
||
1550 | 10 | protected function generateAssociationMappingPropertyDocBlock(array $associationMapping, ClassMetadataInfo $metadata) |
|
1695 | |||
1696 | /** |
||
1697 | 1 | * @param array $fieldMapping |
|
1698 | 1 | * @param ClassMetadataInfo $metadata |
|
1699 | * |
||
1700 | * @return string |
||
1701 | 1 | */ |
|
1702 | protected function generateFieldMappingPropertyDocBlock(array $fieldMapping, ClassMetadataInfo $metadata) |
||
1791 | |||
1792 | 1 | /** |
|
1793 | * @param array $embeddedClass |
||
1794 | * |
||
1795 | * @return string |
||
1796 | */ |
||
1797 | protected function generateEmbeddedPropertyDocBlock(array $embeddedClass) |
||
1820 | 1 | ||
1821 | /** |
||
1822 | 1 | * @param string $code |
|
1823 | 1 | * @param int $num |
|
1824 | 1 | * |
|
1825 | * @return string |
||
1826 | 1 | */ |
|
1827 | protected function prefixCodeWithSpaces($code, $num = 1) |
||
1839 | |||
1840 | /** |
||
1841 | * @param integer $type The inheritance type used by the class and its subclasses. |
||
1842 | * |
||
1843 | * @return string The literal string for the inheritance type. |
||
1844 | * |
||
1845 | * @throws \InvalidArgumentException When the inheritance type does not exist. |
||
1846 | */ |
||
1847 | protected function getInheritanceTypeString($type) |
||
1855 | |||
1856 | /** |
||
1857 | * @param integer $type The policy used for change-tracking for the mapped class. |
||
1858 | * |
||
1859 | * @return string The literal string for the change-tracking type. |
||
1860 | * |
||
1861 | * @throws \InvalidArgumentException When the change-tracking type does not exist. |
||
1862 | */ |
||
1863 | protected function getChangeTrackingPolicyString($type) |
||
1871 | |||
1872 | /** |
||
1873 | * @param integer $type The generator to use for the mapped class. |
||
1874 | * |
||
1875 | * @return string The literal string for the generator type. |
||
1876 | * |
||
1877 | * @throws \InvalidArgumentException When the generator type does not exist. |
||
1878 | */ |
||
1879 | protected function getIdGeneratorTypeString($type) |
||
1887 | |||
1888 | /** |
||
1889 | * Exports (nested) option elements. |
||
1890 | * |
||
1891 | * @param array $options |
||
1892 | * |
||
1893 | * @return string |
||
1894 | */ |
||
1895 | private function exportTableOptions(array $options) |
||
1909 | |||
1910 | /** |
||
1911 | * @param ClassMetadataInfo $metadata |
||
1912 | * @param string $type |
||
1913 | * @param string $fieldName |
||
1914 | * @param string $variableType |
||
1915 | * @return string |
||
1916 | */ |
||
1917 | private function getMethodReturnType(ClassMetadataInfo $metadata, $type, $fieldName, $variableType) |
||
1941 | } |
||
1942 |
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: