Complex classes like EntityRepositoryGenerator 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 EntityRepositoryGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class EntityRepositoryGenerator |
||
38 | { |
||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $repositoryName; |
||
43 | |||
44 | /** |
||
45 | * Hash-map for handle types. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $typeAlias = [ |
||
50 | Type::DATETIMETZ => '\DateTime', |
||
51 | Type::DATETIME => '\DateTime', |
||
52 | Type::DATE => '\DateTime', |
||
53 | Type::TIME => '\DateTime', |
||
54 | Type::OBJECT => '\stdClass', |
||
55 | Type::INTEGER => 'int', |
||
56 | Type::BIGINT => 'int', |
||
57 | Type::SMALLINT => 'int', |
||
58 | Type::TEXT => 'string', |
||
59 | Type::BLOB => 'string', |
||
60 | Type::DECIMAL => 'string', |
||
61 | Type::JSON_ARRAY => 'array', |
||
62 | Type::SIMPLE_ARRAY => 'array', |
||
63 | Type::BOOLEAN => 'bool', |
||
64 | ]; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | protected static $_template = |
||
70 | '<?php |
||
71 | |||
72 | <namespace> |
||
73 | |||
74 | /** |
||
75 | * <className> |
||
76 | * |
||
77 | * This class was generated by the Doctrine ORM. Add your own custom |
||
78 | * repository methods below. |
||
79 | */ |
||
80 | class <className> extends <repositoryName> |
||
81 | { |
||
82 | } |
||
83 | '; |
||
84 | |||
85 | /** |
||
86 | * @var string |
||
87 | */ |
||
88 | protected static $_templateWithMagicMethodDocblocks = <<<'EOT' |
||
89 | <?php |
||
90 | |||
91 | <namespace> |
||
92 | |||
93 | /** |
||
94 | * <className> |
||
95 | * |
||
96 | <magicMethodDocblocks> |
||
97 | * |
||
98 | * This class was generated by the Doctrine ORM. Add your own custom |
||
99 | * repository methods below. |
||
100 | */ |
||
101 | class <className> extends <repositoryName> |
||
102 | { |
||
103 | } |
||
104 | EOT; |
||
105 | |||
106 | /** |
||
107 | * @var string |
||
108 | */ |
||
109 | protected static $findByDocblockTemplate = <<<'EOT' |
||
110 | * @method <entity>[] findBy<fieldName>(<arg1TypeHint>$<arg1Name>) Finds occurrences of <entity> matching the <arg1Name> property. |
||
111 | EOT; |
||
112 | |||
113 | /** |
||
114 | * @var string |
||
115 | */ |
||
116 | protected static $findOneByDocblockTemplate = <<<'EOT' |
||
117 | * @method <entity> findOneBy<fieldName>(<arg1TypeHint>$<arg1Name>) Finds a single occurrence of <entity> matching the <arg1Name> property, if any. |
||
118 | EOT; |
||
119 | |||
120 | /** |
||
121 | * @var string |
||
122 | */ |
||
123 | protected static $countByDocblockTemplate = <<<'EOT' |
||
124 | * @method int countBy<fieldName>(<arg1TypeHint>$<arg1Name>) Counts the number of occurrences of <entity> matching the <arg1Name> property. |
||
125 | EOT; |
||
126 | |||
127 | /** |
||
128 | * @param string $fullClassName |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | 4 | public function generateEntityRepositoryClass($fullClassName) |
|
142 | |||
143 | /** |
||
144 | * @param ClassMetadataInfo $metadata |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function generateEntityRepositoryClassWithMagicMethodDocblocks(ClassMetadataInfo $metadata) |
||
159 | |||
160 | /** |
||
161 | * Generates the namespace, if class do not have namespace, return empty string instead. |
||
162 | * |
||
163 | * @param string $fullClassName |
||
164 | * |
||
165 | * @return string $namespace |
||
166 | */ |
||
167 | 4 | private function getClassNamespace($fullClassName) |
|
173 | |||
174 | /** |
||
175 | * Generates the class name |
||
176 | * |
||
177 | * @param string $fullClassName |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | 4 | private function generateClassName($fullClassName) |
|
193 | |||
194 | /** |
||
195 | * Generates the namespace statement, if class do not have namespace, return empty string instead. |
||
196 | * |
||
197 | * @param string $fullClassName The full repository class name. |
||
198 | * |
||
199 | * @return string $namespace |
||
200 | */ |
||
201 | 4 | private function generateEntityRepositoryNamespace($fullClassName) |
|
207 | |||
208 | /** |
||
209 | * @param string $fullClassName |
||
210 | * |
||
211 | * @return string $repositoryName |
||
212 | */ |
||
213 | 4 | private function generateEntityRepositoryName($fullClassName) |
|
225 | |||
226 | /** |
||
227 | * @param string $fullClassName |
||
228 | * @param string $outputDirectory |
||
229 | * |
||
230 | * @return void |
||
231 | */ |
||
232 | 4 | public function writeEntityRepositoryClass($fullClassName, $outputDirectory) |
|
249 | |||
250 | /** |
||
251 | * @param string $repositoryName |
||
252 | * |
||
253 | * @return \Doctrine\ORM\Tools\EntityRepositoryGenerator |
||
254 | */ |
||
255 | 4 | public function setDefaultRepositoryName($repositoryName) |
|
261 | |||
262 | public function writeEntityRepository(ClassMetadataInfo $metadata, $outputDirectory) |
||
279 | |||
280 | public function generate(array $metadatas, $outputDirectory) |
||
286 | |||
287 | public function generateDocblocksForMagicMethods(ClassMetadataInfo $metadata) |
||
345 | |||
346 | protected function generateDocblocksForMagicMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null) |
||
376 | |||
377 | /** |
||
378 | * @param string $type |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | protected function getType($type) |
||
390 | |||
391 | /** |
||
392 | * @param ClassMetadataInfo $metadata |
||
393 | * |
||
394 | * @return string |
||
395 | */ |
||
396 | protected function getClassName(ClassMetadataInfo $metadata) |
||
401 | |||
402 | /** |
||
403 | * @param array $associationMapping |
||
404 | * |
||
405 | * @return bool |
||
406 | */ |
||
407 | protected function isAssociationIsNullable($associationMapping) |
||
428 | } |
||
429 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.