From 8d6b3e43969e3a813c5d4d91b8a5fa6a0315f95c Mon Sep 17 00:00:00 2001 From: Eduard Zhylenko Date: Thu, 24 Jul 2025 11:19:14 +0300 Subject: [PATCH] Avoid using null as a default parameter, use MissingValue instead --- Resources/ConditionallyLoadsAttributes.php | 62 +++++++--------------- 1 file changed, 19 insertions(+), 43 deletions(-) diff --git a/Resources/ConditionallyLoadsAttributes.php b/Resources/ConditionallyLoadsAttributes.php index 16e02698..b5cb6dce 100644 --- a/Resources/ConditionallyLoadsAttributes.php +++ b/Resources/ConditionallyLoadsAttributes.php @@ -100,13 +100,13 @@ protected function removeMissingValues($data) * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - protected function when($condition, $value, $default = null) + protected function when($condition, $value, $default = new MissingValue) { if ($condition) { return value($value); } - return func_num_args() === 3 ? value($default) : new MissingValue; + return func_num_args() === 3 ? value($default) : $default; } /** @@ -117,7 +117,7 @@ protected function when($condition, $value, $default = null) * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - public function unless($condition, $value, $default = null) + public function unless($condition, $value, $default = new MissingValue) { $arguments = func_num_args() === 2 ? [$value] : [$value, $default]; @@ -143,13 +143,13 @@ protected function merge($value) * @param mixed $default * @return \Illuminate\Http\Resources\MergeValue|mixed */ - protected function mergeWhen($condition, $value, $default = null) + protected function mergeWhen($condition, $value, $default = new MissingValue) { if ($condition) { return new MergeValue(value($value)); } - return func_num_args() === 3 ? new MergeValue(value($default)) : new MissingValue(); + return func_num_args() === 3 ? new MergeValue(value($default)) : $default; } /** @@ -160,7 +160,7 @@ protected function mergeWhen($condition, $value, $default = null) * @param mixed $default * @return \Illuminate\Http\Resources\MergeValue|mixed */ - protected function mergeUnless($condition, $value, $default = null) + protected function mergeUnless($condition, $value, $default = new MissingValue) { $arguments = func_num_args() === 2 ? [$value] : [$value, $default]; @@ -188,12 +188,8 @@ protected function attributes($attributes) * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - public function whenHas($attribute, $value = null, $default = null) + public function whenHas($attribute, $value = null, $default = new MissingValue) { - if (func_num_args() < 3) { - $default = new MissingValue; - } - if (! array_key_exists($attribute, $this->resource->getAttributes())) { return value($default); } @@ -210,7 +206,7 @@ public function whenHas($attribute, $value = null, $default = null) * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - protected function whenNull($value, $default = null) + protected function whenNull($value, $default = new MissingValue) { $arguments = func_num_args() == 1 ? [$value] : [$value, $default]; @@ -224,7 +220,7 @@ protected function whenNull($value, $default = null) * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - protected function whenNotNull($value, $default = null) + protected function whenNotNull($value, $default = new MissingValue) { $arguments = func_num_args() == 1 ? [$value] : [$value, $default]; @@ -239,13 +235,13 @@ protected function whenNotNull($value, $default = null) * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - protected function whenAppended($attribute, $value = null, $default = null) + protected function whenAppended($attribute, $value = null, $default = new MissingValue) { if ($this->resource->hasAppended($attribute)) { return func_num_args() >= 2 ? value($value) : $this->resource->$attribute; } - return func_num_args() === 3 ? value($default) : new MissingValue; + return func_num_args() === 3 ? value($default) : $default; } /** @@ -256,12 +252,8 @@ protected function whenAppended($attribute, $value = null, $default = null) * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - protected function whenLoaded($relationship, $value = null, $default = null) + protected function whenLoaded($relationship, $value = null, $default = new MissingValue) { - if (func_num_args() < 3) { - $default = new MissingValue; - } - if (! $this->resource->relationLoaded($relationship)) { return value($default); } @@ -291,12 +283,8 @@ protected function whenLoaded($relationship, $value = null, $default = null) * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - public function whenCounted($relationship, $value = null, $default = null) + public function whenCounted($relationship, $value = null, $default = new MissingValue) { - if (func_num_args() < 3) { - $default = new MissingValue; - } - $attribute = (new Stringable($relationship))->snake()->finish('_count')->value(); if (! array_key_exists($attribute, $this->resource->getAttributes())) { @@ -328,12 +316,8 @@ public function whenCounted($relationship, $value = null, $default = null) * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - public function whenAggregated($relationship, $column, $aggregate, $value = null, $default = null) + public function whenAggregated($relationship, $column, $aggregate, $value = null, $default = new MissingValue) { - if (func_num_args() < 5) { - $default = new MissingValue; - } - $attribute = (new Stringable($relationship))->snake()->append('_')->append($aggregate)->append('_')->finish($column)->value(); if (! array_key_exists($attribute, $this->resource->getAttributes())) { @@ -363,12 +347,8 @@ public function whenAggregated($relationship, $column, $aggregate, $value = null * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - public function whenExistsLoaded($relationship, $value = null, $default = null) + public function whenExistsLoaded($relationship, $value = null, $default = new MissingValue) { - if (func_num_args() < 3) { - $default = new MissingValue; - } - $attribute = (new Stringable($relationship))->snake()->finish('_exists')->value(); if (! array_key_exists($attribute, $this->resource->getAttributes())) { @@ -394,7 +374,7 @@ public function whenExistsLoaded($relationship, $value = null, $default = null) * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - protected function whenPivotLoaded($table, $value, $default = null) + protected function whenPivotLoaded($table, $value, $default = new MissingValue) { return $this->whenPivotLoadedAs('pivot', ...func_get_args()); } @@ -408,12 +388,8 @@ protected function whenPivotLoaded($table, $value, $default = null) * @param mixed $default * @return \Illuminate\Http\Resources\MissingValue|mixed */ - protected function whenPivotLoadedAs($accessor, $table, $value, $default = null) + protected function whenPivotLoadedAs($accessor, $table, $value, $default = new MissingValue) { - if (func_num_args() === 3) { - $default = new MissingValue; - } - return $this->when( $this->hasPivotLoadedAs($accessor, $table), $value, @@ -454,10 +430,10 @@ protected function hasPivotLoadedAs($accessor, $table) * @param mixed $default * @return mixed */ - protected function transform($value, callable $callback, $default = null) + protected function transform($value, callable $callback, $default = new MissingValue) { return transform( - $value, $callback, func_num_args() === 3 ? $default : new MissingValue + $value, $callback, $default ); } }