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 DefaultPersistentCollectionGenerator 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 DefaultPersistentCollectionGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | final class DefaultPersistentCollectionGenerator implements PersistentCollectionGenerator |
||
30 | { |
||
31 | /** |
||
32 | * The namespace that contains all persistent collection classes. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $collectionNamespace; |
||
37 | |||
38 | /** |
||
39 | * The directory that contains all persistent collection classes. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | private $collectionDir; |
||
44 | |||
45 | /** |
||
46 | * @param string $collectionDir |
||
47 | * @param string $collectionNs |
||
48 | */ |
||
49 | 11 | public function __construct($collectionDir, $collectionNs) |
|
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function generateClass($class, $dir) |
||
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 10 | public function loadClass($collectionClass, $autoGenerate) |
|
70 | { |
||
71 | // These checks are not in __construct() because of BC and should be moved for 2.0 |
||
72 | 10 | if ( ! $this->collectionDir) { |
|
73 | throw PersistentCollectionException::directoryRequired(); |
||
74 | } |
||
75 | 10 | if ( ! $this->collectionNamespace) { |
|
76 | throw PersistentCollectionException::namespaceRequired(); |
||
77 | } |
||
78 | |||
79 | 10 | $collClassName = str_replace('\\', '', $collectionClass) . 'Persistent'; |
|
80 | 10 | $className = $this->collectionNamespace . '\\' . $collClassName; |
|
81 | 10 | View Code Duplication | if ( ! class_exists($className, false)) { |
|
|||
82 | 6 | $fileName = $this->collectionDir . DIRECTORY_SEPARATOR . $collClassName . '.php'; |
|
83 | switch ($autoGenerate) { |
||
84 | 6 | case Configuration::AUTOGENERATE_NEVER: |
|
85 | require $fileName; |
||
86 | break; |
||
87 | |||
88 | 6 | case Configuration::AUTOGENERATE_ALWAYS: |
|
89 | 3 | $this->generateCollectionClass($collectionClass, $className, $fileName); |
|
90 | 3 | require $fileName; |
|
91 | 3 | break; |
|
92 | |||
93 | 3 | case Configuration::AUTOGENERATE_FILE_NOT_EXISTS: |
|
94 | if ( ! file_exists($fileName)) { |
||
95 | $this->generateCollectionClass($collectionClass, $className, $fileName); |
||
96 | } |
||
97 | require $fileName; |
||
98 | break; |
||
99 | |||
100 | 3 | case Configuration::AUTOGENERATE_EVAL: |
|
101 | 3 | $this->generateCollectionClass($collectionClass, $className, false); |
|
102 | 3 | break; |
|
103 | } |
||
104 | } |
||
105 | |||
106 | 10 | return $className; |
|
107 | } |
||
108 | |||
109 | 6 | private function generateCollectionClass($for, $targetFqcn, $fileName) |
|
110 | { |
||
111 | 6 | $exploded = explode('\\', $targetFqcn); |
|
112 | 6 | $class = array_pop($exploded); |
|
113 | 6 | $namespace = join('\\', $exploded); |
|
114 | $code = <<<CODE |
||
115 | <?php |
||
116 | |||
117 | 6 | namespace $namespace; |
|
118 | |||
119 | use Doctrine\Common\Collections\Collection as BaseCollection; |
||
120 | use Doctrine\ODM\MongoDB\DocumentManager; |
||
121 | use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
||
122 | use Doctrine\ODM\MongoDB\MongoDBException; |
||
123 | use Doctrine\ODM\MongoDB\UnitOfWork; |
||
124 | use Doctrine\ODM\MongoDB\Utility\CollectionHelper; |
||
125 | |||
126 | /** |
||
127 | * DO NOT EDIT THIS FILE - IT WAS CREATED BY DOCTRINE\'S PERSISTENT COLLECTION GENERATOR |
||
128 | */ |
||
129 | 6 | class $class extends \\$for implements \\Doctrine\\ODM\\MongoDB\\PersistentCollection\\PersistentCollectionInterface |
|
130 | { |
||
131 | use \\Doctrine\\ODM\\MongoDB\\PersistentCollection\\PersistentCollectionTrait; |
||
132 | |||
133 | /** |
||
134 | * @param BaseCollection \$coll |
||
135 | * @param DocumentManager \$dm |
||
136 | * @param UnitOfWork \$uow |
||
137 | */ |
||
138 | public function __construct(BaseCollection \$coll, DocumentManager \$dm, UnitOfWork \$uow) |
||
139 | { |
||
140 | \$this->coll = \$coll; |
||
141 | \$this->dm = \$dm; |
||
142 | \$this->uow = \$uow; |
||
143 | } |
||
144 | |||
145 | CODE; |
||
146 | 6 | $rc = new \ReflectionClass($for); |
|
147 | 6 | $rt = new \ReflectionClass('Doctrine\\ODM\\MongoDB\\PersistentCollection\\PersistentCollectionTrait'); |
|
148 | 6 | foreach ($rc->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
149 | if ( |
||
150 | 6 | $rt->hasMethod($method->name) || |
|
151 | 6 | $method->isConstructor() || |
|
152 | 6 | $method->isFinal() || |
|
153 | 6 | $method->isStatic() |
|
154 | ) { |
||
155 | 6 | continue; |
|
156 | } |
||
157 | 6 | $code .= $this->generateMethod($method); |
|
158 | } |
||
159 | 6 | $code .= "}\n"; |
|
160 | |||
161 | 6 | if ($fileName === false) { |
|
162 | 3 | if ( ! class_exists($targetFqcn)) { |
|
163 | 3 | eval(substr($code, 5)); |
|
164 | } |
||
165 | View Code Duplication | } else { |
|
166 | 3 | $parentDirectory = dirname($fileName); |
|
167 | |||
168 | 3 | if ( ! is_dir($parentDirectory) && (false === @mkdir($parentDirectory, 0775, true))) { |
|
169 | throw PersistentCollectionException::directoryNotWritable(); |
||
170 | } |
||
171 | |||
172 | 3 | if ( ! is_writable($parentDirectory)) { |
|
173 | throw PersistentCollectionException::directoryNotWritable(); |
||
174 | } |
||
175 | |||
176 | 3 | $tmpFileName = $fileName . '.' . uniqid('', true); |
|
177 | 3 | file_put_contents($tmpFileName, $code); |
|
178 | 3 | rename($tmpFileName, $fileName); |
|
179 | } |
||
180 | 6 | } |
|
181 | |||
182 | 6 | private function generateMethod(\ReflectionMethod $method) |
|
183 | { |
||
184 | 6 | $parametersString = $this->buildParametersString($method); |
|
185 | 6 | $callParamsString = implode(', ', $this->getParameterNamesForDecoratedCall($method->getParameters())); |
|
186 | |||
187 | $method = <<<CODE |
||
188 | |||
189 | /** |
||
190 | * {@inheritDoc} |
||
191 | */ |
||
192 | 6 | public function {$method->name}($parametersString){$this->getMethodReturnType($method)} |
|
193 | { |
||
194 | \$this->initialize(); |
||
195 | if (\$this->needsSchedulingForDirtyCheck()) { |
||
196 | \$this->changed(); |
||
197 | } |
||
198 | 6 | return \$this->coll->{$method->name}($callParamsString); |
|
199 | } |
||
200 | |||
201 | CODE; |
||
202 | 6 | return $method; |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * @param \ReflectionMethod $method |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | 6 | private function buildParametersString(\ReflectionMethod $method) |
|
245 | |||
246 | /** |
||
247 | * @param \ReflectionParameter $parameter |
||
248 | * |
||
249 | * @return string|null |
||
250 | */ |
||
251 | 6 | private function getParameterType(\ReflectionParameter $parameter) |
|
275 | |||
276 | /** |
||
277 | * @param \ReflectionParameter[] $parameters |
||
278 | * |
||
279 | * @return string[] |
||
280 | */ |
||
281 | 6 | private function getParameterNamesForDecoratedCall(array $parameters) |
|
300 | |||
301 | /** |
||
302 | * @param \ReflectionMethod $method |
||
303 | * |
||
304 | * @return string |
||
305 | * |
||
306 | * @see \Doctrine\Common\Proxy\ProxyGenerator::getMethodReturnType() |
||
307 | */ |
||
308 | 6 | private function getMethodReturnType(\ReflectionMethod $method) |
|
315 | |||
316 | /** |
||
317 | * @param \ReflectionType $type |
||
318 | * @param \ReflectionMethod $method |
||
319 | * @param \ReflectionParameter|null $parameter |
||
320 | * |
||
321 | * @return string |
||
322 | * |
||
323 | * @see \Doctrine\Common\Proxy\ProxyGenerator::formatType() |
||
324 | */ |
||
325 | 2 | private function formatType( |
|
361 | } |
||
362 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.