| Conditions | 2 |
| Paths | 1 |
| Total Lines | 68 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | 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 |
||
| 37 | public function convertDefinitionToModel($class, $attributes) |
||
| 38 | { |
||
| 39 | $template = <<<EOF |
||
| 40 | <?php |
||
| 41 | |||
| 42 | namespace {{ Namespace }}; |
||
| 43 | |||
| 44 | use Carbon\Carbon; |
||
| 45 | use Spinen\ConnectWise\Support\Model; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Class {{ Class }} Version {{ Version }} |
||
| 49 | * |
||
| 50 | * {{ Description }} |
||
| 51 | * |
||
| 52 | {{ Properties }} |
||
| 53 | */ |
||
| 54 | class {{ Class }} extends Model |
||
| 55 | { |
||
| 56 | /** |
||
| 57 | * Properties that need to be casts to a specific object or type |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected \$casts = [ |
||
| 62 | {{ Casts }} |
||
| 63 | ]; |
||
| 64 | } |
||
| 65 | |||
| 66 | EOF; |
||
| 67 | |||
| 68 | // Make array of types keyed by property |
||
| 69 | $property_type = collect($attributes['properties']) |
||
| 70 | ->map(function ($attributes) { |
||
| 71 | return $this->parseType($attributes); |
||
| 72 | }); |
||
| 73 | |||
| 74 | $casts = $property_type->map(function ($type, $property) { |
||
| 75 | $primitives = [ |
||
| 76 | 'array', |
||
| 77 | 'boolean', |
||
| 78 | 'float', |
||
| 79 | 'integer', |
||
| 80 | 'object', |
||
| 81 | 'string', |
||
| 82 | ]; |
||
| 83 | |||
| 84 | return " '${property}' => " . ((in_array($type, $primitives)) ? "'${type}'" : "${type}::class"); |
||
| 85 | }) |
||
| 86 | ->values() |
||
| 87 | ->sort() |
||
| 88 | ->implode(",\n"); |
||
| 89 | |||
| 90 | $properties = $property_type->map(function ($type, $property) { |
||
| 91 | return " * @property ${type} $${property}"; |
||
| 92 | }) |
||
| 93 | ->values() |
||
| 94 | ->sort() |
||
| 95 | ->implode("\n"); |
||
| 96 | |||
| 97 | $model = preg_replace('|{{ Namespace }}|u', $this->getNamespace(), $template); |
||
| 98 | $model = preg_replace('|{{ Version }}|u', $this->version, $model); |
||
| 99 | $model = preg_replace('|{{ Class }}|u', $class, $model); |
||
| 100 | $model = preg_replace('|{{ Description }}|u', $attributes['description'] ?? 'Model for ' . $class, $model); |
||
| 101 | $model = preg_replace('|{{ Casts }}|u', $casts, $model); |
||
| 102 | $model = preg_replace('|{{ Properties }}|u', $properties, $model); |
||
| 103 | |||
| 104 | return $model; |
||
| 105 | } |
||
| 265 |