| Conditions | 20 |
| Paths | 756 |
| Total Lines | 176 |
| Code Lines | 121 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 153 | public function generateClass(ClassDefinition $classDef): PhpFile |
||
| 154 | { |
||
| 155 | $phpFile = new PhpFile(); |
||
| 156 | $phpFile->addComment('This phpFile is auto-generated.'); |
||
| 157 | $phpFile->setStrictTypes(); // adds declare(strict_types=1) |
||
| 158 | |||
| 159 | $phpNamespace = $phpFile->addNamespace($this->baseNamespace); |
||
| 160 | |||
| 161 | $class = $phpNamespace->addClass($classDef->className); |
||
| 162 | |||
| 163 | $parentClass = $classDef->parentClass; |
||
| 164 | if ('Object' === $parentClass) { |
||
| 165 | $parentClass = static::OBJECT_CLASS; |
||
| 166 | } elseif ('Function' === $parentClass) { |
||
| 167 | $parentClass = static::FUNCTION_CLASS; |
||
| 168 | } |
||
| 169 | |||
| 170 | $class->addExtend($parentClass) |
||
| 171 | ->addComment($classDef->classDocs); |
||
| 172 | |||
| 173 | $class->addConstant('TYPE_NAME', $classDef->typeName) |
||
| 174 | ->setPublic(); |
||
| 175 | |||
| 176 | $constructor = $class->addMethod('__construct') |
||
| 177 | ->setPublic(); |
||
| 178 | |||
| 179 | if (!in_array($parentClass, [static::OBJECT_CLASS, static::FUNCTION_CLASS])) { |
||
| 180 | $constructor->addBody('parent::__construct();') |
||
| 181 | ->addBody(''); |
||
| 182 | } |
||
| 183 | |||
| 184 | $fromArray = $class->addMethod('fromArray') |
||
| 185 | ->setPublic() |
||
| 186 | ->setStatic() |
||
| 187 | ->setReturnType($classDef->className); |
||
| 188 | |||
| 189 | $serialize = $class->addMethod('typeSerialize') |
||
| 190 | ->setReturnType('array'); |
||
| 191 | |||
| 192 | $fromArray->addParameter('array') |
||
| 193 | ->setType('array'); |
||
| 194 | |||
| 195 | if (count($classDef->fields) > 0) { |
||
| 196 | $fromArray->addBody('return new static('); |
||
| 197 | |||
| 198 | $serialize->addBody('return ['); |
||
| 199 | $serialize->addBody(' \'@type\' => static::TYPE_NAME,'); |
||
| 200 | } else { |
||
| 201 | $fromArray->addBody('return new static();'); |
||
| 202 | $serialize->addBody('return [\'@type\' => static::TYPE_NAME];'); |
||
| 203 | } |
||
| 204 | |||
| 205 | foreach ($classDef->fields as $fieldDef) { |
||
| 206 | $typeStyle = $fieldDef->type; |
||
| 207 | $type = $fieldDef->type; |
||
| 208 | |||
| 209 | $arrayNestLevels = substr_count($type, '[]'); |
||
| 210 | if (1 === $arrayNestLevels) { |
||
| 211 | $type = 'array'; |
||
| 212 | $typeStyle = 'array'; |
||
| 213 | } elseif (2 === $arrayNestLevels) { |
||
| 214 | $type = 'array'; |
||
| 215 | $typeStyle = 'array_array'; |
||
| 216 | } elseif ($arrayNestLevels > 2) { |
||
| 217 | throw new InvalidArgumentException('Vector of higher than 2 lvl deep'); |
||
| 218 | } |
||
| 219 | |||
| 220 | $class->addProperty($fieldDef->name) |
||
| 221 | ->setProtected() |
||
| 222 | ->setNullable($fieldDef->mayBeNull) |
||
| 223 | ->setType($type) |
||
| 224 | ->addComment($fieldDef->doc) |
||
| 225 | ->addComment('') |
||
| 226 | ->addComment('@var ' . $fieldDef->type . ($fieldDef->mayBeNull ? '|null' : '')); |
||
| 227 | |||
| 228 | $constructor->addParameter($fieldDef->name) |
||
| 229 | ->setType($type) |
||
| 230 | ->setNullable($fieldDef->mayBeNull); |
||
| 231 | |||
| 232 | $constructor->addBody('$this->' . $fieldDef->name . ' = $' . $fieldDef->name . ';'); |
||
| 233 | |||
| 234 | [$rawType] = explode('[', $fieldDef->type); |
||
| 235 | |||
| 236 | switch ($rawType) { |
||
| 237 | case 'string': |
||
| 238 | case 'int': |
||
| 239 | case 'bool': |
||
| 240 | case 'float': |
||
| 241 | $fromArray->addBody(' $array[\'' . $fieldDef->rawName . '\'],'); |
||
| 242 | $serialize->addBody(' \'' . $fieldDef->rawName . '\' => $this->' . $fieldDef->name . ','); |
||
| 243 | break; |
||
| 244 | |||
| 245 | default: |
||
| 246 | if ($fieldDef->mayBeNull) { |
||
| 247 | if ('array' === $typeStyle) { |
||
| 248 | $fromArray->addBody( |
||
| 249 | ' (isset($array[\'' . $fieldDef->name . |
||
| 250 | '\']) ? array_map(fn($x) => ' . 'TdSchemaRegistry::fromArray($x), $array[\'' . |
||
| 251 | $fieldDef->name . '\']) : null),' |
||
| 252 | ); |
||
| 253 | |||
| 254 | $serialize->addBody( |
||
| 255 | ' (isset($this->' . $fieldDef->name . |
||
| 256 | ') ? array_map(fn($x) => $x->typeSerialize(), $this->' . $fieldDef->name . ') : null),' |
||
| 257 | ); |
||
| 258 | } elseif ('array_array' === $typeStyle) { |
||
| 259 | $fromArray->addBody( |
||
| 260 | ' (isset($array[\'' . $fieldDef->name . |
||
| 261 | '\']) ? array_map(fn($x) => ' . |
||
| 262 | 'array_map(fn($y) => TdSchemaRegistry::fromArray($y), $x), $array[\'' . |
||
| 263 | $fieldDef->name . '\']) : null),' |
||
| 264 | ); |
||
| 265 | |||
| 266 | $serialize->addBody( |
||
| 267 | ' (isset($this->' . $fieldDef->name . |
||
| 268 | ') ? array_map(fn($x) => array_map(fn($y) => $y->typeSerialize(), $x), $this->' . |
||
| 269 | $fieldDef->name . ') : null),' |
||
| 270 | ); |
||
| 271 | } else { |
||
| 272 | $fromArray->addBody( |
||
| 273 | ' (isset($array[\'' . $fieldDef->rawName . '\']) ? ' . |
||
| 274 | 'TdSchemaRegistry::fromArray($array[\'' . $fieldDef->rawName . '\']) : null),' |
||
| 275 | ); |
||
| 276 | |||
| 277 | $serialize->addBody( |
||
| 278 | ' \'' . $fieldDef->rawName . '\' => (isset($this->' . |
||
| 279 | $fieldDef->name . ') ? $this->' . $fieldDef->name . ' : null),' |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | } else { |
||
| 283 | if ('array' === $typeStyle) { |
||
| 284 | $fromArray->addBody( |
||
| 285 | ' array_map(fn($x) => TdSchemaRegistry::fromArray($x), $array[\'' . |
||
| 286 | $fieldDef->name . '\']),' |
||
| 287 | ); |
||
| 288 | |||
| 289 | $serialize->addBody( |
||
| 290 | ' array_map(fn($x) => $x->typeSerialize(), $this->' . $fieldDef->name . '),' |
||
| 291 | ); |
||
| 292 | } elseif ('array_array' === $typeStyle) { |
||
| 293 | $fromArray->addBody( |
||
| 294 | ' array_map(fn($x) => array_map(fn($y) => TdSchemaRegistry::fromArray($y), $x)' . |
||
| 295 | ', $array[\'' . $fieldDef->name . '\']),' |
||
| 296 | ); |
||
| 297 | |||
| 298 | $serialize->addBody( |
||
| 299 | ' array_map(fn($x) => array_map(fn($y) => $y->typeSerialize(), $x), $this->' . |
||
| 300 | $fieldDef->name . '),' |
||
| 301 | ); |
||
| 302 | } else { |
||
| 303 | $fromArray->addBody( |
||
| 304 | ' ' . 'TdSchemaRegistry::fromArray($array[\'' . $fieldDef->rawName . '\']),' |
||
| 305 | ); |
||
| 306 | |||
| 307 | $serialize->addBody( |
||
| 308 | ' \'' . $fieldDef->rawName . '\' => $this->' . $fieldDef->name . '->typeSerialize(),' |
||
| 309 | ); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | $getter = $class->addMethod('get' . ucfirst($fieldDef->name)) |
||
| 315 | ->setPublic() |
||
| 316 | ->setReturnType($type) |
||
| 317 | ->setReturnNullable($fieldDef->mayBeNull); |
||
| 318 | |||
| 319 | $getter->addBody('return $this->' . $fieldDef->name . ';'); |
||
| 320 | } |
||
| 321 | |||
| 322 | if (count($classDef->fields) > 0) { |
||
| 323 | $fromArray->addBody(');'); |
||
| 324 | |||
| 325 | $serialize->addBody('];'); |
||
| 326 | } |
||
| 327 | |||
| 328 | return $phpFile; |
||
| 329 | } |
||
| 404 |