We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 18 |
| Paths | 182 |
| Total Lines | 85 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 2 | 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 |
||
| 80 | private function getRelationDetailsFromInput($input, $crudFields = [], $relationMethod = false) |
||
| 81 | { |
||
| 82 | // main entity |
||
| 83 | if (empty($crudFields)) { |
||
| 84 | $relationFields = $this->getRelationFields(); |
||
| 85 | } else { |
||
| 86 | // relations sends the fields that represent them so we can parse the input accordingly. |
||
| 87 | $relationFields = $crudFields; |
||
| 88 | |||
| 89 | foreach ($crudFields as $key => $crudField) { |
||
| 90 | if (isset($crudField['subfields'])) { |
||
| 91 | foreach ($crudField['subfields'] as $crudSubField) { |
||
| 92 | if (isset($crudSubField['relation_type'])) { |
||
| 93 | $relationFields[] = $crudSubField; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | //remove fields that are not in the submitted form input |
||
| 101 | $relationFields = array_filter($relationFields, function ($field) use ($input) { |
||
| 102 | return Arr::has($input, $field['name']) || isset($input[$field['name']]) || Arr::has($input, Str::afterLast($field['name'], '.')); |
||
| 103 | }); |
||
| 104 | |||
| 105 | $relationDetails = []; |
||
| 106 | |||
| 107 | foreach ($relationFields as $field) { |
||
| 108 | // if relationMethod is set we strip it out of the fieldName that we use to create the relations array |
||
| 109 | $fieldName = $relationMethod ? Str::after($field['name'], $relationMethod.'.') : $field['name']; |
||
| 110 | |||
| 111 | $key = Str::before($this->getOnlyRelationEntity(['entity' => $fieldName]), '.'); |
||
| 112 | |||
| 113 | // if the field entity contains the attribute we want to add that attribute in the correct relation key. |
||
| 114 | // eg: address.street, we want to add `street` as an attribute in `address` relation, `street` is not |
||
| 115 | // a relation of `address` |
||
| 116 | if ($this->getOnlyRelationEntity($field) !== $field['entity']) { |
||
| 117 | if (Str::before($field['entity'], '.') === $relationMethod) { |
||
| 118 | $key = Str::before($this->getOnlyRelationEntity($field), '.'); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | $attributeName = (string) Str::of($field['name'])->afterLast('.'); |
||
| 123 | |||
| 124 | switch ($field['relation_type']) { |
||
| 125 | case 'BelongsTo': |
||
| 126 | // when it's a nested belongsTo relation we want to make sure |
||
| 127 | // the key used to store the values is the main relation key |
||
| 128 | $key = Str::beforeLast($this->getOnlyRelationEntity($field), '.'); |
||
| 129 | |||
| 130 | if (! isset($field['parentFieldName']) && isset($field['entity'])) { |
||
| 131 | $mainField = $field; |
||
| 132 | $mainField['entity'] = Str::beforeLast($field['entity'], '.'); |
||
| 133 | |||
| 134 | $inferredRelation = $this->inferRelationTypeFromRelationship($mainField); |
||
| 135 | } |
||
| 136 | |||
| 137 | break; |
||
| 138 | } |
||
| 139 | |||
| 140 | // we don't need to re-setup this relation method values, we just want the relations |
||
| 141 | if ($key === $relationMethod) { |
||
| 142 | unset($inferredRelation); |
||
| 143 | continue; |
||
| 144 | } |
||
| 145 | |||
| 146 | $fieldDetails = Arr::get($relationDetails, $key, []); |
||
| 147 | $fieldDetails['values'][$attributeName] = Arr::get($input, $fieldName); |
||
| 148 | $fieldDetails['model'] = $fieldDetails['model'] ?? $field['model']; |
||
| 149 | $fieldDetails['relation_type'] = $fieldDetails['relation_type'] ?? $inferredRelation ?? $field['relation_type']; |
||
| 150 | $fieldDetails['crudFields'][] = $field; |
||
| 151 | $fieldDetails['entity'] = $this->getOnlyRelationEntity($field); |
||
| 152 | |||
| 153 | if (isset($field['fallback_id'])) { |
||
| 154 | $fieldDetails['fallback_id'] = $field['fallback_id']; |
||
| 155 | } |
||
| 156 | if (isset($field['force_delete'])) { |
||
| 157 | $fieldDetails['force_delete'] = $field['force_delete']; |
||
| 158 | } |
||
| 159 | |||
| 160 | Arr::set($relationDetails, $key, $fieldDetails); |
||
| 161 | unset($inferredRelation); |
||
| 162 | } |
||
| 163 | |||
| 164 | return $relationDetails; |
||
| 165 | } |
||
| 243 |