| Conditions | 5 |
| Paths | 4 |
| Total Lines | 54 |
| 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 |
||
| 18 | public function handle(CreateResourceRequest $request) |
||
| 19 | { |
||
| 20 | if (! $this->isTranslatableResource($request)) { |
||
| 21 | return parent::handle($request); |
||
| 22 | } |
||
| 23 | |||
| 24 | // Inherited from parent controller |
||
| 25 | $resource = $request->resource(); |
||
| 26 | |||
| 27 | $resource::authorizeToCreate($request); |
||
| 28 | $resource::validateForCreation($request); |
||
| 29 | |||
| 30 | $model = DB::transaction(function () use ($request, $resource) { |
||
| 31 | [$model, $callbacks] = $resource::fill( |
||
| 32 | $request, $resource::newModel() |
||
| 33 | ); |
||
| 34 | |||
| 35 | if ($request->viaRelationship()) { |
||
| 36 | $request->findParentModelOrFail() |
||
| 37 | ->{$request->viaRelationship}() |
||
| 38 | ->save($model); |
||
| 39 | } else { |
||
| 40 | $model->save(); |
||
| 41 | } |
||
| 42 | |||
| 43 | ActionEvent::forResourceCreate($request->user(), $model)->save(); |
||
| 44 | |||
| 45 | collect($callbacks)->each->__invoke(); |
||
| 46 | |||
| 47 | return $model; |
||
| 48 | }); |
||
| 49 | |||
| 50 | // Create base translation |
||
| 51 | $currentLocale = Locale::query()->select('id')->where('iso', '=', app()->getLocale())->first(); |
||
| 52 | $baseTranslation = $model->upsertTranslationEntry($currentLocale->id, 0); |
||
| 53 | |||
| 54 | // Create base model |
||
| 55 | $otherLocales = Locale::query()->select('id')->where('id', '!=', $currentLocale->id)->get(); |
||
| 56 | foreach ($otherLocales as $otherLocale) { |
||
| 57 | $otherModel = $resource::newModel(); |
||
| 58 | foreach ($model->getOnCreateTranslatable() as $field) { |
||
| 59 | $otherModel->$field = $model->$field; |
||
| 60 | } |
||
| 61 | $otherModel->save(); |
||
| 62 | $otherModel->upsertTranslationEntry($otherLocale->id, $baseTranslation->translation_id); |
||
| 63 | } |
||
| 64 | |||
| 65 | // Inherited from parent controller |
||
| 66 | return response()->json([ |
||
| 67 | 'id' => $model->getKey(), |
||
| 68 | 'resource' => $model->attributesToArray(), |
||
| 69 | 'redirect' => $resource::redirectAfterCreate($request, $request->newResourceWith($model)), |
||
| 70 | ], 201); |
||
| 71 | } |
||
| 72 | } |
||
| 73 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.