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

[fix] getting attribute by named php agrument keyname #644

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions src/Support/DataType.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,98 @@
default => $type,
};

return $this->acceptsType($type);
}

public function acceptsType(string $type): bool

Check failure on line 64 in src/Support/DataType.php

View workflow job for this annotation

GitHub Actions / phpstan

Cannot redeclare method Spatie\LaravelData\Support\DataType::acceptsType().

Check failure on line 64 in src/Support/DataType.php

View workflow job for this annotation

GitHub Actions / P8.2 - L10.* - prefer-lowest - ubuntu-latest

Cannot redeclare Spatie\LaravelData\Support\DataType::acceptsType()
{
if ($this->isMixed) {
return true;
}

if (array_key_exists($type, $this->acceptedTypes)) {

Check failure on line 70 in src/Support/DataType.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Spatie\LaravelData\Support\DataType::$acceptedTypes.
return true;
}

if (in_array($type, ['string', 'int', 'bool', 'float', 'array'])) {
return false;
}

foreach ([$type, ...$this->resolveBaseTypes($type)] as $givenType) {
if (array_key_exists($givenType, $this->acceptedTypes)) {

Check failure on line 79 in src/Support/DataType.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Spatie\LaravelData\Support\DataType::$acceptedTypes.
return true;
}
}

return false;
}

public function findAcceptedTypeForBaseType(string $class): ?string

Check failure on line 87 in src/Support/DataType.php

View workflow job for this annotation

GitHub Actions / phpstan

Cannot redeclare method Spatie\LaravelData\Support\DataType::findAcceptedTypeForBaseType().
{
foreach ($this->acceptedTypes as $acceptedType => $acceptedBaseTypes) {

Check failure on line 89 in src/Support/DataType.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Spatie\LaravelData\Support\DataType::$acceptedTypes.
if ($class === $acceptedType) {
return $acceptedType;
}

if (in_array($class, $acceptedBaseTypes)) {
return $acceptedType;
}
}

return null;
}

protected function resolveBaseTypes(string $type): array
{
if (! class_exists($type)) {
return [];
}

return array_unique([
...array_values(class_parents($type)),
...array_values(class_implements($type)),
]);
}

protected function resolveDataCollectableClass(
ReflectionProperty|ReflectionParameter $reflection,

Check failure on line 115 in src/Support/DataType.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $reflection of method Spatie\LaravelData\Support\DataType::resolveDataCollectableClass() has invalid type Spatie\LaravelData\Support\ReflectionParameter.

Check failure on line 115 in src/Support/DataType.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $reflection of method Spatie\LaravelData\Support\DataType::resolveDataCollectableClass() has invalid type Spatie\LaravelData\Support\ReflectionProperty.
): ?string {
$attributes = $reflection->getAttributes(DataCollectionOf::class);

Check failure on line 117 in src/Support/DataType.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getAttributes() on an unknown class Spatie\LaravelData\Support\ReflectionParameter.

Check failure on line 117 in src/Support/DataType.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to method getAttributes() on an unknown class Spatie\LaravelData\Support\ReflectionProperty.

Check failure on line 117 in src/Support/DataType.php

View workflow job for this annotation

GitHub Actions / phpstan

Class Spatie\LaravelData\Support\DataCollectionOf not found.

if (! empty($attributes)) {
$attributeArgumentKey = array_key_first($attributes[0]->getArguments());

return $attributes[0]->getArguments()[$attributeArgumentKey];
}

if ($reflection instanceof ReflectionParameter) {
return null;
}

$class = (new DataCollectionAnnotationReader())->getClass($reflection);

if ($class === null) {
throw CannotFindDataClass::wrongDataCollectionAnnotation(
$reflection->class,
$reflection->name
);
}

return $class;
}

protected function resolveDataCollectableType(
ReflectionNamedType $reflection,
): ?DataCollectableType {
$className = $reflection->getName();

return match (true) {
is_a($className, DataCollection::class, true) => DataCollectableType::Default,
is_a($className, PaginatedDataCollection::class, true) => DataCollectableType::Paginated,
is_a($className, CursorPaginatedDataCollection::class, true) => DataCollectableType::CursorPaginated,
default => null,
};

return $this->type->acceptsType($type);
}
}
Loading