| Conditions | 5 |
| Paths | 12 |
| Total Lines | 78 |
| Code Lines | 49 |
| 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 declare(strict_types=1); |
||
| 25 | public function getTemplateData(Model $m, array $elements): array |
||
| 26 | { |
||
| 27 | // get the props array with all js data |
||
| 28 | $props = $this->vueCode->props($m); |
||
| 29 | // get only props names |
||
| 30 | $propsNames = array_map( |
||
| 31 | function ($p) { |
||
| 32 | return $p->name; |
||
| 33 | }, |
||
| 34 | $props |
||
| 35 | ); |
||
| 36 | /** |
||
| 37 | * @var array $propsNames |
||
| 38 | */ |
||
| 39 | $propsNames = array_combine($propsNames, $propsNames); |
||
| 40 | // get the binding |
||
| 41 | $propsBind = array_map( |
||
| 42 | function (Prop $p) { |
||
| 43 | return $p->toBind(); |
||
| 44 | }, |
||
| 45 | $props |
||
| 46 | ); |
||
| 47 | |||
| 48 | // get data, and avoid anything that is already declared in props |
||
| 49 | $data = []; |
||
| 50 | foreach (array_merge($m->getDefault(), $m->getData(), $this->vueCode->extraData) as $k => $v) { |
||
| 51 | if (array_key_exists($k, $propsNames)) { |
||
| 52 | continue; |
||
| 53 | } |
||
| 54 | $data[$k] = $v; |
||
| 55 | } |
||
| 56 | // ensure it's a dict even if empty |
||
| 57 | if ($data === []) { |
||
| 58 | $jsonData = '{}'; |
||
| 59 | } else { |
||
| 60 | $jsonData = json_encode($data, JSON_PRETTY_PRINT); |
||
| 61 | } |
||
| 62 | |||
| 63 | $templateData = [ |
||
| 64 | 'jsonData' => $jsonData, |
||
| 65 | 'propsCode' => implode( |
||
| 66 | "\n", |
||
| 67 | array_map(function (Prop $p) { |
||
| 68 | return $p->toStruct(); |
||
| 69 | }, $props) |
||
| 70 | ), |
||
| 71 | 'propsBind' => implode(' ', $propsBind), |
||
| 72 | 'imports' => implode( |
||
| 73 | "\n", |
||
| 74 | array_map(function ($key, $value) { |
||
| 75 | return "import $key from \"$value\";"; |
||
| 76 | }, array_keys($this->vueCode->imports), $this->vueCode->imports) |
||
| 77 | ), |
||
| 78 | 'computedCode' => implode( |
||
| 79 | "\n", |
||
| 80 | array_map( |
||
| 81 | function (Computed $c) { |
||
| 82 | return $c->toStruct(); |
||
| 83 | }, |
||
| 84 | $this->vueCode->computed |
||
| 85 | ) |
||
| 86 | ), |
||
| 87 | 'otherData' => implode( |
||
| 88 | ",\n", |
||
| 89 | expandJS($this->vueCode->other) |
||
| 90 | ), |
||
| 91 | 'methodsCode' => implode( |
||
| 92 | "\n", |
||
| 93 | array_map(function ($key, $value) { |
||
| 94 | return "$key { $value },"; |
||
| 95 | }, array_keys($this->vueCode->methods), $this->vueCode->methods) |
||
| 96 | ), |
||
| 97 | ]; |
||
| 98 | if ($templateData['otherData']) { |
||
| 99 | $templateData['otherData'] .= ",\n"; |
||
| 100 | } |
||
| 101 | |||
| 102 | return $templateData; |
||
| 103 | } |
||
| 227 |