Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General code health improvements #920

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions src/Attributes/Validation/Exclude.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Attributes/Validation/ValidationAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Casts/UnserializeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class UnserializeCast implements Cast
{
public function __construct(
private bool $failSilently = false,
private readonly bool $failSilently = false,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/DataMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 4 additions & 7 deletions src/Concerns/AppendableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ public function getAdditionalData(): array
{
$additional = $this->with();

$computedAdditional = [];

foreach ($additional as $name => $value) {
$computedAdditional[$name] = $value instanceof Closure
? ($value)($this)
: $value;
}
$computedAdditional = array_map(
fn ($value) => $value instanceof Closure ? ($value)($this) : $value,
$additional
);

foreach ($this->_additional as $name => $value) {
$computedAdditional[$name] = $value instanceof Closure
Expand Down
1 change: 0 additions & 1 deletion src/Concerns/ValidateableData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/DataPipes/CastPropertiesDataPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 0 additions & 2 deletions src/DataPipes/DefaultValuesDataPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public function handle(

if ($property->type->isNullable) {
$properties[$name] = null;

continue;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/CannotPerformPartialOnDataField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 8 additions & 6 deletions src/Resolvers/DataValidationMessagesAndAttributesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -63,13 +64,14 @@ public function execute(
[...$nestingChain, $dataProperty->type->dataClass],
);

$messages = array_merge($messages, $collected['messages']);
$attributes = array_merge($attributes, $collected['attributes']);

continue;
$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(
Expand Down
2 changes: 1 addition & 1 deletion src/Resolvers/EmptyDataResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 1 addition & 5 deletions src/Resolvers/RequestQueryStringPartialsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
9 changes: 4 additions & 5 deletions src/Resolvers/TransformedDataCollectableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ protected function transformItems(
bool $executeWrap,
TransformationContext $nestedContext,
): array {
$collection = [];

foreach ($items as $key => $value) {
$collection[$key] = $this->transformationClosure($nestedContext)($value);
}
$collection = array_map(
fn ($value) => $this->transformationClosure($nestedContext)($value),
is_array($items) ? $items : $items->all()
);

return $executeWrap
? $wrap->wrap($collection)
Expand Down
4 changes: 1 addition & 3 deletions src/Support/Caching/DataStructureCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Creation/CreationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function next(
): self {
$this->dataClass = $dataClass;

array_push($this->currentPath, $path);
$this->currentPath[] = $path;

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Support/DataConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function createFromConfig(array $config): static
$dataClasses = [];

$ruleInferrers = array_map(
fn (string $ruleInferrerClass) => app($ruleInferrerClass),
app(...),
$config['rule_inferrers'] ?? []
);

Expand Down
1 change: 0 additions & 1 deletion src/Support/EloquentCasts/DataEloquentCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Partials/Partial.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down