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 |
||
28 | class ProxyFactory extends AbstractProxyFactory |
||
29 | { |
||
30 | /** @var ClassMetadataFactory */ |
||
31 | private $metadataFactory; |
||
32 | |||
33 | /** @var UnitOfWork The UnitOfWork this factory is bound to. */ |
||
34 | private $uow; |
||
35 | |||
36 | /** @var string The namespace that contains all proxy classes. */ |
||
37 | private $proxyNamespace; |
||
38 | |||
39 | /** @var EventManager */ |
||
40 | private $lifecycleEventManager; |
||
41 | |||
42 | /** |
||
43 | * Initializes a new instance of the <tt>ProxyFactory</tt> class that is |
||
44 | * connected to the given <tt>DocumentManager</tt>. |
||
45 | * |
||
46 | * @param DocumentManager $documentManager The DocumentManager the new factory works for. |
||
47 | * @param string $proxyDir The directory to use for the proxy classes. It |
||
48 | * must exist. |
||
49 | * @param string $proxyNamespace The namespace to use for the proxy classes. |
||
50 | * @param int $autoGenerate Whether to automatically generate proxy classes. |
||
51 | */ |
||
52 | 1580 | public function __construct(DocumentManager $documentManager, $proxyDir, $proxyNamespace, $autoGenerate = AbstractProxyFactory::AUTOGENERATE_NEVER) |
|
64 | |||
65 | /** |
||
66 | * {@inheritDoc} |
||
67 | */ |
||
68 | public function skipClass(BaseClassMetadata $class) |
||
73 | |||
74 | /** |
||
75 | * {@inheritDoc} |
||
76 | */ |
||
77 | 97 | public function createProxyDefinition($className) |
|
92 | |||
93 | /** |
||
94 | * Generates a closure capable of initializing a proxy |
||
95 | * |
||
96 | * |
||
97 | * @return \Closure |
||
98 | * |
||
99 | * @throws DocumentNotFoundException |
||
100 | */ |
||
101 | 97 | private function createInitializer( |
|
102 | BaseClassMetadata $classMetadata, |
||
103 | DocumentPersister $documentPersister, |
||
104 | ReflectionProperty $reflectionId |
||
105 | ) { |
||
106 | 97 | if ($classMetadata->getReflectionClass()->hasMethod('__wakeup')) { |
|
107 | View Code Duplication | return function (BaseProxy $proxy) use ($documentPersister, $reflectionId) { |
|
108 | $proxy->__setInitializer(null); |
||
109 | $proxy->__setCloner(null); |
||
110 | |||
111 | if ($proxy->__isInitialized()) { |
||
112 | return; |
||
113 | } |
||
114 | |||
115 | $properties = $proxy->__getLazyProperties(); |
||
116 | |||
117 | foreach ($properties as $propertyName => $property) { |
||
118 | if (isset($proxy->$propertyName)) { |
||
119 | continue; |
||
120 | } |
||
121 | |||
122 | $proxy->$propertyName = $properties[$propertyName]; |
||
123 | } |
||
124 | |||
125 | $proxy->__setInitialized(true); |
||
126 | $proxy->__wakeup(); |
||
127 | |||
128 | $id = $reflectionId->getValue($proxy); |
||
129 | |||
130 | if ($documentPersister->load(['_id' => $id], $proxy) === null) { |
||
131 | if (! $this->lifecycleEventManager->documentNotFound($proxy, $id)) { |
||
132 | throw DocumentNotFoundException::documentNotFound(get_class($proxy), $id); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | if (! ($proxy instanceof NotifyPropertyChanged)) { |
||
137 | return; |
||
138 | } |
||
139 | |||
140 | $proxy->addPropertyChangedListener($this->uow); |
||
141 | }; |
||
142 | } |
||
143 | |||
144 | 97 | View Code Duplication | return function (BaseProxy $proxy) use ($documentPersister, $reflectionId) { |
145 | 31 | $proxy->__setInitializer(null); |
|
146 | 31 | $proxy->__setCloner(null); |
|
147 | |||
148 | 31 | if ($proxy->__isInitialized()) { |
|
149 | return; |
||
150 | } |
||
151 | |||
152 | 31 | $properties = $proxy->__getLazyProperties(); |
|
153 | |||
154 | 31 | foreach ($properties as $propertyName => $property) { |
|
155 | 12 | if (isset($proxy->$propertyName)) { |
|
156 | continue; |
||
157 | } |
||
158 | |||
159 | 12 | $proxy->$propertyName = $properties[$propertyName]; |
|
160 | } |
||
161 | |||
162 | 31 | $proxy->__setInitialized(true); |
|
163 | |||
164 | 31 | $id = $reflectionId->getValue($proxy); |
|
165 | |||
166 | 31 | if ($documentPersister->load(['_id' => $id], $proxy) === null) { |
|
167 | 9 | if (! $this->lifecycleEventManager->documentNotFound($proxy, $id)) { |
|
168 | 8 | throw DocumentNotFoundException::documentNotFound(get_class($proxy), $id); |
|
169 | } |
||
170 | } |
||
171 | |||
172 | 25 | if (! ($proxy instanceof NotifyPropertyChanged)) { |
|
173 | 24 | return; |
|
174 | } |
||
175 | |||
176 | 1 | $proxy->addPropertyChangedListener($this->uow); |
|
177 | 97 | }; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Generates a closure capable of finalizing a cloned proxy |
||
182 | * |
||
183 | * |
||
184 | * @return \Closure |
||
185 | * |
||
186 | * @throws DocumentNotFoundException |
||
187 | */ |
||
188 | private function createCloner( |
||
222 | } |
||
223 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..