Skip to content

Commit

Permalink
fix: add phpstan types and fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
marijoo committed Jun 6, 2024
1 parent ffbb199 commit 2abdf93
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 13 deletions.
2 changes: 0 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ parameters:
tmpDir: build/phpstan
checkOctaneCompatibility: true
checkModelProperties: true
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false

24 changes: 19 additions & 5 deletions src/DataType/ModelCollectionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function canHandleValue($value): bool
/**
* Convert the value to a string, so that it can be stored in the database.
*
* @param Collection $value
* @param Collection<string, Model> $value
*/
public function serializeValue($value): string
{
Expand All @@ -60,18 +60,29 @@ public function unserializeValue(?string $value): mixed
return null;
}

/** @var null|array<string, null|string|array<string, array<string, string|int|null>>> */
$data = json_decode($value, true);

if (is_null($data) || !is_array($data) || !isset($data['class'])) {
if (
is_null($data)
|| !is_array($data)
|| !isset($data['items'])
|| !is_array($data['items'])
|| !isset($data['class'])
) {
return null;
}

/** @var Collection */
/** @var Collection<string, Model> */
$collection = new $data['class']();
$models = $this->loadModels($data['items']);

/** @var array<string, array<string, string|int|null>> */
$items = $data['items'];

$models = $this->loadModels($items);

// Repopulate collection keys with loaded models.
foreach ($data['items'] as $key => $item) {
foreach ($items as $key => $item) {
if (is_null($item['key']) && ($model = new $item['class']()) instanceof Model) {
$collection->put($key, $model);
} elseif (isset($models[$item['class']][$item['key']])) {
Expand All @@ -84,6 +95,9 @@ public function unserializeValue(?string $value): mixed

/**
* Load each model instance, grouped by class.
*
* @param array<string, array<string, string|int|null>> $items
* @return array<int|string, \Illuminate\Database\Eloquent\Collection<int|string, Model>>
*/
private function loadModels(array $items): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/DataType/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Registry
/**
* List of registered handlers .
*
* @var array
* @var array<string, HandlerInterface>
*/
protected $handlers = [];

Expand Down
6 changes: 4 additions & 2 deletions src/HasConfigurableMorphType.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ protected function isValidUniqueMorphId(mixed $value): bool

/**
* Get the columns that should receive a unique identifier.
*
* @return array<string>
*/
public function uniqueIds(): array
{
Expand Down Expand Up @@ -104,10 +106,10 @@ public function newUniqueId(): ?string
/**
* Retrieve the model for a bound value.
*
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation $query
* @param \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model> $query
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Database\Eloquent\Relations\Relation
* @return \Illuminate\Database\Eloquent\Relations\Relation<\Illuminate\Database\Eloquent\Model>
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
Expand Down
10 changes: 9 additions & 1 deletion src/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public static function boot(): void

/**
* Metable Relation.
*
* @return MorphTo<Model, Meta>
*/
public function metable(): MorphTo
{
Expand Down Expand Up @@ -211,6 +213,8 @@ public static function getDataTypeRegistry(): Registry

/**
* Query records where value is considered empty.
*
* @param Builder<Meta> $query
*/
public function scopeWhereValueEmpty(Builder $query): void
{
Expand All @@ -219,6 +223,8 @@ public function scopeWhereValueEmpty(Builder $query): void

/**
* Query records where value is considered not empty.
*
* @param Builder<Meta> $query
*/
public function scopeWhereValueNotEmpty(Builder $query): void
{
Expand All @@ -229,8 +235,9 @@ public function scopeWhereValueNotEmpty(Builder $query): void
* Query records where value equals the serialized version of the given value.
* If `$type` is omited the type will be taken from the data type registry.
*
* @param mixed $operator
* @param Builder<Meta> $query
* @param mixed $value
* @param mixed $operator
* @param ?string $type
*/
public function scopeWhereValue(Builder $query, $value, $operator = '=', ?string $type = null): void
Expand All @@ -251,6 +258,7 @@ public function scopeWhereValue(Builder $query, $value, $operator = '=', ?string
* If `$type` is omited the type will be taken from the data type registry.
*
* @param Builder<Meta> $query
* @param array<mixed> $values
* @param ?string $type
*/
public function scopeWhereValueIn(Builder $query, array $values, ?string $type = null): void
Expand Down
7 changes: 5 additions & 2 deletions src/MetaAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

/**
* @implements CastsAttributes<mixed, mixed>
*/
class MetaAttribute implements CastsAttributes
{
/**
Expand All @@ -12,7 +15,7 @@ class MetaAttribute implements CastsAttributes
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @param array<mixed> $attributes
*/
public function get($model, $key, $value, $attributes): mixed
{
Expand All @@ -30,7 +33,7 @@ public function get($model, $key, $value, $attributes): mixed
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @param array<mixed> $attributes
*/
public function set($model, $key, $value, $attributes): mixed
{
Expand Down
4 changes: 4 additions & 0 deletions tests/DataTypeHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static function handlerProvider(): array

/**
* @test
*
* @dataProvider handlerProvider
*/
public function it_specifies_a_datatype_identifier(HandlerInterface $handler, $type): void
Expand All @@ -118,6 +119,7 @@ public function it_specifies_a_datatype_identifier(HandlerInterface $handler, $t

/**
* @test
*
* @dataProvider handlerProvider
*/
public function it_can_verify_compatibility(HandlerInterface $handler, $type, $value, $incompatible): void
Expand All @@ -131,6 +133,7 @@ public function it_can_verify_compatibility(HandlerInterface $handler, $type, $v

/**
* @test
*
* @dataProvider handlerProvider
*/
public function it_can_serialize_and_unserialize_values(HandlerInterface $handler, $type, $value, $incompatible, ?callable $closure = null): void
Expand All @@ -147,6 +150,7 @@ public function it_can_serialize_and_unserialize_values(HandlerInterface $handle

/**
* @test
*
* @dataProvider handlerProvider
*/
public function it_can_handle_null_values(HandlerInterface $handler): void
Expand Down
1 change: 1 addition & 0 deletions tests/HasMetaScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function it_scopes_where_meta(): void

/**
* @test
*
* @dataProvider datatypeProvider
*/
public function it_scopes_where_meta_with_datatype($type, $input, $another): void
Expand Down
2 changes: 2 additions & 0 deletions tests/MetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ public function it_can_include_only_current(): void

/**
* @test
*
* @dataProvider handlerProvider
*/
public function it_can_store_and_retrieve_datatypes($type, $input): void
Expand All @@ -394,6 +395,7 @@ public function it_can_store_and_retrieve_datatypes($type, $input): void

/**
* @test
*
* @dataProvider handlerProvider
*/
public function it_can_query_by_value($type, $input): void
Expand Down
4 changes: 4 additions & 0 deletions tests/PrimaryKeyTypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function refreshDatabaseWithType($type): void

/**
* @test
*
* @dataProvider morphTypes
**/
public function it_uses_the_configured_column_type(string $type, string $column_type): void
Expand Down Expand Up @@ -51,6 +52,7 @@ public function it_uses_the_configured_column_type(string $type, string $column_

/**
* @test
*
* @dataProvider stringMorphTypes
*/
public function it_throws_error_for_invalid_unique_ids(string $type): void
Expand All @@ -66,6 +68,7 @@ public function it_throws_error_for_invalid_unique_ids(string $type): void

/**
* @test
*
* @dataProvider stringMorphTypes
*/
public function it_throws_error_for_invalid_unique_ids_with_implicit_key_name(string $type): void
Expand All @@ -92,6 +95,7 @@ public function it_resolves_unique_id_models_by_key(): void

/**
* @test
*
* @dataProvider stringMorphTypes
*/
public function it_resolves_integer_id_models_by_key(string $type): void
Expand Down

0 comments on commit 2abdf93

Please sign in to comment.