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 |
||
11 | class DefaultNamingStrategy extends AbstractNamingStrategy |
||
12 | { |
||
13 | private $beanPrefix = ''; |
||
14 | private $beanSuffix = ''; |
||
15 | private $baseBeanPrefix = 'Abstract'; |
||
16 | private $baseBeanSuffix = ''; |
||
17 | private $daoPrefix = ''; |
||
18 | private $daoSuffix = 'Dao'; |
||
19 | private $baseDaoPrefix = 'Abstract'; |
||
20 | private $baseDaoSuffix = 'Dao'; |
||
21 | private $exceptions = []; |
||
22 | |||
23 | /** |
||
24 | * Sets the string prefix to any bean class name. |
||
25 | * |
||
26 | * @param string $beanPrefix |
||
27 | */ |
||
28 | public function setBeanPrefix(string $beanPrefix) |
||
32 | |||
33 | /** |
||
34 | * Sets the string suffix to any bean class name. |
||
35 | * |
||
36 | * @param string $beanSuffix |
||
37 | */ |
||
38 | public function setBeanSuffix(string $beanSuffix) |
||
42 | |||
43 | /** |
||
44 | * Sets the string prefix to any base bean class name. |
||
45 | * |
||
46 | * @param string $baseBeanPrefix |
||
47 | */ |
||
48 | public function setBaseBeanPrefix(string $baseBeanPrefix) |
||
52 | |||
53 | /** |
||
54 | * Sets the string suffix to any base bean class name. |
||
55 | * |
||
56 | * @param string $baseBeanSuffix |
||
57 | */ |
||
58 | public function setBaseBeanSuffix(string $baseBeanSuffix) |
||
62 | |||
63 | /** |
||
64 | * Sets the string prefix to any DAO class name. |
||
65 | * |
||
66 | * @param string $daoPrefix |
||
67 | */ |
||
68 | public function setDaoPrefix(string $daoPrefix) |
||
72 | |||
73 | /** |
||
74 | * Sets the string suffix to any DAO class name. |
||
75 | * |
||
76 | * @param string $daoSuffix |
||
77 | */ |
||
78 | public function setDaoSuffix(string $daoSuffix) |
||
82 | |||
83 | /** |
||
84 | * Sets the string prefix to any base DAO class name. |
||
85 | * |
||
86 | * @param string $baseDaoPrefix |
||
87 | */ |
||
88 | public function setBaseDaoPrefix(string $baseDaoPrefix) |
||
92 | |||
93 | /** |
||
94 | * Sets the string suffix to any base DAO class name. |
||
95 | * |
||
96 | * @param string $baseDaoSuffix |
||
97 | */ |
||
98 | public function setBaseDaoSuffix(string $baseDaoSuffix) |
||
102 | |||
103 | |||
104 | /** |
||
105 | * Returns the bean class name from the table name (excluding the namespace). |
||
106 | * |
||
107 | * @param string $tableName |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getBeanClassName(string $tableName): string |
||
114 | |||
115 | /** |
||
116 | * Returns the base bean class name from the table name (excluding the namespace). |
||
117 | * |
||
118 | * @param string $tableName |
||
119 | * @return string |
||
120 | */ |
||
121 | public function getBaseBeanClassName(string $tableName): string |
||
125 | |||
126 | /** |
||
127 | * Returns the name of the DAO class from the table name (excluding the namespace). |
||
128 | * |
||
129 | * @param string $tableName |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getDaoClassName(string $tableName): string |
||
136 | |||
137 | /** |
||
138 | * Returns the name of the base DAO class from the table name (excluding the namespace). |
||
139 | * |
||
140 | * @param string $tableName |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getBaseDaoClassName(string $tableName): string |
||
147 | |||
148 | /** |
||
149 | * Tries to put string to the singular form (if it is plural) and camel case form. |
||
150 | * We assume the table names are in english. |
||
151 | * |
||
152 | * @param $str string |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | View Code Duplication | private function toSingularCamelCase(string $str): string |
|
172 | |||
173 | /** |
||
174 | * Put string to camel case form. |
||
175 | * |
||
176 | * @param $str string |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | View Code Duplication | private function toCamelCase(string $str): string |
|
196 | |||
197 | /** |
||
198 | * Returns the class name for the DAO factory. |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getDaoFactoryClassName(): string |
||
206 | |||
207 | /** |
||
208 | * Sets exceptions in the naming of classes. |
||
209 | * The key is the name of the table, the value the "base" name of beans and DAOs. |
||
210 | * |
||
211 | * This is very useful for dealing with plural to singular translations in non english table names. |
||
212 | * |
||
213 | * For instance if you are dealing with a table containing horses in French ("chevaux" that has a singular "cheval"): |
||
214 | * |
||
215 | * [ |
||
216 | * "chevaux" => "Cheval" |
||
217 | * ] |
||
218 | * |
||
219 | * @param array<string,string> $exceptions |
||
220 | */ |
||
221 | public function setExceptions(array $exceptions): void |
||
225 | |||
226 | protected function getForeignKeyUpperCamelCaseName(ForeignKeyConstraint $foreignKey, bool $alternativeName): string |
||
254 | |||
255 | protected function getScalarColumnUpperCamelCaseName(string $columnName, bool $alternativeName): string |
||
259 | |||
260 | protected function getUpperCamelCaseName(AbstractBeanPropertyDescriptor $property): string |
||
270 | } |
||
271 |
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.