| Conditions | 18 |
| Paths | 94 |
| Total Lines | 114 |
| Code Lines | 80 |
| Lines | 18 |
| Ratio | 15.79 % |
| Changes | 11 | ||
| Bugs | 1 | Features | 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:
| 1 | <?php |
||
| 69 | public function generateBean($fields, $namespace, $className, $moduleName, $targetDirectory) |
||
| 70 | { |
||
| 71 | |||
| 72 | // if (class_exists($namespace."\\".$className)) { |
||
| 73 | // $class = PhpClass::fromReflection(new \ReflectionClass($namespace."\\".$className)); |
||
| 74 | // } else { |
||
| 75 | $class = PhpClass::create(); |
||
| 76 | // } |
||
| 77 | |||
| 78 | $class->setName($className) |
||
| 79 | ->setNamespace($namespace) |
||
| 80 | ->addInterface('\\Wabel\\Zoho\\CRM\\ZohoBeanInterface') |
||
| 81 | ->setMethod(PhpMethod::create('__construct')); |
||
| 82 | |||
| 83 | // Let's add the ZohoID property |
||
| 84 | self::registerProperty($class, 'zohoId', "The ID of this record in Zoho\nType: string\n", 'string'); |
||
| 85 | |||
| 86 | foreach ($fields as &$fieldCategory) { |
||
| 87 | foreach ($fieldCategory as $name => &$field) { |
||
| 88 | $req = $field['req']; |
||
| 89 | $type = $field['type']; |
||
| 90 | $isreadonly = $field['isreadonly']; |
||
| 91 | $maxlength = $field['maxlength']; |
||
| 92 | $label = $field['label']; |
||
| 93 | $dv = $field['dv']; |
||
| 94 | $customfield = $field['customfield']; |
||
| 95 | |||
| 96 | View Code Duplication | switch ($type) { |
|
| 97 | case 'DateTime': |
||
| 98 | case 'Date': |
||
| 99 | $phpType = '\\DateTime'; |
||
| 100 | break; |
||
| 101 | case 'Boolean': |
||
| 102 | $phpType = 'bool'; |
||
| 103 | break; |
||
| 104 | case 'Integer': |
||
| 105 | $phpType = 'int'; |
||
| 106 | break; |
||
| 107 | default: |
||
| 108 | $phpType = 'string'; |
||
| 109 | break; |
||
| 110 | } |
||
| 111 | |||
| 112 | $field['phpType'] = $phpType; |
||
| 113 | |||
| 114 | self::registerProperty($class, self::camelCase($name), 'Zoho field '.$name."\n". |
||
| 115 | 'Type: '.$type."\n". |
||
| 116 | 'Read only: '.($isreadonly ? 'true' : 'false')."\n". |
||
| 117 | 'Max length: '.$maxlength."\n". |
||
| 118 | 'Custom field: '.($customfield ? 'true' : 'false')."\n", $phpType); |
||
| 119 | |||
| 120 | // Adds a ID field for lookups |
||
| 121 | if ($type === 'Lookup') { |
||
| 122 | $generateId = false; |
||
| 123 | |||
| 124 | if ($customfield) { |
||
| 125 | $name .= '_ID'; |
||
| 126 | $generateId = true; |
||
| 127 | } else { |
||
| 128 | switch ($name) { |
||
| 129 | //TODO : To be completed with known lookup fields that are not custom fields but default in Zoho |
||
| 130 | case 'Account Name' : |
||
| 131 | $name = 'ACCOUNTID'; |
||
| 132 | $generateId = true; |
||
| 133 | break; |
||
| 134 | case 'Contact Name' : |
||
| 135 | $name = 'CONTACTID'; |
||
| 136 | $generateId = true; |
||
| 137 | break; |
||
| 138 | default : |
||
| 139 | $this->logger->warning('Unable to set a ID for the field {name} of the {module} module', [ |
||
| 140 | 'name' => $name, |
||
| 141 | 'module' => $moduleName, |
||
| 142 | ]); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($generateId) { |
||
| 147 | $req = false; |
||
| 148 | $type = 'Lookup ID'; |
||
| 149 | $isreadonly = true; |
||
| 150 | $maxlength = $field['maxlength']; |
||
| 151 | $label = $field['label']; |
||
| 152 | $dv = $field['dv']; |
||
| 153 | |||
| 154 | $field['phpType'] = $phpType; |
||
| 155 | |||
| 156 | self::registerProperty($class, ($customfield ? self::camelCase($name) : $name), 'Zoho field '.$name."\n". |
||
| 157 | 'Type: '.$type."\n". |
||
| 158 | 'Read only: '.($isreadonly ? 'true' : 'false')."\n". |
||
| 159 | 'Max length: '.$maxlength."\n". |
||
| 160 | 'Custom field: '.($customfield ? 'true' : 'false')."\n", 'string'); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | self::registerProperty($class, 'createdTime', "The time the record was created in Zoho\nType: DateTime\n", '\\DateTime'); |
||
| 167 | self::registerProperty($class, 'modifiedTime', "The last time the record was modified in Zoho\nType: DateTime\n", '\\DateTime'); |
||
| 168 | |||
| 169 | $method = PhpMethod::create('isDirty'); |
||
| 170 | $method->setDescription('Returns whether a property is changed or not.'); |
||
| 171 | $method->addParameter(PhpParameter::create('name')); |
||
| 172 | $method->setBody("\$propertyName = 'dirty'.ucfirst(\$name);\nreturn \$this->\$propertyName;"); |
||
| 173 | $method->setType('bool'); |
||
| 174 | $class->setMethod($method); |
||
| 175 | |||
| 176 | $generator = new CodeFileGenerator(); |
||
| 177 | $code = $generator->generate($class); |
||
| 178 | |||
| 179 | View Code Duplication | if (!file_put_contents(rtrim($targetDirectory, '/').'/'.$className.'.php', $code)) { |
|
| 180 | throw new ZohoCRMException("An error occurred while creating the class $className. Please verify the target directory or the rights of the file."); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 359 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.