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:
1 | <?php |
||
40 | class ProxyFactory extends AbstractProxyFactory |
||
41 | { |
||
42 | /** |
||
43 | * @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory |
||
44 | */ |
||
45 | private $metadataFactory; |
||
|
|||
46 | |||
47 | /** |
||
48 | * @var \Doctrine\ODM\MongoDB\UnitOfWork The UnitOfWork this factory is bound to. |
||
49 | */ |
||
50 | private $uow; |
||
51 | |||
52 | /** |
||
53 | * @var string The namespace that contains all proxy classes. |
||
54 | */ |
||
55 | private $proxyNamespace; |
||
56 | |||
57 | /** |
||
58 | * @var \Doctrine\Common\EventManager |
||
59 | */ |
||
60 | private $lifecycleEventManager; |
||
61 | |||
62 | /** |
||
63 | * Initializes a new instance of the <tt>ProxyFactory</tt> class that is |
||
64 | * connected to the given <tt>DocumentManager</tt>. |
||
65 | * |
||
66 | * @param \Doctrine\ODM\MongoDB\DocumentManager $documentManager The DocumentManager the new factory works for. |
||
67 | * @param string $proxyDir The directory to use for the proxy classes. It |
||
68 | * must exist. |
||
69 | * @param string $proxyNamespace The namespace to use for the proxy classes. |
||
70 | * @param integer $autoGenerate Whether to automatically generate proxy classes. |
||
71 | */ |
||
72 | 1038 | public function __construct(DocumentManager $documentManager, $proxyDir, $proxyNamespace, $autoGenerate = AbstractProxyFactory::AUTOGENERATE_NEVER) |
|
84 | |||
85 | /** |
||
86 | * {@inheritDoc} |
||
87 | */ |
||
88 | public function skipClass(BaseClassMetadata $class) |
||
89 | { |
||
90 | /* @var $class \Doctrine\ODM\Mongodb\Mapping\ClassMetadataInfo */ |
||
91 | return $class->isMappedSuperclass || $class->getReflectionClass()->isAbstract(); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@inheritDoc} |
||
96 | */ |
||
97 | 93 | public function createProxyDefinition($className) |
|
98 | { |
||
99 | /* @var $classMetadata \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo */ |
||
100 | 93 | $classMetadata = $this->metadataFactory->getMetadataFor($className); |
|
101 | 93 | $documentPersister = $this->uow->getDocumentPersister($className); |
|
102 | 93 | $reflectionId = $classMetadata->reflFields[$classMetadata->identifier]; |
|
103 | |||
104 | 93 | return new ProxyDefinition( |
|
105 | 93 | ClassUtils::generateProxyClassName($className, $this->proxyNamespace), |
|
106 | 93 | $classMetadata->getIdentifierFieldNames(), |
|
107 | 93 | $classMetadata->getReflectionProperties(), |
|
108 | 93 | $this->createInitializer($classMetadata, $documentPersister, $reflectionId), |
|
109 | 93 | $this->createCloner($classMetadata, $documentPersister, $reflectionId) |
|
110 | ); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Generates a closure capable of initializing a proxy |
||
115 | * |
||
116 | * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata |
||
117 | * @param \Doctrine\ODM\MongoDB\Persisters\DocumentPersister $documentPersister |
||
118 | * @param \ReflectionProperty $reflectionId |
||
119 | * |
||
120 | * @return \Closure |
||
121 | * |
||
122 | * @throws \Doctrine\ODM\MongoDB\DocumentNotFoundException |
||
123 | */ |
||
124 | 93 | private function createInitializer( |
|
194 | |||
195 | /** |
||
196 | * Generates a closure capable of finalizing a cloned proxy |
||
197 | * |
||
198 | * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata |
||
199 | * @param \Doctrine\ODM\MongoDB\Persisters\DocumentPersister $documentPersister |
||
200 | * @param \ReflectionProperty $reflectionId |
||
201 | * |
||
202 | * @return \Closure |
||
203 | * |
||
204 | * @throws \Doctrine\ODM\MongoDB\DocumentNotFoundException |
||
205 | */ |
||
206 | private function createCloner( |
||
238 | } |
||
239 |