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 | * @<name> |
||
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 | 38 | public function __construct() |
|
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) |
||
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 | 29 | public function writeEntityClass(ClassMetadataInfo $metadata, $outputDirectory) |
|
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 | 29 | 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) |
|
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) |
||
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 | 38 | 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 | 37 | public function setFieldVisibility($visibility) |
|
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 | 38 | 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 | 38 | 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 | 38 | 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 | 38 | 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 | 29 | protected function getType($type) |
|
598 | |||
599 | /** |
||
600 | * @param ClassMetadataInfo $metadata |
||
601 | * |
||
602 | * @return string |
||
603 | */ |
||
604 | 29 | protected function generateEntityNamespace(ClassMetadataInfo $metadata) |
|
610 | |||
611 | 29 | protected function generateEntityUse() |
|
619 | |||
620 | /** |
||
621 | * @param ClassMetadataInfo $metadata |
||
622 | * |
||
623 | * @return string |
||
624 | */ |
||
625 | 29 | protected function generateEntityClassName(ClassMetadataInfo $metadata) |
|
630 | |||
631 | /** |
||
632 | * @param ClassMetadataInfo $metadata |
||
633 | * |
||
634 | * @return string |
||
635 | */ |
||
636 | 30 | protected function generateEntityBody(ClassMetadataInfo $metadata) |
|
670 | |||
671 | /** |
||
672 | * @param ClassMetadataInfo $metadata |
||
673 | * |
||
674 | * @return string |
||
675 | */ |
||
676 | 30 | protected function generateEntityConstructor(ClassMetadataInfo $metadata) |
|
700 | |||
701 | /** |
||
702 | * @param ClassMetadataInfo $metadata |
||
703 | * |
||
704 | * @return string |
||
705 | */ |
||
706 | 1 | private function generateEmbeddableConstructor(ClassMetadataInfo $metadata) |
|
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) |
|
848 | |||
849 | /** |
||
850 | * @param string $property |
||
851 | * @param ClassMetadataInfo $metadata |
||
852 | * |
||
853 | * @return bool |
||
854 | */ |
||
855 | 29 | protected function hasProperty($property, ClassMetadataInfo $metadata) |
|
877 | |||
878 | /** |
||
879 | * @param string $method |
||
880 | * @param ClassMetadataInfo $metadata |
||
881 | * |
||
882 | * @return bool |
||
883 | */ |
||
884 | 30 | protected function hasMethod($method, ClassMetadataInfo $metadata) |
|
907 | |||
908 | /** |
||
909 | * @param ClassMetadataInfo $metadata |
||
910 | * |
||
911 | * @return array |
||
912 | */ |
||
913 | 30 | protected function getTraits(ClassMetadataInfo $metadata) |
|
933 | |||
934 | /** |
||
935 | * @param ClassMetadataInfo $metadata |
||
936 | * |
||
937 | * @return bool |
||
938 | */ |
||
939 | 29 | protected function hasNamespace(ClassMetadataInfo $metadata) |
|
943 | |||
944 | /** |
||
945 | * @return bool |
||
946 | */ |
||
947 | 30 | 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() |
|
969 | |||
970 | /** |
||
971 | * @param ClassMetadataInfo $metadata |
||
972 | * |
||
973 | * @return string |
||
974 | */ |
||
975 | 30 | protected function getClassName(ClassMetadataInfo $metadata) |
|
980 | |||
981 | /** |
||
982 | * @param ClassMetadataInfo $metadata |
||
983 | * |
||
984 | * @return string |
||
985 | */ |
||
986 | 29 | protected function getNamespace(ClassMetadataInfo $metadata) |
|
990 | |||
991 | /** |
||
992 | * @param ClassMetadataInfo $metadata |
||
993 | * |
||
994 | * @return string |
||
995 | */ |
||
996 | 29 | protected function generateEntityDocBlock(ClassMetadataInfo $metadata) |
|
1028 | |||
1029 | /** |
||
1030 | * @param ClassMetadataInfo $metadata |
||
1031 | * |
||
1032 | * @return string |
||
1033 | */ |
||
1034 | 29 | protected function generateEntityAnnotation(ClassMetadataInfo $metadata) |
|
1048 | |||
1049 | /** |
||
1050 | * @param ClassMetadataInfo $metadata |
||
1051 | * |
||
1052 | * @return string |
||
1053 | */ |
||
1054 | 29 | protected function generateTableAnnotation($metadata) |
|
1086 | |||
1087 | /** |
||
1088 | * @param string $constraintName |
||
1089 | * @param array $constraints |
||
1090 | * |
||
1091 | * @return string |
||
1092 | */ |
||
1093 | 9 | protected function generateTableConstraints($constraintName, $constraints) |
|
1094 | { |
||
1095 | 9 | $annotations = array(); |
|
1096 | 9 | foreach ($constraints as $name => $constraint) { |
|
1097 | 9 | $columns = array(); |
|
1098 | 9 | foreach ($constraint['columns'] as $column) { |
|
1099 | 9 | $columns[] = '"' . $column . '"'; |
|
1100 | } |
||
1101 | 9 | $annotations[] = '@' . $this->annotationsPrefix . $constraintName . '(name="' . $name . '", columns={' . implode(', ', $columns) . '})'; |
|
1102 | } |
||
1103 | |||
1104 | 9 | return implode(', ', $annotations); |
|
1105 | } |
||
1106 | |||
1107 | /** |
||
1108 | * @param ClassMetadataInfo $metadata |
||
1109 | * |
||
1110 | * @return string |
||
1111 | */ |
||
1112 | 29 | protected function generateInheritanceAnnotation($metadata) |
|
1118 | |||
1119 | /** |
||
1120 | * @param ClassMetadataInfo $metadata |
||
1121 | * |
||
1122 | * @return string |
||
1123 | */ |
||
1124 | 29 | protected function generateDiscriminatorColumnAnnotation($metadata) |
|
1135 | |||
1136 | /** |
||
1137 | * @param ClassMetadataInfo $metadata |
||
1138 | * |
||
1139 | * @return string |
||
1140 | */ |
||
1141 | 29 | protected function generateDiscriminatorMapAnnotation($metadata) |
|
1153 | |||
1154 | /** |
||
1155 | * @param ClassMetadataInfo $metadata |
||
1156 | * |
||
1157 | * @return string |
||
1158 | */ |
||
1159 | 29 | protected function generateEntityStubMethods(ClassMetadataInfo $metadata) |
|
1160 | { |
||
1161 | 29 | $methods = array(); |
|
1162 | |||
1163 | 29 | foreach ($metadata->fieldMappings as $fieldMapping) { |
|
1164 | 28 | if (isset($fieldMapping['declaredField']) && |
|
1165 | 28 | isset($metadata->embeddedClasses[$fieldMapping['declaredField']]) |
|
1166 | ) { |
||
1167 | continue; |
||
1168 | } |
||
1169 | |||
1170 | 28 | $nullableField = $this->getNullableField($fieldMapping); |
|
1171 | 28 | if (( ! isset($fieldMapping['id']) || |
|
1172 | 26 | ! $fieldMapping['id'] || |
|
1173 | 28 | $metadata->generatorType == ClassMetadataInfo::GENERATOR_TYPE_NONE |
|
1174 | 28 | ) && (! $metadata->isEmbeddedClass || ! $this->embeddablesImmutable) |
|
1175 | ) { |
||
1176 | 25 | if ($code = $this->generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type'], $nullableField)) { |
|
1177 | 25 | $methods[] = $code; |
|
1178 | } |
||
1179 | } |
||
1180 | |||
1181 | 28 | if ($code = $this->generateEntityStubMethod($metadata, 'get', $fieldMapping['fieldName'], $fieldMapping['type'], $nullableField)) { |
|
1182 | 28 | $methods[] = $code; |
|
1183 | } |
||
1184 | } |
||
1185 | |||
1186 | 29 | foreach ($metadata->embeddedClasses as $fieldName => $embeddedClass) { |
|
1187 | 8 | if (isset($embeddedClass['declaredField'])) { |
|
1188 | 1 | continue; |
|
1189 | } |
||
1190 | |||
1191 | 8 | if ( ! $metadata->isEmbeddedClass || ! $this->embeddablesImmutable) { |
|
1192 | 7 | if ($code = $this->generateEntityStubMethod($metadata, 'set', $fieldName, $embeddedClass['class'])) { |
|
1193 | 7 | $methods[] = $code; |
|
1194 | } |
||
1195 | } |
||
1196 | |||
1197 | 8 | if ($code = $this->generateEntityStubMethod($metadata, 'get', $fieldName, $embeddedClass['class'])) { |
|
1198 | 8 | $methods[] = $code; |
|
1199 | } |
||
1200 | } |
||
1201 | |||
1202 | 29 | foreach ($metadata->associationMappings as $associationMapping) { |
|
1203 | 12 | if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) { |
|
1204 | 11 | $nullable = $this->isAssociationIsNullable($associationMapping) ? 'null' : null; |
|
1205 | 11 | if ($code = $this->generateEntityStubMethod($metadata, 'set', $associationMapping['fieldName'], $associationMapping['targetEntity'], $nullable)) { |
|
1206 | 9 | $methods[] = $code; |
|
1207 | } |
||
1208 | 11 | if ($code = $this->generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], $associationMapping['targetEntity'], $nullable)) { |
|
1209 | 11 | $methods[] = $code; |
|
1210 | } |
||
1211 | 10 | } elseif ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) { |
|
1212 | 10 | if ($code = $this->generateEntityStubMethod($metadata, 'add', $associationMapping['fieldName'], $associationMapping['targetEntity'])) { |
|
1213 | 10 | $methods[] = $code; |
|
1214 | } |
||
1215 | 10 | if ($code = $this->generateEntityStubMethod($metadata, 'remove', $associationMapping['fieldName'], $associationMapping['targetEntity'])) { |
|
1216 | 10 | $methods[] = $code; |
|
1217 | } |
||
1218 | 10 | if ($code = $this->generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], 'Doctrine\Common\Collections\Collection')) { |
|
1219 | 12 | $methods[] = $code; |
|
1220 | } |
||
1221 | } |
||
1222 | } |
||
1223 | |||
1224 | 29 | return implode("\n\n", $methods); |
|
1225 | } |
||
1226 | |||
1227 | /** |
||
1228 | * @param array $associationMapping |
||
1229 | * |
||
1230 | * @return bool |
||
1231 | */ |
||
1232 | 11 | protected function isAssociationIsNullable($associationMapping) |
|
1253 | |||
1254 | /** |
||
1255 | * @param ClassMetadataInfo $metadata |
||
1256 | * |
||
1257 | * @return string |
||
1258 | */ |
||
1259 | 30 | protected function generateEntityLifecycleCallbackMethods(ClassMetadataInfo $metadata) |
|
1277 | |||
1278 | /** |
||
1279 | * @param ClassMetadataInfo $metadata |
||
1280 | * |
||
1281 | * @return string |
||
1282 | */ |
||
1283 | 30 | protected function generateEntityAssociationMappingProperties(ClassMetadataInfo $metadata) |
|
1299 | |||
1300 | /** |
||
1301 | * @param ClassMetadataInfo $metadata |
||
1302 | * |
||
1303 | * @return string |
||
1304 | */ |
||
1305 | 30 | protected function generateEntityFieldMappingProperties(ClassMetadataInfo $metadata) |
|
1327 | |||
1328 | /** |
||
1329 | * @param ClassMetadataInfo $metadata |
||
1330 | * |
||
1331 | * @return string |
||
1332 | */ |
||
1333 | 30 | protected function generateEntityEmbeddedProperties(ClassMetadataInfo $metadata) |
|
1348 | |||
1349 | /** |
||
1350 | * @param ClassMetadataInfo $metadata |
||
1351 | * @param string $type |
||
1352 | * @param string $fieldName |
||
1353 | * @param string|null $typeHint |
||
1354 | * @param string|null $defaultValue |
||
1355 | * |
||
1356 | * @return string |
||
1357 | */ |
||
1358 | 28 | protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) |
|
1359 | { |
||
1360 | 28 | $methodName = $type . Inflector::classify($fieldName); |
|
1361 | 28 | $variableName = Inflector::camelize($fieldName); |
|
1362 | 28 | if (in_array($type, array("add", "remove"))) { |
|
1363 | 10 | $methodName = Inflector::singularize($methodName); |
|
1364 | 10 | $variableName = Inflector::singularize($variableName); |
|
1365 | } |
||
1366 | |||
1367 | 28 | if ($this->hasMethod($methodName, $metadata)) { |
|
1368 | 5 | return ''; |
|
1369 | } |
||
1370 | 28 | $this->staticReflection[$metadata->name]['methods'][] = strtolower($methodName); |
|
1371 | |||
1372 | 28 | $var = sprintf('%sMethodTemplate', $type); |
|
1373 | 28 | $template = static::$$var; |
|
1374 | |||
1375 | 28 | $methodTypeHint = null; |
|
1376 | 28 | $types = Type::getTypesMap(); |
|
1377 | 28 | $variableType = $typeHint ? $this->getType($typeHint) : null; |
|
1378 | |||
1379 | 28 | if ($typeHint && ! isset($types[$typeHint])) { |
|
1380 | 12 | $variableType = '\\' . ltrim($variableType, '\\'); |
|
1381 | 12 | $methodTypeHint = '\\' . $typeHint . ' '; |
|
1382 | } |
||
1383 | |||
1384 | $replacements = array( |
||
1385 | 28 | '<description>' => ucfirst($type) . ' ' . $variableName, |
|
1386 | 28 | '<methodTypeHint>' => $methodTypeHint, |
|
1387 | 28 | '<variableType>' => $variableType.(null !== $defaultValue ? ('|'.$defaultValue) : ''), |
|
1388 | 28 | '<variableName>' => $variableName, |
|
1389 | 28 | '<methodName>' => $methodName, |
|
1390 | 28 | '<fieldName>' => $fieldName, |
|
1391 | 28 | '<variableDefault>' => ($defaultValue !== null ) ? (' = '.$defaultValue) : '', |
|
1392 | 28 | '<entity>' => $this->getClassName($metadata) |
|
1393 | ); |
||
1394 | |||
1395 | 28 | $method = str_replace( |
|
1396 | array_keys($replacements), |
||
1397 | array_values($replacements), |
||
1398 | $template |
||
1399 | ); |
||
1400 | |||
1401 | 28 | return $this->prefixCodeWithSpaces($method); |
|
1402 | } |
||
1403 | |||
1404 | /** |
||
1405 | * @param string $name |
||
1406 | * @param string $methodName |
||
1407 | * @param ClassMetadataInfo $metadata |
||
1408 | * |
||
1409 | * @return string |
||
1410 | */ |
||
1411 | 10 | protected function generateLifecycleCallbackMethod($name, $methodName, $metadata) |
|
1431 | |||
1432 | /** |
||
1433 | * @param array $joinColumn |
||
1434 | * |
||
1435 | * @return string |
||
1436 | */ |
||
1437 | 11 | protected function generateJoinColumnAnnotation(array $joinColumn) |
|
1467 | |||
1468 | /** |
||
1469 | * @param array $associationMapping |
||
1470 | * @param ClassMetadataInfo $metadata |
||
1471 | * |
||
1472 | * @return string |
||
1473 | */ |
||
1474 | 11 | protected function generateAssociationMappingPropertyDocBlock(array $associationMapping, ClassMetadataInfo $metadata) |
|
1619 | |||
1620 | /** |
||
1621 | * @param array $fieldMapping |
||
1622 | * @param ClassMetadataInfo $metadata |
||
1623 | * |
||
1624 | * @return string |
||
1625 | */ |
||
1626 | 27 | protected function generateFieldMappingPropertyDocBlock(array $fieldMapping, ClassMetadataInfo $metadata) |
|
1627 | { |
||
1628 | 27 | $lines = array(); |
|
1629 | 27 | $lines[] = $this->spaces . '/**'; |
|
1630 | 27 | $lines[] = $this->spaces . ' * @var ' . $this->getType($fieldMapping['type']) . ($this->getNullableField($fieldMapping) ? '|' . $this->getNullableField($fieldMapping) : '');; |
|
1631 | |||
1632 | 27 | if ($this->generateAnnotations) { |
|
1633 | 27 | $lines[] = $this->spaces . ' *'; |
|
1634 | |||
1635 | 27 | $column = array(); |
|
1636 | 27 | if (isset($fieldMapping['columnName'])) { |
|
1637 | 27 | $column[] = 'name="' . $fieldMapping['columnName'] . '"'; |
|
1638 | } |
||
1639 | |||
1640 | 27 | if (isset($fieldMapping['type'])) { |
|
1641 | 27 | $column[] = 'type="' . $fieldMapping['type'] . '"'; |
|
1642 | } |
||
1643 | |||
1644 | 27 | if (isset($fieldMapping['length'])) { |
|
1645 | 4 | $column[] = 'length=' . $fieldMapping['length']; |
|
1646 | } |
||
1647 | |||
1648 | 27 | if (isset($fieldMapping['precision'])) { |
|
1649 | 4 | $column[] = 'precision=' . $fieldMapping['precision']; |
|
1650 | } |
||
1651 | |||
1652 | 27 | if (isset($fieldMapping['scale'])) { |
|
1653 | 4 | $column[] = 'scale=' . $fieldMapping['scale']; |
|
1654 | } |
||
1655 | |||
1656 | 27 | if (isset($fieldMapping['nullable'])) { |
|
1657 | 8 | $column[] = 'nullable=' . var_export($fieldMapping['nullable'], true); |
|
1658 | } |
||
1659 | |||
1660 | 27 | $options = []; |
|
1661 | |||
1662 | 27 | if (isset($fieldMapping['options']['unsigned']) && $fieldMapping['options']['unsigned']) { |
|
1663 | 1 | $options[] = '"unsigned"=true'; |
|
1664 | } |
||
1665 | |||
1666 | 27 | if ($options) { |
|
1667 | 1 | $column[] = 'options={'.implode(',', $options).'}'; |
|
1668 | } |
||
1669 | |||
1670 | 27 | if (isset($fieldMapping['columnDefinition'])) { |
|
1671 | 1 | $column[] = 'columnDefinition="' . $fieldMapping['columnDefinition'] . '"'; |
|
1672 | } |
||
1673 | |||
1674 | 27 | if (isset($fieldMapping['unique'])) { |
|
1675 | 4 | $column[] = 'unique=' . var_export($fieldMapping['unique'], true); |
|
1676 | } |
||
1677 | |||
1678 | 27 | $lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'Column(' . implode(', ', $column) . ')'; |
|
1679 | |||
1680 | 27 | if (isset($fieldMapping['id']) && $fieldMapping['id']) { |
|
1681 | 25 | $lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'Id'; |
|
1682 | |||
1683 | 25 | if ($generatorType = $this->getIdGeneratorTypeString($metadata->generatorType)) { |
|
1684 | 25 | $lines[] = $this->spaces.' * @' . $this->annotationsPrefix . 'GeneratedValue(strategy="' . $generatorType . '")'; |
|
1685 | } |
||
1686 | |||
1687 | 25 | if ($metadata->sequenceGeneratorDefinition) { |
|
1688 | 1 | $sequenceGenerator = array(); |
|
1689 | |||
1690 | 1 | if (isset($metadata->sequenceGeneratorDefinition['sequenceName'])) { |
|
1691 | 1 | $sequenceGenerator[] = 'sequenceName="' . $metadata->sequenceGeneratorDefinition['sequenceName'] . '"'; |
|
1692 | } |
||
1693 | |||
1694 | 1 | if (isset($metadata->sequenceGeneratorDefinition['allocationSize'])) { |
|
1695 | 1 | $sequenceGenerator[] = 'allocationSize=' . $metadata->sequenceGeneratorDefinition['allocationSize']; |
|
1696 | } |
||
1697 | |||
1698 | 1 | if (isset($metadata->sequenceGeneratorDefinition['initialValue'])) { |
|
1699 | 1 | $sequenceGenerator[] = 'initialValue=' . $metadata->sequenceGeneratorDefinition['initialValue']; |
|
1700 | } |
||
1701 | |||
1702 | 1 | $lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'SequenceGenerator(' . implode(', ', $sequenceGenerator) . ')'; |
|
1703 | } |
||
1704 | } |
||
1705 | |||
1706 | 27 | if (isset($fieldMapping['version']) && $fieldMapping['version']) { |
|
1707 | $lines[] = $this->spaces . ' * @' . $this->annotationsPrefix . 'Version'; |
||
1708 | } |
||
1709 | } |
||
1710 | |||
1711 | 27 | $lines[] = $this->spaces . ' */'; |
|
1712 | |||
1713 | 27 | return implode("\n", $lines); |
|
1714 | } |
||
1715 | |||
1716 | /** |
||
1717 | * @param array $embeddedClass |
||
1718 | * |
||
1719 | * @return string |
||
1720 | */ |
||
1721 | 8 | protected function generateEmbeddedPropertyDocBlock(array $embeddedClass) |
|
1744 | |||
1745 | /** |
||
1746 | * @param string $code |
||
1747 | * @param int $num |
||
1748 | * |
||
1749 | * @return string |
||
1750 | */ |
||
1751 | 29 | protected function prefixCodeWithSpaces($code, $num = 1) |
|
1763 | |||
1764 | /** |
||
1765 | * @param integer $type The inheritance type used by the class and its subclasses. |
||
1766 | * |
||
1767 | * @return string The literal string for the inheritance type. |
||
1768 | * |
||
1769 | * @throws \InvalidArgumentException When the inheritance type does not exist. |
||
1770 | */ |
||
1771 | 1 | protected function getInheritanceTypeString($type) |
|
1779 | |||
1780 | /** |
||
1781 | * @param integer $type The policy used for change-tracking for the mapped class. |
||
1782 | * |
||
1783 | * @return string The literal string for the change-tracking type. |
||
1784 | * |
||
1785 | * @throws \InvalidArgumentException When the change-tracking type does not exist. |
||
1786 | */ |
||
1787 | 1 | protected function getChangeTrackingPolicyString($type) |
|
1795 | |||
1796 | /** |
||
1797 | * @param integer $type The generator to use for the mapped class. |
||
1798 | * |
||
1799 | * @return string The literal string for the generator type. |
||
1800 | * |
||
1801 | * @throws \InvalidArgumentException When the generator type does not exist. |
||
1802 | */ |
||
1803 | 26 | protected function getIdGeneratorTypeString($type) |
|
1811 | |||
1812 | /** |
||
1813 | * @param array $fieldMapping |
||
1814 | * |
||
1815 | * @return string|null |
||
1816 | */ |
||
1817 | 29 | protected function getNullableField(array $fieldMapping) |
|
1823 | |||
1824 | /** |
||
1825 | * Exports (nested) option elements. |
||
1826 | * |
||
1827 | * @param array $options |
||
1828 | * |
||
1829 | * @return string |
||
1830 | */ |
||
1831 | 1 | private function exportTableOptions(array $options) |
|
1845 | } |
||
1846 |
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: