We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Complex classes like Create often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Create, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | trait Create |
||
| 11 | { |
||
| 12 | /* |
||
| 13 | |-------------------------------------------------------------------------- |
||
| 14 | | CREATE |
||
| 15 | |-------------------------------------------------------------------------- |
||
| 16 | */ |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Insert a row in the database. |
||
| 20 | * |
||
| 21 | * @param [Request] All input values to be inserted. |
||
| 22 | * |
||
| 23 | * @return [Eloquent Collection] |
||
|
|
|||
| 24 | */ |
||
| 25 | 5 | public function create($data) |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Get all fields needed for the ADD NEW ENTRY form. |
||
| 42 | * |
||
| 43 | * @return [array] The fields with attributes and fake attributes. |
||
| 44 | */ |
||
| 45 | 23 | public function getCreateFields() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Get all fields with relation set (model key set on field). |
||
| 52 | * |
||
| 53 | * @param [string: create/update/both] |
||
| 54 | * |
||
| 55 | * @return [array] The fields with model key set. |
||
| 56 | */ |
||
| 57 | 16 | public function getRelationFields($form = 'create') |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Get all fields with n-n relation set (pivot table is true). |
||
| 86 | * |
||
| 87 | * @param [string: create/update/both] |
||
| 88 | * |
||
| 89 | * @return [array] The fields with n-n relationships. |
||
| 90 | */ |
||
| 91 | 8 | public function getRelationFieldsWithPivot($form = 'create') |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Create the relations for the current model. |
||
| 102 | * |
||
| 103 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
| 104 | * @param array $data The form data. |
||
| 105 | * @param string $form Optional form type. Can be either 'create', 'update' or 'both'. Default is 'create'. |
||
| 106 | */ |
||
| 107 | 6 | public function createRelations($item, $data, $form = 'create') |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Sync the declared many-to-many associations through the pivot field. |
||
| 115 | * |
||
| 116 | * @param \Illuminate\Database\Eloquent\Model $model The current CRUD model. |
||
| 117 | * @param array $data The form data. |
||
| 118 | * @param string $form Optional form type. Can be either 'create', 'update' or 'both'. Default is 'create'. |
||
| 119 | */ |
||
| 120 | 9 | public function syncPivot($model, $data, $form = 'create') |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Create any existing direct (not n:n) relations for the current model from the form data. |
||
| 147 | * |
||
| 148 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
| 149 | * @param array $data The form data. |
||
| 150 | * @param string $form Optional form type. Can be either 'create', 'update' or 'both'. Default is 'create'. |
||
| 151 | */ |
||
| 152 | 6 | private function createDirectRelations($item, $data, $form = 'create') |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Create any existing one to one relations for the current model from the relation data. |
||
| 160 | * |
||
| 161 | * @param \Illuminate\Database\Eloquent\Model $item The current CRUD model. |
||
| 162 | * @param array $formattedData The form data. |
||
| 163 | */ |
||
| 164 | 6 | private function createRelationsForItem($item, $formattedData) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get a relation data array from the form data. |
||
| 211 | * For each relation defined in the fields through the entity attribute, set the model, the parent model and the |
||
| 212 | * attribute values. For relations defined with the "dot" notations, this will be used to calculate the depth in the |
||
| 213 | * final array (@see \Illuminate\Support\Arr::set() for more). |
||
| 214 | * |
||
| 215 | * @param array $data The form data. |
||
| 216 | * @param string $form Optional form type. Can be either 'create', 'update' or 'both'. Default is 'create'. |
||
| 217 | * |
||
| 218 | * @return array The formatted relation data. |
||
| 219 | */ |
||
| 220 | 6 | private function getRelationDataFromFormData($data, $form = 'create') |
|
| 251 | } |
||
| 252 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.