Conditions | 18 |
Paths | 94 |
Total Lines | 122 |
Code Lines | 85 |
Lines | 18 |
Ratio | 14.75 % |
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:
1 | <?php |
||
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 | |||
121 | View Code Duplication | switch ($type) { |
|
122 | case 'DateTime': |
||
123 | case 'Date': |
||
124 | $phpType = '\\DateTimeImmutable'; |
||
125 | break; |
||
126 | case 'Boolean': |
||
127 | $phpType = 'bool'; |
||
128 | break; |
||
129 | case 'Integer': |
||
130 | $phpType = 'int'; |
||
131 | break; |
||
132 | default: |
||
133 | $phpType = 'string'; |
||
134 | break; |
||
135 | } |
||
136 | |||
137 | $field['phpType'] = $phpType; |
||
138 | |||
139 | $identifier = $this->getUniqueIdentifier($name, $usedIdentifiers); |
||
140 | $usedIdentifiers[$identifier] = true; |
||
141 | |||
142 | self::registerProperty($class, $identifier, 'Zoho field '.$name."\n". |
||
143 | 'Type: '.$type."\n". |
||
144 | 'Read only: '.($isreadonly ? 'true' : 'false')."\n". |
||
145 | 'Max length: '.$maxlength."\n". |
||
146 | 'Custom field: '.($customfield ? 'true' : 'false')."\n", $phpType); |
||
147 | |||
148 | // Adds a ID field for lookups |
||
149 | if ($type === 'Lookup') { |
||
150 | $generateId = false; |
||
151 | |||
152 | if ($customfield) { |
||
153 | $name .= '_ID'; |
||
154 | $generateId = true; |
||
155 | } elseif ($name === $moduleSingular.' Owner') { |
||
156 | // Check if this is a "owner" field. |
||
157 | $name = 'SMOWNERID'; |
||
158 | $generateId = true; |
||
159 | } else { |
||
160 | $mapping = [ |
||
161 | 'Account Name' => 'ACCOUNTID', |
||
162 | 'Contact Name' => 'CONTACTID', |
||
163 | 'Parent Account' => 'PARENTACCOUNTID', |
||
164 | 'Campaign Source' => 'CAMPAIGNID', |
||
165 | ]; |
||
166 | if (isset($mapping[$name])) { |
||
167 | $name = $mapping[$name]; |
||
168 | $generateId = true; |
||
169 | } else { |
||
170 | $this->logger->warning('Unable to set a ID for the field {name} of the {module} module', [ |
||
171 | 'name' => $name, |
||
172 | 'module' => $moduleName, |
||
173 | ]); |
||
174 | } |
||
175 | } |
||
176 | |||
177 | if ($generateId) { |
||
178 | $req = false; |
||
179 | $type = 'Lookup ID'; |
||
180 | $isreadonly = true; |
||
181 | $maxlength = $field['maxlength']; |
||
182 | $label = $field['label']; |
||
183 | $dv = $field['dv']; |
||
184 | |||
185 | $field['phpType'] = $phpType; |
||
186 | |||
187 | self::registerProperty($class, ($customfield ? self::camelCase($name) : $name), 'Zoho field '.$name."\n". |
||
188 | 'Type: '.$type."\n". |
||
189 | 'Read only: '.($isreadonly ? 'true' : 'false')."\n". |
||
190 | 'Max length: '.$maxlength."\n". |
||
191 | 'Custom field: '.($customfield ? 'true' : 'false')."\n", 'string'); |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | self::registerProperty($class, 'createdTime', "The time the record was created in Zoho\nType: DateTime\n", '\\DateTime'); |
||
198 | self::registerProperty($class, 'modifiedTime', "The last time the record was modified in Zoho\nType: DateTime\n", '\\DateTime'); |
||
199 | |||
200 | $method = PhpMethod::create('isDirty'); |
||
201 | $method->setDescription('Returns whether a property is changed or not.'); |
||
202 | $method->addParameter(PhpParameter::create('name')); |
||
203 | $method->setBody("\$propertyName = 'dirty'.ucfirst(\$name);\nreturn \$this->\$propertyName;"); |
||
204 | $method->setType('bool'); |
||
205 | $class->setMethod($method); |
||
206 | |||
207 | $generator = new CodeFileGenerator(); |
||
208 | $code = $generator->generate($class); |
||
209 | |||
210 | View Code Duplication | if (!file_put_contents(rtrim($targetDirectory, '/').'/'.$className.'.php', $code)) { |
|
211 | throw new ZohoCRMException("An error occurred while creating the class $className. Please verify the target directory or the rights of the file."); |
||
212 | } |
||
213 | } |
||
214 | |||
399 |
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.