| Conditions | 21 |
| Paths | 904 |
| Total Lines | 138 |
| Lines | 50 |
| Ratio | 36.23 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | <?php |
||
| 238 | public function generateDao($fields, $namespace, $className, $daoClassName, $moduleName, $targetDirectory, $moduleSingular, $modulePlural) |
||
| 239 | { |
||
| 240 | // if (class_exists($namespace."\\".$className)) { |
||
| 241 | // $class = PhpClass::fromReflection(new \ReflectionClass($namespace."\\".$daoClassName)); |
||
| 242 | // } else { |
||
| 243 | $class = PhpClass::create(); |
||
| 244 | // } |
||
| 245 | |||
| 246 | $class->setName($daoClassName) |
||
| 247 | ->setNamespace($namespace) |
||
| 248 | ->setParentClassName('\\Wabel\\Zoho\\CRM\\AbstractZohoDao'); |
||
| 249 | |||
| 250 | $usedIdentifiers = []; |
||
| 251 | |||
| 252 | foreach ($fields as $key => $fieldCategory) { |
||
| 253 | foreach ($fieldCategory as $name => $field) { |
||
| 254 | $type = $field['type']; |
||
| 255 | |||
| 256 | View Code Duplication | switch ($type) { |
|
| 257 | case 'DateTime': |
||
| 258 | case 'Date': |
||
| 259 | $phpType = '\\DateTime'; |
||
| 260 | break; |
||
| 261 | case 'Boolean': |
||
| 262 | $phpType = 'bool'; |
||
| 263 | break; |
||
| 264 | case 'Integer': |
||
| 265 | $phpType = 'int'; |
||
| 266 | break; |
||
| 267 | default: |
||
| 268 | $phpType = 'string'; |
||
| 269 | break; |
||
| 270 | } |
||
| 271 | |||
| 272 | $fields[$key][$name]['phpType'] = $phpType; |
||
| 273 | $identifier = $this->getUniqueIdentifier($name, $usedIdentifiers); |
||
| 274 | $usedIdentifiers[$identifier] = true; |
||
| 275 | $fields[$key][$name]['getter'] = 'get'.ucfirst($identifier); |
||
| 276 | $fields[$key][$name]['setter'] = 'set'.ucfirst($identifier); |
||
| 277 | $fields[$key][$name]['name'] = $identifier; |
||
| 278 | |||
| 279 | |||
| 280 | //Detect Special Fields from Zoho - Using API, you cannot create or update these system-generated fields: |
||
| 281 | //@Todo: Manage these fields. The problem comes "MODIFIEDBY" and "Modified By" fields |
||
| 282 | $specialOwnerFieldsMapping = [ |
||
| 283 | 'Created By' => 'SMCREATORID', |
||
| 284 | 'Modified By' =>'MODIFIEDBY', |
||
| 285 | ]; |
||
| 286 | $specialDateFieldsMapping = [ |
||
| 287 | 'Created Time' => 'createdTime', |
||
| 288 | 'Modified Time' =>'modifiedTime' |
||
| 289 | ]; |
||
| 290 | $linkedSystemGeneratedFields = [ |
||
| 291 | 'Created By' => 'Created Time', |
||
| 292 | 'Modified By' =>'Modified Time', |
||
| 293 | ]; |
||
| 294 | $systemGenerated = false; |
||
| 295 | if(array_key_exists($field['label'],$specialOwnerFieldsMapping)){ |
||
| 296 | $systemGenerated = true; |
||
| 297 | } |
||
| 298 | if($systemGenerated && isset($linkedSystemGeneratedFields[$field['label']])){ |
||
| 299 | $dateSpecialField = false; |
||
| 300 | if (isset($specialDateFieldsMapping[$linkedSystemGeneratedFields[$field['label']]])) { |
||
| 301 | $name = $specialDateFieldsMapping[$linkedSystemGeneratedFields[$field['label']]]; |
||
| 302 | $generateId = true; |
||
| 303 | $dateSpecialField = true; |
||
| 304 | } |
||
| 305 | View Code Duplication | if ($generateId && $dateSpecialField) { |
|
| 306 | $fields[$key][$name]['req'] = false; |
||
| 307 | $fields[$key][$name]['type'] = 'DateTime'; |
||
| 308 | $fields[$key][$name]['isreadonly'] = false; |
||
| 309 | $fields[$key][$name]['maxlength'] = 20; |
||
| 310 | $fields[$key][$name]['label'] = $name; |
||
| 311 | $fields[$key][$name]['dv'] = $name; |
||
| 312 | $fields[$key][$name]['customfield'] = true; |
||
| 313 | $fields[$key][$name]['phpType'] = '\\DateTime'; |
||
| 314 | $fields[$key][$name]['getter'] = 'get'.ucfirst(self::camelCase($name)); |
||
| 315 | $fields[$key][$name]['setter'] = 'set'.ucfirst(self::camelCase($name)); |
||
| 316 | $fields[$key][$name]['name'] = self::camelCase($name); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | if ($type === 'Lookup') { |
||
| 320 | $generateId = false; |
||
| 321 | |||
| 322 | if ($field['customfield']) { |
||
| 323 | $name .= '_ID'; |
||
| 324 | $generateId = true; |
||
| 325 | View Code Duplication | } elseif ($field['label'] === $moduleSingular.' Owner' |
|
| 326 | || ($field['dv'] === $moduleSingular.' Owner' && $name === $moduleName. ' Owner')) { |
||
| 327 | // Check if this is a "owner" field. |
||
| 328 | $name = 'SMOWNERID'; |
||
| 329 | $generateId = true; |
||
| 330 | } else { |
||
| 331 | $mapping = [ |
||
| 332 | 'Account Name' => 'ACCOUNTID', |
||
| 333 | 'Contact Name' => 'CONTACTID', |
||
| 334 | 'Parent Account' => 'PARENTACCOUNTID', |
||
| 335 | 'Campaign Source' => 'CAMPAIGNID', |
||
| 336 | ]; |
||
| 337 | if (isset($mapping[$field['label']])) { |
||
| 338 | $name = $mapping[$field['label']]; |
||
| 339 | $generateId = true; |
||
| 340 | } |
||
| 341 | } |
||
| 342 | View Code Duplication | if ($generateId) { |
|
| 343 | $fields[$key][$name]['req'] = false; |
||
| 344 | $fields[$key][$name]['type'] = 'Lookup ID'; |
||
| 345 | $fields[$key][$name]['isreadonly'] = true; |
||
| 346 | $fields[$key][$name]['maxlength'] = 100; |
||
| 347 | $fields[$key][$name]['label'] = $name; |
||
| 348 | $fields[$key][$name]['dv'] = $name; |
||
| 349 | $fields[$key][$name]['customfield'] = true; |
||
| 350 | $fields[$key][$name]['phpType'] = $phpType; |
||
| 351 | $fields[$key][$name]['getter'] = 'get'.ucfirst(self::camelCase($name)); |
||
| 352 | $fields[$key][$name]['setter'] = 'set'.ucfirst(self::camelCase($name)); |
||
| 353 | $fields[$key][$name]['name'] = self::camelCase($name); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | } |
||
| 358 | |||
| 359 | $class->setMethod(PhpMethod::create('getModule')->setBody('return '.var_export($moduleName, true).';')); |
||
| 360 | |||
| 361 | $class->setMethod(PhpMethod::create('getSingularModuleName')->setBody('return '.var_export($moduleSingular, true).';')); |
||
| 362 | |||
| 363 | $class->setMethod(PhpMethod::create('getPluralModuleName')->setBody('return '.var_export($modulePlural, true).';')); |
||
| 364 | |||
| 365 | $class->setMethod(PhpMethod::create('getFields')->setBody('return '.var_export($fields, true).';')); |
||
| 366 | |||
| 367 | $class->setMethod(PhpMethod::create('getBeanClassName')->setBody('return '.var_export($namespace.'\\'.$className, true).';')); |
||
| 368 | |||
| 369 | $generator = new CodeFileGenerator(); |
||
| 370 | $code = $generator->generate($class); |
||
| 371 | |||
| 372 | View Code Duplication | if (!file_put_contents(rtrim($targetDirectory, '/').'/'.$daoClassName.'.php', $code)) { |
|
| 373 | throw new ZohoCRMException("An error occurred while creating the DAO $daoClassName. Please verify the target directory exists or the rights of the file."); |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 446 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.