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 EntitiesGeneratorService 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 EntitiesGeneratorService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class EntitiesGeneratorService |
||
| 18 | { |
||
| 19 | private $zohoClient; |
||
| 20 | private $logger; |
||
| 21 | |||
| 22 | public function __construct(ZohoClient $zohoClient, LoggerInterface $logger) |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Generate ALL entities for all Zoho modules. |
||
| 30 | * |
||
| 31 | * @param string $targetDirectory |
||
| 32 | * @param string $namespace |
||
| 33 | * |
||
| 34 | * @return array Array containing each fully qualified dao class name |
||
| 35 | */ |
||
| 36 | public function generateAll($targetDirectory, $namespace) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Generate a dao for a zoho module. |
||
| 58 | * |
||
| 59 | * @param string $moduleName |
||
| 60 | * @param string $modulePlural |
||
| 61 | * @param string $moduleSingular |
||
| 62 | * @param string $targetDirectory |
||
| 63 | * @param string $namespace |
||
| 64 | * |
||
| 65 | * @return string The fully qualified Dao class name |
||
| 66 | */ |
||
| 67 | public function generateModule($moduleName, $modulePlural, $moduleSingular, $targetDirectory, $namespace) |
||
| 91 | |||
| 92 | public function generateBean($fields, $namespace, $className, $moduleName, $targetDirectory, $moduleSingular) |
||
| 93 | { |
||
| 94 | |||
| 95 | // if (class_exists($namespace."\\".$className)) { |
||
|
|
|||
| 96 | // $class = PhpClass::fromReflection(new \ReflectionClass($namespace."\\".$className)); |
||
| 97 | // } else { |
||
| 98 | $class = PhpClass::create(); |
||
| 99 | // } |
||
| 100 | |||
| 101 | $class->setName($className) |
||
| 102 | ->setNamespace($namespace) |
||
| 103 | ->addInterface('\\Wabel\\Zoho\\CRM\\ZohoBeanInterface') |
||
| 104 | ->setMethod(PhpMethod::create('__construct')); |
||
| 105 | |||
| 106 | // Let's add the ZohoID property |
||
| 107 | self::registerProperty($class, 'zohoId', "The ID of this record in Zoho\nType: string\n", 'string'); |
||
| 108 | |||
| 109 | $usedIdentifiers = []; |
||
| 110 | |||
| 111 | foreach ($fields as &$fieldCategory) { |
||
| 112 | foreach ($fieldCategory as $name => &$field) { |
||
| 113 | $req = $field['req']; |
||
| 114 | $type = $field['type']; |
||
| 115 | $isreadonly = $field['isreadonly']; |
||
| 116 | $maxlength = $field['maxlength']; |
||
| 117 | $label = $field['label']; |
||
| 118 | $dv = $field['dv']; |
||
| 119 | $customfield = $field['customfield']; |
||
| 120 | $nullable = false; |
||
| 121 | |||
| 122 | View Code Duplication | switch ($type) { |
|
| 123 | case 'DateTime': |
||
| 124 | case 'Date': |
||
| 125 | $phpType = '\\DateTimeInterface'; |
||
| 126 | $nullable = true; |
||
| 127 | break; |
||
| 128 | case 'Boolean': |
||
| 129 | $phpType = 'bool'; |
||
| 130 | break; |
||
| 131 | case 'Integer': |
||
| 132 | $phpType = 'int'; |
||
| 133 | break; |
||
| 134 | default: |
||
| 135 | $phpType = 'string'; |
||
| 136 | break; |
||
| 137 | } |
||
| 138 | |||
| 139 | $field['phpType'] = $phpType; |
||
| 140 | |||
| 141 | $identifier = $this->getUniqueIdentifier($name, $usedIdentifiers); |
||
| 142 | $usedIdentifiers[$identifier] = true; |
||
| 143 | |||
| 144 | self::registerProperty($class, $identifier, 'Zoho field '.$name."\n". |
||
| 145 | 'Type: '.$type."\n". |
||
| 146 | 'Read only: '.($isreadonly ? 'true' : 'false')."\n". |
||
| 147 | 'Max length: '.$maxlength."\n". |
||
| 148 | 'Custom field: '.($customfield ? 'true' : 'false')."\n", $phpType, $nullable); |
||
| 149 | |||
| 150 | // Adds a ID field for lookups |
||
| 151 | if ($type === 'Lookup') { |
||
| 152 | $generateId = false; |
||
| 153 | |||
| 154 | if ($customfield) { |
||
| 155 | $name .= '_ID'; |
||
| 156 | $generateId = true; |
||
| 157 | } elseif ($name === $moduleSingular.' Owner') { |
||
| 158 | // Check if this is a "owner" field. |
||
| 159 | $name = 'SMOWNERID'; |
||
| 160 | $generateId = true; |
||
| 161 | } else { |
||
| 162 | $mapping = [ |
||
| 163 | 'Account Name' => 'ACCOUNTID', |
||
| 164 | 'Contact Name' => 'CONTACTID', |
||
| 165 | 'Parent Account' => 'PARENTACCOUNTID', |
||
| 166 | 'Campaign Source' => 'CAMPAIGNID', |
||
| 167 | ]; |
||
| 168 | if (isset($mapping[$name])) { |
||
| 169 | $name = $mapping[$name]; |
||
| 170 | $generateId = true; |
||
| 171 | } else { |
||
| 172 | $this->logger->warning('Unable to set a ID for the field {name} of the {module} module', [ |
||
| 173 | 'name' => $name, |
||
| 174 | 'module' => $moduleName, |
||
| 175 | ]); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | if ($generateId) { |
||
| 180 | $req = false; |
||
| 181 | $type = 'Lookup ID'; |
||
| 182 | $isreadonly = true; |
||
| 183 | $maxlength = $field['maxlength']; |
||
| 184 | $label = $field['label']; |
||
| 185 | $dv = $field['dv']; |
||
| 186 | |||
| 187 | $field['phpType'] = $phpType; |
||
| 188 | |||
| 189 | self::registerProperty($class, ($customfield ? self::camelCase($name) : $name), 'Zoho field '.$name."\n". |
||
| 190 | 'Type: '.$type."\n". |
||
| 191 | 'Read only: '.($isreadonly ? 'true' : 'false')."\n". |
||
| 192 | 'Max length: '.$maxlength."\n". |
||
| 193 | 'Custom field: '.($customfield ? 'true' : 'false')."\n", 'string'); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | self::registerProperty($class, 'createdTime', "The time the record was created in Zoho\nType: DateTime\n", '\\DateTime'); |
||
| 200 | self::registerProperty($class, 'modifiedTime', "The last time the record was modified in Zoho\nType: DateTime\n", '\\DateTime'); |
||
| 201 | $method = PhpMethod::create('isDirty'); |
||
| 202 | $method->setDescription('Returns whether a property is changed or not.'); |
||
| 203 | $method->addParameter(PhpParameter::create('name')); |
||
| 204 | $method->setBody("\$propertyName = 'dirty'.ucfirst(\$name);\nreturn \$this->\$propertyName;"); |
||
| 205 | $method->setType('bool'); |
||
| 206 | $class->setMethod($method); |
||
| 207 | |||
| 208 | $generator = new CodeFileGenerator(); |
||
| 209 | $code = $generator->generate($class); |
||
| 210 | |||
| 211 | View Code Duplication | if (!file_put_contents(rtrim($targetDirectory, '/').'/'.$className.'.php', $code)) { |
|
| 212 | throw new ZohoCRMException("An error occurred while creating the class $className. Please verify the target directory or the rights of the file."); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns a unique identifier from the name. |
||
| 218 | * |
||
| 219 | * @param $name |
||
| 220 | * @param array $usedNames |
||
| 221 | */ |
||
| 222 | private function getUniqueIdentifier($name, array $usedIdentifiers) |
||
| 223 | { |
||
| 224 | $id = self::camelCase($name); |
||
| 225 | if (isset($usedIdentifiers[$id])) { |
||
| 226 | $counter = 2; |
||
| 227 | while (isset($usedIdentifiers[$id.'_'.$counter])) { |
||
| 228 | ++$counter; |
||
| 229 | } |
||
| 230 | |||
| 231 | return $id.'_'.$counter; |
||
| 232 | } else { |
||
| 233 | return $id; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | public function generateDao($fields, $namespace, $className, $daoClassName, $moduleName, $targetDirectory, $moduleSingular, $modulePlural) |
||
| 238 | { |
||
| 239 | // if (class_exists($namespace."\\".$className)) { |
||
| 240 | // $class = PhpClass::fromReflection(new \ReflectionClass($namespace."\\".$daoClassName)); |
||
| 241 | // } else { |
||
| 242 | $class = PhpClass::create(); |
||
| 243 | // } |
||
| 244 | |||
| 245 | $class->setName($daoClassName) |
||
| 246 | ->setNamespace($namespace) |
||
| 247 | ->setParentClassName('\\Wabel\\Zoho\\CRM\\AbstractZohoDao'); |
||
| 248 | |||
| 249 | $usedIdentifiers = []; |
||
| 250 | |||
| 251 | foreach ($fields as $key => $fieldCategory) { |
||
| 252 | foreach ($fieldCategory as $name => $field) { |
||
| 253 | $type = $field['type']; |
||
| 254 | |||
| 255 | View Code Duplication | switch ($type) { |
|
| 256 | case 'DateTime': |
||
| 257 | case 'Date': |
||
| 258 | $phpType = '\\DateTime'; |
||
| 259 | break; |
||
| 260 | case 'Boolean': |
||
| 261 | $phpType = 'bool'; |
||
| 262 | break; |
||
| 263 | case 'Integer': |
||
| 264 | $phpType = 'int'; |
||
| 265 | break; |
||
| 266 | default: |
||
| 267 | $phpType = 'string'; |
||
| 268 | break; |
||
| 269 | } |
||
| 270 | |||
| 271 | $fields[$key][$name]['phpType'] = $phpType; |
||
| 272 | $identifier = $this->getUniqueIdentifier($name, $usedIdentifiers); |
||
| 273 | $usedIdentifiers[$identifier] = true; |
||
| 274 | $fields[$key][$name]['getter'] = 'get'.ucfirst($identifier); |
||
| 275 | $fields[$key][$name]['setter'] = 'set'.ucfirst($identifier); |
||
| 276 | $fields[$key][$name]['name'] = $identifier; |
||
| 277 | |||
| 278 | |||
| 279 | //Detect Special Fields from Zoho - Using API, you cannot create or update these system-generated fields: |
||
| 280 | //@Todo: Manage these fields. The problem comes "MODIFIEDBY" and "Modified By" fields |
||
| 281 | $specialOwnerFieldsMapping = [ |
||
| 282 | 'Created By' => 'SMCREATORID', |
||
| 283 | 'Modified By' =>'MODIFIEDBY', |
||
| 284 | ]; |
||
| 285 | $specialDateFieldsMapping = [ |
||
| 286 | 'Created Time' => 'createdTime', |
||
| 287 | 'Modified Time' =>'modifiedTime' |
||
| 288 | ]; |
||
| 289 | $linkedSystemGeneratedFields = [ |
||
| 290 | 'Created By' => 'Created Time', |
||
| 291 | 'Modified By' =>'Modified Time', |
||
| 292 | ]; |
||
| 293 | $systemGenerated = false; |
||
| 294 | if(array_key_exists($field['label'],$specialOwnerFieldsMapping)){ |
||
| 295 | $systemGenerated = true; |
||
| 296 | } |
||
| 297 | if($systemGenerated && isset($linkedSystemGeneratedFields[$field['label']])){ |
||
| 298 | $dateSpecialField = false; |
||
| 299 | if (isset($specialDateFieldsMapping[$linkedSystemGeneratedFields[$field['label']]])) { |
||
| 300 | $name = $specialDateFieldsMapping[$linkedSystemGeneratedFields[$field['label']]]; |
||
| 301 | $generateId = true; |
||
| 302 | $dateSpecialField = true; |
||
| 303 | } |
||
| 304 | View Code Duplication | if ($generateId && $dateSpecialField) { |
|
| 305 | $fields[$key][$name]['req'] = false; |
||
| 306 | $fields[$key][$name]['type'] = 'Date'; |
||
| 307 | $fields[$key][$name]['isreadonly'] = false; |
||
| 308 | $fields[$key][$name]['maxlength'] = 20; |
||
| 309 | $fields[$key][$name]['label'] = $name; |
||
| 310 | $fields[$key][$name]['dv'] = $name; |
||
| 311 | $fields[$key][$name]['customfield'] = true; |
||
| 312 | $fields[$key][$name]['phpType'] = '\\DateTime'; |
||
| 313 | $fields[$key][$name]['getter'] = 'get'.ucfirst(self::camelCase($name)); |
||
| 314 | $fields[$key][$name]['setter'] = 'set'.ucfirst(self::camelCase($name)); |
||
| 315 | $fields[$key][$name]['name'] = self::camelCase($name); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | if ($type === 'Lookup') { |
||
| 319 | $generateId = false; |
||
| 320 | |||
| 321 | if ($field['customfield']) { |
||
| 322 | $name .= '_ID'; |
||
| 323 | $generateId = true; |
||
| 324 | } elseif ($field['label'] === $moduleSingular.' Owner') { |
||
| 325 | // Check if this is a "owner" field. |
||
| 326 | $name = 'SMOWNERID'; |
||
| 327 | $generateId = true; |
||
| 328 | } else { |
||
| 329 | $mapping = [ |
||
| 330 | 'Account Name' => 'ACCOUNTID', |
||
| 331 | 'Contact Name' => 'CONTACTID', |
||
| 332 | 'Parent Account' => 'PARENTACCOUNTID', |
||
| 333 | 'Campaign Source' => 'CAMPAIGNID', |
||
| 334 | ]; |
||
| 335 | if (isset($mapping[$field['label']])) { |
||
| 336 | $name = $mapping[$field['label']]; |
||
| 337 | $generateId = true; |
||
| 338 | } |
||
| 339 | } |
||
| 340 | View Code Duplication | if ($generateId) { |
|
| 341 | $fields[$key][$name]['req'] = false; |
||
| 342 | $fields[$key][$name]['type'] = 'Lookup ID'; |
||
| 343 | $fields[$key][$name]['isreadonly'] = true; |
||
| 344 | $fields[$key][$name]['maxlength'] = 100; |
||
| 345 | $fields[$key][$name]['label'] = $name; |
||
| 346 | $fields[$key][$name]['dv'] = $name; |
||
| 347 | $fields[$key][$name]['customfield'] = true; |
||
| 348 | $fields[$key][$name]['phpType'] = $phpType; |
||
| 349 | $fields[$key][$name]['getter'] = 'get'.ucfirst(self::camelCase($name)); |
||
| 350 | $fields[$key][$name]['setter'] = 'set'.ucfirst(self::camelCase($name)); |
||
| 351 | $fields[$key][$name]['name'] = self::camelCase($name); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | $class->setMethod(PhpMethod::create('getModule')->setBody('return '.var_export($moduleName, true).';')); |
||
| 358 | |||
| 359 | $class->setMethod(PhpMethod::create('getSingularModuleName')->setBody('return '.var_export($moduleSingular, true).';')); |
||
| 360 | |||
| 361 | $class->setMethod(PhpMethod::create('getPluralModuleName')->setBody('return '.var_export($modulePlural, true).';')); |
||
| 362 | |||
| 363 | $class->setMethod(PhpMethod::create('getFields')->setBody('return '.var_export($fields, true).';')); |
||
| 364 | |||
| 365 | $class->setMethod(PhpMethod::create('getBeanClassName')->setBody('return '.var_export($namespace.'\\'.$className, true).';')); |
||
| 366 | |||
| 367 | $generator = new CodeFileGenerator(); |
||
| 368 | $code = $generator->generate($class); |
||
| 369 | |||
| 370 | View Code Duplication | if (!file_put_contents(rtrim($targetDirectory, '/').'/'.$daoClassName.'.php', $code)) { |
|
| 371 | throw new ZohoCRMException("An error occurred while creating the DAO $daoClassName. Please verify the target directory exists or the rights of the file."); |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | private static function camelCase($str, array $noStrip = []) |
||
| 382 | |||
| 383 | private static function upperCamelCase($str, array $noStrip = []) |
||
| 394 | |||
| 395 | private static function registerProperty(PhpClass $class, $name, $description, $type, $nullable = false) |
||
| 443 | } |
||
| 444 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.