From 3b1b0e5aad15fdc0a981af15ae6b81dbb429617d Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 12:30:31 +0100 Subject: [PATCH 01/13] Use []= instead of array_push The []= syntax has superior performance compared to the function call. --- src/Support/Creation/CreationContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/Creation/CreationContext.php b/src/Support/Creation/CreationContext.php index 55d421af..374fd187 100644 --- a/src/Support/Creation/CreationContext.php +++ b/src/Support/Creation/CreationContext.php @@ -79,7 +79,7 @@ public function next( ): self { $this->dataClass = $dataClass; - array_push($this->currentPath, $path); + $this->currentPath[] = $path; return $this; } From 32c86aa0ba759081aaec6a02bd4e478c9ce20cd7 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 12:32:44 +0100 Subject: [PATCH 02/13] Remove unnecessary type casting The class gets auto-casted to string since its a string concatenation. Explicit casting is unnecessary in a non-strict context. --- src/Exceptions/CannotPerformPartialOnDataField.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exceptions/CannotPerformPartialOnDataField.php b/src/Exceptions/CannotPerformPartialOnDataField.php index 15e47869..c06326d5 100644 --- a/src/Exceptions/CannotPerformPartialOnDataField.php +++ b/src/Exceptions/CannotPerformPartialOnDataField.php @@ -19,7 +19,7 @@ public static function create( ): self { $message = "Tried to {$partialType->getVerb()} a non existing field `{$field}` on `{$dataClass->name}`.".PHP_EOL; $message .= 'Provided transformation context:'.PHP_EOL.PHP_EOL; - $message .= (string) $transformationContext; + $message .= $transformationContext; return new self(message: $message, previous: $exception); } From 033c80ab660df1cc7f0d707b5e9d806e6e4a59e9 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 12:45:55 +0100 Subject: [PATCH 03/13] Remove useless statements --- src/Concerns/ValidateableData.php | 1 - src/DataPipes/DefaultValuesDataPipe.php | 2 -- src/Resolvers/DataValidationMessagesAndAttributesResolver.php | 2 -- src/Support/EloquentCasts/DataEloquentCast.php | 1 - 4 files changed, 6 deletions(-) diff --git a/src/Concerns/ValidateableData.php b/src/Concerns/ValidateableData.php index b48716e4..d4ad96ff 100644 --- a/src/Concerns/ValidateableData.php +++ b/src/Concerns/ValidateableData.php @@ -41,7 +41,6 @@ public static function validateAndCreate(Arrayable|array $payload): static public static function withValidator(Validator $validator): void { - return; } public static function getValidationRules(array $payload): array diff --git a/src/DataPipes/DefaultValuesDataPipe.php b/src/DataPipes/DefaultValuesDataPipe.php index a89ea37d..fcea69e0 100644 --- a/src/DataPipes/DefaultValuesDataPipe.php +++ b/src/DataPipes/DefaultValuesDataPipe.php @@ -33,8 +33,6 @@ public function handle( if ($property->type->isNullable) { $properties[$name] = null; - - continue; } } diff --git a/src/Resolvers/DataValidationMessagesAndAttributesResolver.php b/src/Resolvers/DataValidationMessagesAndAttributesResolver.php index dac90ba1..04e0f557 100644 --- a/src/Resolvers/DataValidationMessagesAndAttributesResolver.php +++ b/src/Resolvers/DataValidationMessagesAndAttributesResolver.php @@ -65,8 +65,6 @@ public function execute( $messages = array_merge($messages, $collected['messages']); $attributes = array_merge($attributes, $collected['attributes']); - - continue; } } diff --git a/src/Support/EloquentCasts/DataEloquentCast.php b/src/Support/EloquentCasts/DataEloquentCast.php index 6e709573..551587d7 100644 --- a/src/Support/EloquentCasts/DataEloquentCast.php +++ b/src/Support/EloquentCasts/DataEloquentCast.php @@ -74,7 +74,6 @@ public function set($model, string $key, $value, array $attributes): ?string 'data' => json_decode($value->toJson(), associative: true, flags: JSON_THROW_ON_ERROR), ]) : $value->toJson(); - ; if (in_array('encrypted', $this->arguments)) { return Crypt::encryptString($value); From eb30ea55ab60146913fa2e704a2ff7b5992392bf Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 12:50:28 +0100 Subject: [PATCH 04/13] Narrow return type --- src/Resolvers/EmptyDataResolver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resolvers/EmptyDataResolver.php b/src/Resolvers/EmptyDataResolver.php index 3509d246..0749f13b 100644 --- a/src/Resolvers/EmptyDataResolver.php +++ b/src/Resolvers/EmptyDataResolver.php @@ -34,7 +34,7 @@ public function execute(string $class, array $extra = []): array return $payload; } - protected function getValueForProperty(DataProperty $property): mixed + protected function getValueForProperty(DataProperty $property): ?array { $propertyType = $property->type; From 92e1ec410d921b348b0adcb9cfe11d3970be842b Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 12:55:19 +0100 Subject: [PATCH 05/13] Use nullsafe operator instead of if branch --- src/Support/Caching/DataStructureCache.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Support/Caching/DataStructureCache.php b/src/Support/Caching/DataStructureCache.php index 0968cdc1..88a5e948 100644 --- a/src/Support/Caching/DataStructureCache.php +++ b/src/Support/Caching/DataStructureCache.php @@ -27,9 +27,7 @@ public function getConfig(): ?CachedDataConfig /** @var ?CachedDataConfig $cachedConfig */ $cachedConfig = $this->get('config'); - if ($cachedConfig) { - $cachedConfig->setCache($this); - } + $cachedConfig?->setCache($this); return $cachedConfig; } From 58298cbb4b1d2209a261aab7b4484fc74521e5bc Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 13:01:19 +0100 Subject: [PATCH 06/13] Mark failSilently as readonly --- src/Casts/UnserializeCast.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Casts/UnserializeCast.php b/src/Casts/UnserializeCast.php index db3ffc31..d06686bb 100644 --- a/src/Casts/UnserializeCast.php +++ b/src/Casts/UnserializeCast.php @@ -8,7 +8,7 @@ class UnserializeCast implements Cast { public function __construct( - private bool $failSilently = false, + private readonly bool $failSilently = false, ) { } From 57f46de41bcc17195eefaa1606d173ecf50df0bb Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 13:04:03 +0100 Subject: [PATCH 07/13] Use first-class callables --- src/Support/DataConfig.php | 2 +- src/Support/Partials/Partial.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Support/DataConfig.php b/src/Support/DataConfig.php index fa2a0a30..946bdc87 100644 --- a/src/Support/DataConfig.php +++ b/src/Support/DataConfig.php @@ -15,7 +15,7 @@ public static function createFromConfig(array $config): static $dataClasses = []; $ruleInferrers = array_map( - fn (string $ruleInferrerClass) => app($ruleInferrerClass), + app(...), $config['rule_inferrers'] ?? [] ); diff --git a/src/Support/Partials/Partial.php b/src/Support/Partials/Partial.php index 82c6c929..18c337df 100644 --- a/src/Support/Partials/Partial.php +++ b/src/Support/Partials/Partial.php @@ -90,7 +90,7 @@ protected static function resolveSegmentsFromPath(string $path): array substr($segmentString, 1, -1) ); - $segments[] = new FieldsPartialSegment(array_map(fn (string $field) => trim($field), $fields)); + $segments[] = new FieldsPartialSegment(array_map(trim(...), $fields)); return $segments; } From 0659716d6e2abc97c8740f3d4a462edd631a7390 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 13:05:37 +0100 Subject: [PATCH 08/13] Use short notation for concatenation --- src/Commands/DataMakeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/DataMakeCommand.php b/src/Commands/DataMakeCommand.php index 075c51fa..a828dc9b 100644 --- a/src/Commands/DataMakeCommand.php +++ b/src/Commands/DataMakeCommand.php @@ -39,7 +39,7 @@ protected function qualifyClass($name): string { $suffix = trim($this->option('suffix')); if (! empty($suffix) && ! Str::endsWith($name, $suffix)) { - $name = $name . $suffix; + $name .= $suffix; } return parent::qualifyClass($name); From 5665929909ca9910d332fa36a54bd4a42007e991 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 13:06:42 +0100 Subject: [PATCH 09/13] Use is_object instead of gettype --- src/DataPipes/CastPropertiesDataPipe.php | 2 +- src/Support/Transformation/GlobalTransformersCollection.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DataPipes/CastPropertiesDataPipe.php b/src/DataPipes/CastPropertiesDataPipe.php index b13d5ce6..95967a50 100644 --- a/src/DataPipes/CastPropertiesDataPipe.php +++ b/src/DataPipes/CastPropertiesDataPipe.php @@ -129,7 +129,7 @@ protected function cast( protected function shouldBeCasted(DataProperty $property, mixed $value): bool { - if (gettype($value) !== 'object') { + if (!is_object($value)) { return true; } diff --git a/src/Support/Transformation/GlobalTransformersCollection.php b/src/Support/Transformation/GlobalTransformersCollection.php index 29ca499e..a00a32b2 100644 --- a/src/Support/Transformation/GlobalTransformersCollection.php +++ b/src/Support/Transformation/GlobalTransformersCollection.php @@ -26,7 +26,7 @@ public function add(string $transformable, Transformer $transformer): self public function findTransformerForValue(mixed $value): ?Transformer { - if (gettype($value) !== 'object') { + if (!is_object($value)) { return $this->transformers[get_debug_type($value)] ?? null; } From d0dbe2d223da785a8527ba1a62b2fd576314167c Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 13:07:11 +0100 Subject: [PATCH 10/13] Simplify return --- src/Resolvers/RequestQueryStringPartialsResolver.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Resolvers/RequestQueryStringPartialsResolver.php b/src/Resolvers/RequestQueryStringPartialsResolver.php index ead50f69..1ff34e4b 100644 --- a/src/Resolvers/RequestQueryStringPartialsResolver.php +++ b/src/Resolvers/RequestQueryStringPartialsResolver.php @@ -151,10 +151,6 @@ protected function findField( $outputMappedProperties = $dataClass->outputMappedProperties->resolve(); - if (array_key_exists($field, $outputMappedProperties)) { - return $outputMappedProperties[$field]; - } - - return null; + return $outputMappedProperties[$field] ?? null; } } From 339df94fe8fe628fcbb9f52a990ef33c633f0e22 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 13:14:15 +0100 Subject: [PATCH 11/13] Make rule nullable as create() initializes constructor without value --- src/Attributes/Validation/Exclude.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Attributes/Validation/Exclude.php b/src/Attributes/Validation/Exclude.php index 15cf1276..6f83a6a5 100644 --- a/src/Attributes/Validation/Exclude.php +++ b/src/Attributes/Validation/Exclude.php @@ -9,13 +9,13 @@ #[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] class Exclude extends ObjectValidationAttribute { - public function __construct(protected ExcludeIf $rule) + public function __construct(protected ?ExcludeIf $rule = null) { } public function getRule(ValidationPath $path): object|string { - return $this->rule; + return $this->rule ?? self::keyword(); } public static function keyword(): string From 6254aa5edb72a2a4cff7e3f3813b273d8dbd3ba5 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 13:23:24 +0100 Subject: [PATCH 12/13] Put the array_merge outside the loop as it can be resource-heavy --- .../DataValidationMessagesAndAttributesResolver.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Resolvers/DataValidationMessagesAndAttributesResolver.php b/src/Resolvers/DataValidationMessagesAndAttributesResolver.php index 04e0f557..69d8919f 100644 --- a/src/Resolvers/DataValidationMessagesAndAttributesResolver.php +++ b/src/Resolvers/DataValidationMessagesAndAttributesResolver.php @@ -45,8 +45,9 @@ public function execute( [...$nestingChain, $dataProperty->type->dataClass], ); - $messages = array_merge($messages, $nested['messages']); - $attributes = array_merge($attributes, $nested['attributes']); + + $messages[] = $nested['messages']; + $attributes[] = $nested['attributes']; continue; } @@ -63,11 +64,14 @@ public function execute( [...$nestingChain, $dataProperty->type->dataClass], ); - $messages = array_merge($messages, $collected['messages']); - $attributes = array_merge($attributes, $collected['attributes']); + $messages[] = $collected['messages']; + $attributes[] = $collected['attributes']; } } + $messages = array_merge(...$messages); + $attributes = array_merge(...$attributes); + if (method_exists($class, 'messages')) { $messages = collect(app()->call([$class, 'messages'])) ->keyBy( From b5c19608647413966db7ae0b5250cc7a264262d1 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Mon, 30 Dec 2024 14:15:01 +0100 Subject: [PATCH 13/13] Parse boolean string properly --- src/Attributes/Validation/ValidationAttribute.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Attributes/Validation/ValidationAttribute.php b/src/Attributes/Validation/ValidationAttribute.php index cbc3e110..4c4e8eef 100644 --- a/src/Attributes/Validation/ValidationAttribute.php +++ b/src/Attributes/Validation/ValidationAttribute.php @@ -46,11 +46,11 @@ protected static function parseBooleanValue(mixed $value): mixed } if ($value === 'true' || $value === '1') { - return true; + return 'true'; } if ($value === 'false' || $value === '0') { - return true; + return 'false'; } return $value;