| Conditions | 7 |
| Paths | 7 |
| Total Lines | 68 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 declare(strict_types=1); |
||
| 46 | public function editable($value, Field $field, HTMLNode $previous): HTMLNode |
||
| 47 | { |
||
| 48 | $previous = $this->_editable($value, $field, $previous); |
||
| 49 | |||
| 50 | /* |
||
| 51 | * init variables |
||
| 52 | */ |
||
| 53 | $inflector = InflectorFactory::create()->build(); |
||
| 54 | /** |
||
| 55 | * @var VueFramework $vue |
||
| 56 | */ |
||
| 57 | $vue = $this->framework; |
||
| 58 | $mvar = $vue->getFieldModelVariable(); |
||
| 59 | /** |
||
| 60 | * @var Datatype_relationship $datatype |
||
| 61 | */ |
||
| 62 | $datatype = $field->getDatatype(); |
||
| 63 | // @phpstan-ignore-next-line |
||
| 64 | $targetModel = call_user_func($datatype->getTargetClass() . '::getFormularium'); |
||
| 65 | if ($targetModel === false) { |
||
| 66 | throw new ClassNotFoundException("Cannot find model " . $datatype->getTarget()); |
||
| 67 | } |
||
| 68 | /** |
||
| 69 | * @var \Formularium\Model $targetModel |
||
| 70 | */ |
||
| 71 | |||
| 72 | // get the title field |
||
| 73 | $titleField = $targetModel->firstField( |
||
| 74 | function (Field $field) { |
||
| 75 | return $field->getRenderable('title', false); |
||
| 76 | } |
||
| 77 | ); |
||
| 78 | // import graphql query |
||
| 79 | $query = 'relationList' . $targetModel->getName() . 'Query'; |
||
| 80 | $targetStudly = Str::studly($datatype->getTarget()); |
||
| 81 | $vue->appendImport($query, "raw-loader!../" . $targetStudly . "/queryList.graphql"); |
||
| 82 | $vue->appendExtraData($query, $query); |
||
| 83 | |||
| 84 | $relationship = $datatype->getRelationship(); |
||
| 85 | if ($relationship === RelationshipFactory::RELATIONSHIP_MANY_TO_MANY || |
||
| 86 | $relationship === RelationshipFactory::MORPH_MANY_TO_MANY |
||
| 87 | // TODO: inverses 1:n? |
||
| 88 | ) { |
||
| 89 | $component = 'RelationshipMultiple'; |
||
| 90 | } elseif ($field->getRenderable('relationshipSelect', false)) { // TODO: document |
||
| 91 | $component = 'RelationshipSelect'; |
||
| 92 | } else { |
||
| 93 | $component = 'RelationshipAutocomplete'; |
||
| 94 | } |
||
| 95 | |||
| 96 | // replace the <select> with our component |
||
| 97 | foreach (array_merge($previous->get('select'), $previous->get('input')) as $input) { |
||
| 98 | $classes = $input->getAttribute('class'); |
||
| 99 | $input->setTag($component) |
||
| 100 | ->setAttributes( |
||
| 101 | [ |
||
| 102 | 'name' => $field->getName(), |
||
| 103 | 'htmlClass' => $classes, |
||
| 104 | 'titleField' => ($titleField ? $titleField->getName() : 'id'), |
||
| 105 | ':query' => $query, |
||
| 106 | 'targetType' => $datatype->getTarget(), |
||
| 107 | 'targetTypePlural' => $inflector->pluralize(mb_strtolower($datatype->getTarget())), |
||
| 108 | 'v-model' => $mvar . $field->getName() |
||
| 109 | ] |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | return $previous; |
||
| 114 | } |
||
| 116 |