Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DocumentGenerator 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 DocumentGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
41 | class DocumentGenerator |
||
42 | { |
||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $backupExisting = true; |
||
47 | |||
48 | /** The extension to use for written php files */ |
||
49 | private $extension = '.php'; |
||
50 | |||
51 | /** Whether or not the current ClassMetadataInfo instance is new or old */ |
||
52 | private $isNew = true; |
||
53 | |||
54 | private $staticReflection = array(); |
||
55 | |||
56 | /** Number of spaces to use for indention in generated code */ |
||
57 | private $numSpaces = 4; |
||
58 | |||
59 | /** The actual spaces to use for indention */ |
||
60 | private $spaces = ' '; |
||
61 | |||
62 | /** The class all generated documents should extend */ |
||
63 | private $classToExtend; |
||
64 | |||
65 | /** Whether or not to generate annotations */ |
||
66 | private $generateAnnotations = false; |
||
67 | |||
68 | /** Whether or not to generate stub methods */ |
||
69 | private $generateDocumentStubMethods = false; |
||
70 | |||
71 | /** Whether or not to update the document class if it exists already */ |
||
72 | private $updateDocumentIfExists = false; |
||
73 | |||
74 | /** Whether or not to re-generate document class if it exists already */ |
||
75 | private $regenerateDocumentIfExists = false; |
||
76 | |||
77 | private static $classTemplate = |
||
78 | '<?php |
||
79 | |||
80 | <namespace> |
||
81 | |||
82 | <imports> |
||
83 | |||
84 | <documentAnnotation> |
||
85 | <documentClassName> |
||
86 | { |
||
87 | <documentBody> |
||
88 | } |
||
89 | '; |
||
90 | |||
91 | private static $getMethodTemplate = |
||
92 | '/** |
||
93 | * <description> |
||
94 | * |
||
95 | * @return <variableType>$<variableName> |
||
96 | */ |
||
97 | public function <methodName>() |
||
98 | { |
||
99 | <spaces>return $this-><fieldName>; |
||
100 | }'; |
||
101 | |||
102 | private static $setMethodTemplate = |
||
103 | '/** |
||
104 | * <description> |
||
105 | * |
||
106 | * @param <variableType>$<variableName> |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function <methodName>(<methodTypeHint>$<variableName><variableDefault>) |
||
110 | { |
||
111 | <spaces>$this-><fieldName> = $<variableName>; |
||
112 | <spaces>return $this; |
||
113 | }'; |
||
114 | |||
115 | private static $addMethodTemplate = |
||
116 | '/** |
||
117 | * <description> |
||
118 | * |
||
119 | * @param <variableType>$<variableName> |
||
120 | */ |
||
121 | public function <methodName>(<methodTypeHint>$<variableName>) |
||
122 | { |
||
123 | <spaces>$this-><fieldName>[] = $<variableName>; |
||
124 | }'; |
||
125 | |||
126 | private static $removeMethodTemplate = |
||
127 | '/** |
||
128 | * <description> |
||
129 | * |
||
130 | * @param <variableType>$<variableName> |
||
131 | */ |
||
132 | public function <methodName>(<methodTypeHint>$<variableName>) |
||
133 | { |
||
134 | <spaces>$this-><fieldName>->removeElement($<variableName>); |
||
135 | }'; |
||
136 | |||
137 | private static $lifecycleCallbackMethodTemplate = |
||
138 | '<comment> |
||
139 | public function <methodName>() |
||
140 | { |
||
141 | <spaces>// Add your code here |
||
142 | }'; |
||
143 | |||
144 | private static $constructorMethodTemplate = |
||
145 | 'public function __construct() |
||
146 | { |
||
147 | <collections> |
||
148 | } |
||
149 | '; |
||
150 | |||
151 | /** |
||
152 | * Generate and write document classes for the given array of ClassMetadataInfo instances |
||
153 | * |
||
154 | * @param array $metadatas |
||
155 | * @param string $outputDirectory |
||
156 | * @return void |
||
157 | */ |
||
158 | public function generate(array $metadatas, $outputDirectory) |
||
159 | { |
||
160 | foreach ($metadatas as $metadata) { |
||
161 | $this->writeDocumentClass($metadata, $outputDirectory); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Generated and write document class to disk for the given ClassMetadataInfo instance |
||
167 | * |
||
168 | * @param ClassMetadataInfo $metadata |
||
169 | * @param string $outputDirectory |
||
170 | * @throws \RuntimeException |
||
171 | * @return void |
||
172 | */ |
||
173 | 9 | public function writeDocumentClass(ClassMetadataInfo $metadata, $outputDirectory) |
|
174 | { |
||
175 | 9 | $path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->extension; |
|
176 | 9 | $dir = dirname($path); |
|
177 | |||
178 | 9 | if ( ! is_dir($dir)) { |
|
179 | mkdir($dir, 0775, true); |
||
180 | } |
||
181 | |||
182 | 9 | $this->isNew = ! file_exists($path) || (file_exists($path) && $this->regenerateDocumentIfExists); |
|
183 | |||
184 | 9 | if ( ! $this->isNew) { |
|
185 | 2 | $this->parseTokensInDocumentFile($path); |
|
186 | } |
||
187 | |||
188 | 9 | if ($this->backupExisting && file_exists($path)) { |
|
189 | 2 | $backupPath = dirname($path) . DIRECTORY_SEPARATOR . basename($path) . '~'; |
|
190 | 2 | if ( ! copy($path, $backupPath)) { |
|
191 | throw new \RuntimeException('Attempt to backup overwritten document file but copy operation failed.'); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | // If document doesn't exist or we're re-generating the documents entirely |
||
196 | 9 | if ($this->isNew) { |
|
197 | 8 | file_put_contents($path, $this->generateDocumentClass($metadata)); |
|
198 | |||
199 | // If document exists and we're allowed to update the document class |
||
200 | 2 | } elseif ( ! $this->isNew && $this->updateDocumentIfExists) { |
|
201 | 2 | file_put_contents($path, $this->generateUpdatedDocumentClass($metadata, $path)); |
|
202 | } |
||
203 | 9 | chmod($path, 0664); |
|
204 | 9 | } |
|
205 | |||
206 | /** |
||
207 | * Generate a PHP5 Doctrine 2 document class from the given ClassMetadataInfo instance |
||
208 | * |
||
209 | * @param ClassMetadataInfo $metadata |
||
210 | * @return string $code |
||
211 | */ |
||
212 | 8 | public function generateDocumentClass(ClassMetadataInfo $metadata) |
|
213 | { |
||
214 | $placeHolders = array( |
||
215 | 8 | '<namespace>', |
|
216 | '<imports>', |
||
217 | '<documentAnnotation>', |
||
218 | '<documentClassName>', |
||
219 | '<documentBody>' |
||
220 | ); |
||
221 | |||
222 | $replacements = array( |
||
223 | 8 | $this->generateDocumentNamespace($metadata), |
|
224 | 8 | $this->generateDocumentImports(), |
|
225 | 8 | $this->generateDocumentDocBlock($metadata), |
|
226 | 8 | $this->generateDocumentClassName($metadata), |
|
227 | 8 | $this->generateDocumentBody($metadata) |
|
228 | ); |
||
229 | |||
230 | 8 | $code = str_replace($placeHolders, $replacements, self::$classTemplate); |
|
231 | 8 | return str_replace('<spaces>', $this->spaces, $code); |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * Generate the updated code for the given ClassMetadataInfo and document at path |
||
236 | * |
||
237 | * @param ClassMetadataInfo $metadata |
||
238 | * @param string $path |
||
239 | * @return string $code; |
||
240 | */ |
||
241 | 2 | public function generateUpdatedDocumentClass(ClassMetadataInfo $metadata, $path) |
|
242 | { |
||
243 | 2 | $currentCode = file_get_contents($path); |
|
244 | |||
245 | 2 | $body = $this->generateDocumentBody($metadata); |
|
246 | 2 | $body = str_replace('<spaces>', $this->spaces, $body); |
|
247 | 2 | $last = strrpos($currentCode, '}'); |
|
248 | |||
249 | 2 | return substr($currentCode, 0, $last) . $body . (strlen($body) > 0 ? "\n" : '') . "}\n"; |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * Set the number of spaces the exported class should have |
||
254 | * |
||
255 | * @param integer $numSpaces |
||
256 | * @return void |
||
257 | */ |
||
258 | public function setNumSpaces($numSpaces) |
||
259 | { |
||
260 | $this->spaces = str_repeat(' ', $numSpaces); |
||
261 | $this->numSpaces = $numSpaces; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Set the extension to use when writing php files to disk |
||
266 | * |
||
267 | * @param string $extension |
||
268 | * @return void |
||
269 | */ |
||
270 | public function setExtension($extension) |
||
271 | { |
||
272 | $this->extension = $extension; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Set the name of the class the generated classes should extend from |
||
277 | * |
||
278 | * @param string $classToExtend Class name. |
||
279 | * @return void |
||
280 | */ |
||
281 | 1 | public function setClassToExtend($classToExtend) |
|
282 | { |
||
283 | 1 | $this->classToExtend = $classToExtend; |
|
284 | 1 | } |
|
285 | |||
286 | /** |
||
287 | * Set whether or not to generate annotations for the document |
||
288 | * |
||
289 | * @param bool $bool |
||
290 | * @return void |
||
291 | */ |
||
292 | 9 | public function setGenerateAnnotations($bool) |
|
293 | { |
||
294 | 9 | $this->generateAnnotations = $bool; |
|
295 | 9 | } |
|
296 | |||
297 | /** |
||
298 | * Set whether or not to try and update the document if it already exists |
||
299 | * |
||
300 | * @param bool $bool |
||
301 | * @return void |
||
302 | */ |
||
303 | 9 | public function setUpdateDocumentIfExists($bool) |
|
304 | { |
||
305 | 9 | $this->updateDocumentIfExists = $bool; |
|
306 | 9 | } |
|
307 | |||
308 | /** |
||
309 | * Set whether or not to regenerate the document if it exists |
||
310 | * |
||
311 | * @param bool $bool |
||
312 | * @return void |
||
313 | */ |
||
314 | 9 | public function setRegenerateDocumentIfExists($bool) |
|
315 | { |
||
316 | 9 | $this->regenerateDocumentIfExists = $bool; |
|
317 | 9 | } |
|
318 | |||
319 | /** |
||
320 | * Set whether or not to generate stub methods for the document |
||
321 | * |
||
322 | * @param bool $bool |
||
323 | * @return void |
||
324 | */ |
||
325 | 9 | public function setGenerateStubMethods($bool) |
|
326 | { |
||
327 | 9 | $this->generateDocumentStubMethods = $bool; |
|
328 | 9 | } |
|
329 | |||
330 | /** |
||
331 | * Sets a value indicating whether existing documents will be backed up. |
||
332 | * |
||
333 | * @param bool $bool True to backup existing document, false to overwrite. |
||
334 | */ |
||
335 | public function setBackupExisting($bool) |
||
339 | |||
340 | 8 | private function generateDocumentNamespace(ClassMetadataInfo $metadata) |
|
346 | |||
347 | 8 | private function generateDocumentClassName(ClassMetadataInfo $metadata) |
|
352 | |||
353 | 9 | private function generateDocumentBody(ClassMetadataInfo $metadata) |
|
354 | { |
||
355 | 9 | $fieldMappingProperties = $this->generateDocumentFieldMappingProperties($metadata); |
|
356 | 9 | $associationMappingProperties = $this->generateDocumentAssociationMappingProperties($metadata); |
|
357 | 9 | $stubMethods = $this->generateDocumentStubMethods ? $this->generateDocumentStubMethods($metadata) : null; |
|
358 | 9 | $lifecycleCallbackMethods = $this->generateDocumentLifecycleCallbackMethods($metadata); |
|
359 | |||
360 | 9 | $code = array(); |
|
361 | |||
362 | 9 | if ($fieldMappingProperties) { |
|
363 | 7 | $code[] = $fieldMappingProperties; |
|
364 | } |
||
365 | |||
366 | 9 | if ($associationMappingProperties) { |
|
367 | 6 | $code[] = $associationMappingProperties; |
|
368 | } |
||
369 | |||
370 | 9 | $code[] = $this->generateDocumentConstructor($metadata); |
|
371 | |||
372 | 9 | if ($stubMethods) { |
|
|
|||
373 | 9 | $code[] = $stubMethods; |
|
374 | } |
||
375 | |||
376 | 9 | if ($lifecycleCallbackMethods) { |
|
377 | 6 | $code[] = "\n" . $lifecycleCallbackMethods; |
|
378 | } |
||
379 | |||
380 | 9 | return implode("\n", $code); |
|
381 | } |
||
382 | |||
383 | 9 | private function generateDocumentConstructor(ClassMetadataInfo $metadata) |
|
384 | { |
||
385 | 9 | if ($this->hasMethod('__construct', $metadata)) { |
|
386 | 1 | return ''; |
|
387 | } |
||
388 | |||
389 | 9 | $collections = array(); |
|
390 | 9 | foreach ($metadata->fieldMappings AS $mapping) { |
|
391 | 9 | if ($mapping['type'] === ClassMetadataInfo::MANY) { |
|
392 | 6 | $collections[] = '$this->' . $mapping['fieldName'] . ' = new \Doctrine\Common\Collections\ArrayCollection();'; |
|
393 | } |
||
394 | } |
||
395 | 9 | if ($collections) { |
|
396 | 6 | return $this->prefixCodeWithSpaces(str_replace('<collections>', $this->spaces . implode("\n" . $this->spaces, $collections), self::$constructorMethodTemplate)); |
|
397 | } |
||
398 | 3 | return ''; |
|
399 | } |
||
400 | |||
401 | /** |
||
402 | * @todo this won't work if there is a namespace in brackets and a class outside of it. |
||
403 | * @param string $path |
||
404 | */ |
||
405 | 2 | private function parseTokensInDocumentFile($path) |
|
406 | { |
||
407 | 2 | $tokens = token_get_all(file_get_contents($path)); |
|
408 | 2 | $lastSeenNamespace = ''; |
|
409 | 2 | $lastSeenClass = false; |
|
410 | |||
411 | 2 | for ($i = 0; $i < count($tokens); $i++) { |
|
412 | 2 | $token = $tokens[$i]; |
|
413 | 2 | if ($token[0] == T_NAMESPACE) { |
|
414 | 2 | $peek = $i; |
|
415 | 2 | $lastSeenNamespace = ''; |
|
416 | 2 | while (isset($tokens[++$peek])) { |
|
417 | 2 | if (';' == $tokens[$peek]) { |
|
418 | 2 | break; |
|
419 | 2 | } elseif (is_array($tokens[$peek]) && in_array($tokens[$peek][0], array(T_STRING, T_NS_SEPARATOR))) { |
|
420 | 2 | $lastSeenNamespace .= $tokens[$peek][1]; |
|
421 | } |
||
422 | } |
||
423 | 2 | } elseif ($token[0] == T_CLASS) { |
|
424 | 2 | $lastSeenClass = $lastSeenNamespace . '\\' . $tokens[$i + 2][1]; |
|
425 | 2 | $this->staticReflection[$lastSeenClass]['properties'] = array(); |
|
426 | 2 | $this->staticReflection[$lastSeenClass]['methods'] = array(); |
|
427 | 2 | } elseif ($token[0] == T_FUNCTION) { |
|
428 | 1 | if ($tokens[$i + 2][0] == T_STRING) { |
|
429 | 1 | $this->staticReflection[$lastSeenClass]['methods'][] = $tokens[$i + 2][1]; |
|
430 | } elseif ($tokens[$i + 2][0] == '&' && $tokens[$i + 3][0] == T_STRING) { |
||
431 | $this->staticReflection[$lastSeenClass]['methods'][] = $tokens[$i + 3][1]; |
||
432 | } |
||
433 | 2 | } elseif (in_array($token[0], array(T_VAR, T_PUBLIC, T_PRIVATE, T_PROTECTED)) && $tokens[$i + 2][0] != T_FUNCTION) { |
|
434 | 2 | $this->staticReflection[$lastSeenClass]['properties'][] = substr($tokens[$i + 2][1], 1); |
|
435 | } |
||
436 | } |
||
437 | 2 | } |
|
438 | |||
439 | 9 | View Code Duplication | private function hasProperty($property, ClassMetadataInfo $metadata) |
440 | { |
||
441 | 9 | if ($this->extendsClass() || class_exists($metadata->name)) { |
|
442 | // don't generate property if its already on the base class. |
||
443 | 2 | $reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name); |
|
444 | |||
445 | 2 | if ($reflClass->hasProperty($property)) { |
|
446 | 1 | return true; |
|
447 | } |
||
448 | } |
||
449 | |||
450 | 8 | foreach ($this->getTraits($metadata) as $trait) { |
|
451 | 2 | if ($trait->hasProperty($property)) { |
|
452 | 2 | return true; |
|
453 | } |
||
454 | } |
||
455 | |||
456 | return ( |
||
457 | 8 | isset($this->staticReflection[$metadata->name]) && |
|
458 | 8 | in_array($property, $this->staticReflection[$metadata->name]['properties']) |
|
459 | ); |
||
460 | } |
||
461 | |||
462 | 9 | View Code Duplication | private function hasMethod($method, ClassMetadataInfo $metadata) |
463 | { |
||
464 | 9 | if ($this->extendsClass() || class_exists($metadata->name)) { |
|
465 | // don't generate method if its already on the base class. |
||
466 | 2 | $reflClass = new \ReflectionClass($this->getClassToExtend() ?: $metadata->name); |
|
467 | |||
468 | 2 | if ($reflClass->hasMethod($method)) { |
|
469 | 1 | return true; |
|
470 | } |
||
471 | } |
||
472 | |||
473 | 9 | foreach ($this->getTraits($metadata) as $trait) { |
|
474 | 2 | if ($trait->hasMethod($method)) { |
|
475 | 2 | return true; |
|
476 | } |
||
477 | } |
||
478 | |||
479 | return ( |
||
480 | 9 | isset($this->staticReflection[$metadata->name]) && |
|
481 | 9 | in_array($method, $this->staticReflection[$metadata->name]['methods']) |
|
482 | ); |
||
483 | } |
||
484 | |||
485 | 8 | private function hasNamespace(ClassMetadataInfo $metadata) |
|
489 | |||
490 | 9 | private function extendsClass() |
|
494 | |||
495 | 2 | private function getClassToExtend() |
|
499 | |||
500 | 1 | private function getClassToExtendName() |
|
506 | |||
507 | 8 | private function getClassName(ClassMetadataInfo $metadata) |
|
512 | |||
513 | 8 | private function getNamespace(ClassMetadataInfo $metadata) |
|
514 | { |
||
515 | 8 | return substr($metadata->name, 0, strrpos($metadata->name, '\\')); |
|
516 | } |
||
517 | |||
518 | /** |
||
519 | * @param ClassMetadataInfo $metadata |
||
520 | * |
||
521 | * @return array |
||
522 | */ |
||
523 | 9 | protected function getTraits(ClassMetadataInfo $metadata) |
|
524 | { |
||
525 | 9 | if ($metadata->reflClass !== null || class_exists($metadata->name)) { |
|
526 | 3 | $reflClass = $metadata->reflClass === null ? new \ReflectionClass($metadata->name) : $metadata->reflClass; |
|
527 | 3 | $traits = array(); |
|
528 | 3 | while ($reflClass !== false) { |
|
529 | 3 | $traits = array_merge($traits, $reflClass->getTraits()); |
|
530 | 3 | $reflClass = $reflClass->getParentClass(); |
|
531 | } |
||
532 | 3 | return $traits; |
|
533 | } |
||
534 | 6 | return array(); |
|
535 | } |
||
536 | |||
537 | 8 | private function generateDocumentImports() |
|
543 | |||
544 | 8 | private function generateDocumentDocBlock(ClassMetadataInfo $metadata) |
|
545 | { |
||
546 | 8 | $lines = array(); |
|
547 | 8 | $lines[] = '/**'; |
|
548 | 8 | $lines[] = ' * ' . $metadata->name; |
|
549 | |||
550 | 8 | if ($this->generateAnnotations) { |
|
551 | 8 | $lines[] = ' *'; |
|
552 | |||
553 | 8 | if ($metadata->isMappedSuperclass) { |
|
554 | $lines[] = ' * @ODM\\MappedSuperclass'; |
||
555 | 8 | } elseif ($metadata->isEmbeddedDocument) { |
|
556 | $lines[] = ' * @ODM\\EmbeddedDocument'; |
||
557 | 8 | } elseif ($metadata->isQueryResultDocument) { |
|
558 | $lines[] = ' * @ODM\\QueryResultDocument'; |
||
559 | } else { |
||
560 | 8 | $lines[] = ' * @ODM\\Document'; |
|
561 | } |
||
562 | |||
563 | 8 | $document = array(); |
|
564 | 8 | if ( ! $metadata->isMappedSuperclass && ! $metadata->isEmbeddedDocument && ! $metadata->isQueryResultDocument) { |
|
565 | 8 | if ($metadata->collection) { |
|
566 | 8 | $document[] = ' * collection="' . $metadata->collection . '"'; |
|
567 | } |
||
568 | 8 | if ($metadata->customRepositoryClassName) { |
|
569 | 6 | $document[] = ' * repositoryClass="' . $metadata->customRepositoryClassName . '"'; |
|
570 | } |
||
571 | } |
||
572 | 8 | if ($metadata->indexes) { |
|
573 | $indexes = array(); |
||
574 | $indexLines = array(); |
||
575 | $indexLines[] = ' * indexes={'; |
||
576 | foreach ($metadata->indexes as $index) { |
||
577 | $keys = array(); |
||
578 | View Code Duplication | foreach ($index['keys'] as $key => $value) { |
|
579 | $keys[] = '"' . $key . '"="' . $value . '"'; |
||
580 | } |
||
581 | $options = array(); |
||
582 | View Code Duplication | foreach ($index['options'] as $key => $value) { |
|
583 | $options[] = '"' . $key . '"="' . $value . '"'; |
||
584 | } |
||
585 | $indexes[] = '@ODM\\Index(keys={' . implode(', ', $keys) . '}, options={' . implode(', ', $options) . '})'; |
||
586 | } |
||
587 | $indexLines[] = "\n * " . implode(",\n * ", $indexes); |
||
588 | $indexLines[] = "\n * }"; |
||
589 | |||
590 | $document[] = implode(null, $indexLines); |
||
591 | } |
||
592 | |||
593 | 8 | if ($document) { |
|
594 | 8 | $lines[count($lines) - 1] .= '('; |
|
595 | 8 | $lines[] = implode(",\n", $document); |
|
596 | 8 | $lines[] = ' * )'; |
|
597 | } |
||
598 | |||
599 | 8 | if ( ! empty($metadata->lifecycleCallbacks)) { |
|
600 | 6 | $lines[] = ' * @ODM\HasLifecycleCallbacks'; |
|
601 | } |
||
602 | |||
603 | $methods = array( |
||
604 | 8 | 'generateInheritanceAnnotation', |
|
605 | 'generateDiscriminatorFieldAnnotation', |
||
606 | 'generateDiscriminatorMapAnnotation', |
||
607 | 'generateDefaultDiscriminatorValueAnnotation', |
||
608 | 'generateChangeTrackingPolicyAnnotation' |
||
609 | ); |
||
610 | |||
611 | 8 | foreach ($methods as $method) { |
|
612 | 8 | if ($code = $this->$method($metadata)) { |
|
613 | 8 | $lines[] = ' * ' . $code; |
|
614 | } |
||
615 | } |
||
616 | } |
||
617 | |||
618 | 8 | $lines[] = ' */'; |
|
619 | 8 | return implode("\n", $lines); |
|
620 | } |
||
621 | |||
622 | 8 | private function generateInheritanceAnnotation(ClassMetadataInfo $metadata) |
|
628 | |||
629 | 8 | private function generateDiscriminatorFieldAnnotation(ClassMetadataInfo $metadata) |
|
635 | |||
636 | 8 | private function generateDiscriminatorMapAnnotation(ClassMetadataInfo $metadata) |
|
648 | |||
649 | 8 | private function generateDefaultDiscriminatorValueAnnotation(ClassMetadataInfo $metadata) |
|
655 | |||
656 | 8 | private function generateChangeTrackingPolicyAnnotation(ClassMetadataInfo $metadata) |
|
660 | |||
661 | 9 | private function generateDocumentStubMethods(ClassMetadataInfo $metadata) |
|
705 | |||
706 | /** |
||
707 | * @param array $fieldMapping |
||
708 | * |
||
709 | * @return bool |
||
710 | */ |
||
711 | 8 | protected function isAssociationNullable($fieldMapping) |
|
715 | |||
716 | 9 | private function generateDocumentLifecycleCallbackMethods(ClassMetadataInfo $metadata) |
|
734 | |||
735 | 9 | private function generateDocumentAssociationMappingProperties(ClassMetadataInfo $metadata) |
|
755 | |||
756 | 9 | private function generateDocumentFieldMappingProperties(ClassMetadataInfo $metadata) |
|
776 | |||
777 | 9 | private function generateDocumentStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null) |
|
778 | { |
||
779 | // Add/remove methods should use the singular form of the field name |
||
780 | 9 | $formattedFieldName = in_array($type, array('add', 'remove')) |
|
781 | 6 | ? Inflector::singularize($fieldName) |
|
782 | 9 | : $fieldName; |
|
783 | |||
784 | 9 | $methodName = $type . Inflector::classify($formattedFieldName); |
|
785 | 9 | $variableName = Inflector::camelize($formattedFieldName); |
|
786 | |||
787 | 9 | if ($this->hasMethod($methodName, $metadata)) { |
|
788 | 4 | return; |
|
789 | } |
||
790 | |||
791 | 9 | $description = ucfirst($type) . ' ' . $variableName; |
|
792 | |||
793 | 9 | $types = Type::getTypesMap(); |
|
794 | 9 | $methodTypeHint = $typeHint && ! isset($types[$typeHint]) ? '\\' . $typeHint . ' ' : null; |
|
795 | 9 | $variableType = $typeHint ? $typeHint . ' ' : null; |
|
796 | |||
797 | $replacements = array( |
||
798 | 9 | '<description>' => $description, |
|
799 | 9 | '<methodTypeHint>' => $methodTypeHint, |
|
800 | 9 | '<variableType>' => $variableType, |
|
801 | 9 | '<variableName>' => $variableName, |
|
802 | 9 | '<methodName>' => $methodName, |
|
803 | 9 | '<fieldName>' => $fieldName, |
|
804 | 9 | '<variableDefault>' => ($defaultValue !== null ) ? (' = ' . $defaultValue) : '', |
|
805 | ); |
||
806 | |||
807 | 9 | $templateVar = sprintf('%sMethodTemplate', $type); |
|
808 | |||
809 | 9 | $method = str_replace( |
|
810 | 9 | array_keys($replacements), |
|
811 | 9 | array_values($replacements), |
|
812 | 9 | self::$$templateVar |
|
813 | ); |
||
814 | |||
815 | 9 | return $this->prefixCodeWithSpaces($method); |
|
816 | } |
||
817 | |||
818 | 6 | private function generateLifecycleCallbackMethod($name, $methodName, ClassMetadataInfo $metadata) |
|
819 | { |
||
820 | 6 | if ($this->hasMethod($methodName, $metadata)) { |
|
821 | 1 | return; |
|
822 | } |
||
823 | |||
824 | $replacements = array( |
||
825 | 6 | '<comment>' => $this->generateAnnotations ? '/** @ODM\\' . ucfirst($name) . ' */' : '', |
|
826 | 6 | '<methodName>' => $methodName, |
|
827 | ); |
||
828 | |||
829 | 6 | $method = str_replace( |
|
830 | 6 | array_keys($replacements), |
|
831 | 6 | array_values($replacements), |
|
832 | 6 | self::$lifecycleCallbackMethodTemplate |
|
833 | ); |
||
834 | |||
835 | 6 | return $this->prefixCodeWithSpaces($method); |
|
836 | } |
||
837 | |||
838 | 6 | private function generateAssociationMappingPropertyDocBlock(array $fieldMapping) |
|
887 | |||
888 | 7 | private function generateFieldMappingPropertyDocBlock(array $fieldMapping, ClassMetadataInfo $metadata) |
|
949 | |||
950 | 9 | private function prefixCodeWithSpaces($code, $num = 1) |
|
960 | |||
961 | private function getInheritanceTypeString($type) |
||
977 | |||
978 | 8 | private function getChangeTrackingPolicyString($policy) |
|
994 | |||
995 | 7 | private function getIdGeneratorTypeString($type) |
|
1020 | } |
||
1021 |
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: