From 666be63df18e0176a45aacd7d8bff5c4c9e4896a Mon Sep 17 00:00:00 2001 From: JoaoFerrazfs Date: Wed, 2 Oct 2024 09:58:52 -0300 Subject: [PATCH 1/2] refactor: changed types and annotations --- src/Model/AbstractModel.php | 38 +++------ src/Model/AttributesService.php | 62 +++++---------- src/Model/DocumentEmbedder.php | 13 ++-- .../Exception/ModelNotFoundException.php | 12 +-- src/Model/HasAttributesInterface.php | 10 +-- src/Model/HasAttributesTrait.php | 38 +++++---- src/Model/HasLegacyAttributesTrait.php | 78 ++++++++----------- src/Model/Relations/AbstractRelation.php | 51 +++--------- src/Model/Relations/EmbedsMany.php | 7 +- src/Model/Relations/EmbedsOne.php | 5 +- src/Model/Relations/ReferencesMany.php | 14 ++-- src/Model/Relations/ReferencesOne.php | 7 +- src/Model/Relations/RelationInterface.php | 4 +- 13 files changed, 115 insertions(+), 224 deletions(-) diff --git a/src/Model/AbstractModel.php b/src/Model/AbstractModel.php index dd5021a8..81f197cd 100644 --- a/src/Model/AbstractModel.php +++ b/src/Model/AbstractModel.php @@ -29,42 +29,34 @@ abstract class AbstractModel implements ModelInterface * model is queried, it will load its referenced * models together. * - * @var array + * @var string[] */ - public $with = []; + public array $with = []; /** * The $dynamic property tells if the object will accept additional fields * that are not specified in the $fillable or $guarded properties. * This is useful if you do not have a strict document format or * if you want to take full advantage of the "schemaless" nature of MongoDB. - * - * @var bool */ - protected $dynamic = true; + protected bool $dynamic = true; /** * Whether the model should manage the `created_at` and `updated_at` * timestamps automatically. - * - * @var bool */ - protected $timestamps = true; + protected bool $timestamps = true; /** * Name of the collection where this kind of Model is going to be saved or * retrieved from. - * - * @var string */ - protected $collection = null; + protected ?string $collection = null; /** * @see https://docs.mongodb.com/manual/reference/write-concern/ - * - * @var int */ - protected $writeConcern = 1; + protected int $writeConcern = 1; /** * Gets a cursor of this kind of entities that matches the query from the @@ -93,10 +85,8 @@ public static function all(): CursorInterface * @param mixed $query mongoDB selection criteria * @param array $projection fields to project in Mongo query * @param boolean $useCache retrieves the first through a CacheableCursor - * - * @return AbstractModel|null */ - public static function first($query = [], array $projection = [], bool $useCache = false) + public static function first(mixed $query = [], array $projection = [], bool $useCache = false): ?static { return self::getBuilderInstance()->first(new static(), $query, $projection, $useCache); } @@ -110,10 +100,8 @@ public static function first($query = [], array $projection = [], bool $useCache * @param boolean $useCache retrieves the first through a CacheableCursor * * @throws ModelNotFoundException If no document was found - * - * @return AbstractModel|null */ - public static function firstOrFail($query = [], array $projection = [], bool $useCache = false) + public static function firstOrFail(mixed $query = [], array $projection = [], bool $useCache = false): ?static { return self::getBuilderInstance()->firstOrFail(new static(), $query, $projection); } @@ -124,10 +112,8 @@ public static function firstOrFail($query = [], array $projection = [], bool $us * _if field filled. * * @param mixed $id document id - * - * @return AbstractModel|null */ - public static function firstOrNew($id) + public static function firstOrNew(mixed $id): ?static { if (!$model = self::first($id)) { $model = new static(); @@ -196,10 +182,8 @@ public function fresh(): self * Dynamically retrieve attributes on the model. * * @param string $key name of the attribute - * - * @return mixed */ - public function &__get(string $key) + public function &__get(string $key): mixed { return $this->getDocumentAttribute($key); } @@ -210,7 +194,7 @@ public function &__get(string $key) * @param string $key attribute name * @param mixed $value value to be set */ - public function __set(string $key, $value): void + public function __set(string $key, mixed $value): void { $this->setDocumentAttribute($key, $value); } diff --git a/src/Model/AttributesService.php b/src/Model/AttributesService.php index 6bfb9ee5..3a69bd8e 100644 --- a/src/Model/AttributesService.php +++ b/src/Model/AttributesService.php @@ -11,47 +11,45 @@ class AttributesService * the attributes specified here will be changed * with the setDocumentAttribute method. * - * @var array + * @var string[] */ - protected $fillable = []; + protected array $fillable = []; /** * The attributes that are not mass assignable. The opposite * to the fillable array;. * - * @var array + * @var string[] */ - protected $guarded = []; + protected array $guarded = []; /** * Check if model should mutate attributes checking * the existence of a specific method on model * class. Default is false. - * - * @var bool */ - protected $mutable = false; + protected bool $mutable = false; /** * Store mutable attribute values to work with `&__get()`. * - * @var array + * @var string[] */ - protected $mutableCache = []; + protected array $mutableCache = []; /** * The model's attributes. * - * @var array + * @var string[] */ - private $attributes = []; + private array $attributes = []; /** * The model attribute's original state. * - * @var array + * @var array */ - private $originalAttributes = []; + private array $originalAttributes = []; /** * {@inheritdoc} @@ -68,7 +66,7 @@ public static function fill( if ($object instanceof PolymorphableModelInterface) { $class = $object->polymorph(array_merge($object->getDocumentAttributes(), $input)); - if ($class !== get_class($object)) { + if ($class !== $object::class) { $originalAttributes = $object->getDocumentAttributes(); $object = new $class(); @@ -92,18 +90,12 @@ public static function fill( return $object; } - /** - * {@inheritdoc} - */ public function hasDocumentAttribute(string $key): bool { return !is_null($this->getDocumentAttribute($key)); } - /** - * {@inheritdoc} - */ - public function &getDocumentAttribute(string $key) + public function &getDocumentAttribute(string $key): mixed { if ($this->mutable && $this->hasMutatorMethod($key, 'get')) { $this->mutableCache[$key] = $this->{$this->buildMutatorMethod($key, 'get')}(); @@ -124,9 +116,6 @@ public function &getDocumentAttribute(string $key) return $this->attributes[$key]; } - /** - * {@inheritdoc} - */ public function getDocumentAttributes(): array { foreach ($this->attributes as $field => $value) { @@ -138,10 +127,7 @@ public function getDocumentAttributes(): array return $this->attributes ?? []; } - /** - * {@inheritdoc} - */ - public function cleanDocumentAttribute(string $key) + public function cleanDocumentAttribute(string $key): void { unset($this->attributes[$key]); @@ -150,10 +136,7 @@ public function cleanDocumentAttribute(string $key) } } - /** - * {@inheritdoc} - */ - public function setDocumentAttribute(string $key, $value) + public function setDocumentAttribute(string $key, $value): void { if ($this->mutable && $this->hasMutatorMethod($key, 'set')) { $value = $this->{$this->buildMutatorMethod($key, 'set')}($value); @@ -172,29 +155,20 @@ public function setDocumentAttribute(string $key, $value) } } - /** - * {@inheritdoc} - */ - public function syncOriginalDocumentAttributes() + public function syncOriginalDocumentAttributes(): void { try { $this->originalAttributes = unserialize(serialize($this->getDocumentAttributes())); - } catch (Exception $e) { + } catch (Exception) { $this->originalAttributes = $this->getDocumentAttributes(); } } - /** - * {@inheritdoc} - */ public function getOriginalDocumentAttributes(): array { return $this->originalAttributes; } - /** - * {@inheritdoc} - */ public function toArray(): array { return $this->getDocumentAttributes(); @@ -206,7 +180,7 @@ public function toArray(): array * @param string $key attribute name * @param string $prefix method prefix to be used (get, set) */ - protected function hasMutatorMethod(string $key, $prefix): bool + protected function hasMutatorMethod(string $key, string $prefix): bool { $method = $this->buildMutatorMethod($key, $prefix); diff --git a/src/Model/DocumentEmbedder.php b/src/Model/DocumentEmbedder.php index 0835f1b5..dd7b0301 100644 --- a/src/Model/DocumentEmbedder.php +++ b/src/Model/DocumentEmbedder.php @@ -20,7 +20,7 @@ class DocumentEmbedder * * @return bool Success */ - public function embed($parent, string $field, &$entity): bool + public function embed(mixed $parent, string $field, mixed &$entity): bool { // In order to update the document if it exists inside the $parent $this->unembed($parent, $field, $entity); @@ -42,7 +42,7 @@ public function embed($parent, string $field, &$entity): bool * * @return bool Success */ - public function unembed($parent, string $field, &$entity): bool + public function unembed(mixed $parent, string $field, mixed &$entity): bool { $fieldValue = (array) $parent->$field; $id = $this->getId($entity); @@ -63,11 +63,11 @@ public function unembed($parent, string $field, &$entity): bool * * @param mixed $parent the object where $entity will be referenced * @param string $field the field where the _id reference of $entity will be stored - * @param object|array $entity the object that is being attached + * @param mixed $entity the object that is being attached * * @return bool Success */ - public function attach($parent, string $field, &$entity): bool + public function attach(mixed $parent, string $field, mixed &$entity): bool { $fieldValue = (array) $parent->$field; $newId = $this->getId($entity); @@ -93,7 +93,7 @@ public function attach($parent, string $field, &$entity): bool * * @return bool Success */ - public function detach($parent, string $field, &$entity): bool + public function detach(mixed $parent, string $field, mixed &$entity): bool { $fieldValue = (array) $parent->$field; $newId = $this->getId($entity); @@ -115,9 +115,8 @@ public function detach($parent, string $field, &$entity): bool * * @param mixed $object the object|array that the _id will be retrieved from * - * @return ObjectId|mixed */ - protected function getId(&$object) + protected function getId(mixed &$object): mixed { if (is_array($object)) { if (isset($object['_id']) && $object['_id']) { diff --git a/src/Model/Exception/ModelNotFoundException.php b/src/Model/Exception/ModelNotFoundException.php index f51e8282..0dfe873e 100644 --- a/src/Model/Exception/ModelNotFoundException.php +++ b/src/Model/Exception/ModelNotFoundException.php @@ -17,19 +17,15 @@ class ModelNotFoundException extends RuntimeException /** * Name of the affected Mongolid model. - * - * @var string */ - protected $model; + protected string $model; /** * Set the affected Mongolid model. * * @param string $model name of the model - * - * @return $this */ - public function setModel(string $model) + public function setModel(string $model): self { $this->model = $model; @@ -40,10 +36,8 @@ public function setModel(string $model) /** * Get the affected Mongolid model. - * - * @return string */ - public function getModel() + public function getModel(): string { return $this->model; } diff --git a/src/Model/HasAttributesInterface.php b/src/Model/HasAttributesInterface.php index d7bdedda..1b166ba3 100644 --- a/src/Model/HasAttributesInterface.php +++ b/src/Model/HasAttributesInterface.php @@ -22,10 +22,8 @@ public function hasDocumentAttribute(string $key): bool; * Get an attribute from the model. * * @param string $key the attribute to be accessed - * - * @return mixed */ - public function getDocumentAttribute(string $key); + public function getDocumentAttribute(string $key): mixed; /** * Get all attributes from the model. @@ -37,7 +35,7 @@ public function getDocumentAttributes(): array; * * @param string $key name of the attribute to be unset */ - public function cleanDocumentAttribute(string $key); + public function cleanDocumentAttribute(string $key): void; /** * Set a given attribute on the model. @@ -45,7 +43,7 @@ public function cleanDocumentAttribute(string $key); * @param string $key name of the attribute to be set * @param mixed $value value to be set */ - public function setDocumentAttribute(string $key, $value); + public function setDocumentAttribute(string $key, mixed $value): mixed; /** * Stores original attributes from actual data from attributes @@ -54,7 +52,7 @@ public function setDocumentAttribute(string $key, $value); * Ideally should be called once right after retrieving data from * the database. */ - public function syncOriginalDocumentAttributes(); + public function syncOriginalDocumentAttributes(): mixed; /** * Retrieve original attributes. diff --git a/src/Model/HasAttributesTrait.php b/src/Model/HasAttributesTrait.php index 1b3b0993..4387056b 100644 --- a/src/Model/HasAttributesTrait.php +++ b/src/Model/HasAttributesTrait.php @@ -24,47 +24,45 @@ trait HasAttributesTrait * the attributes specified here will be changed * with the setDocumentAttribute method. * - * @var array + * @var string[] */ - protected $fillable = []; + protected array $fillable = []; /** * The attributes that are not mass assignable. The opposite * to the fillable array;. * - * @var array + * @var string[] */ - protected $guarded = []; + protected array $guarded = []; /** * Check if model should mutate attributes checking * the existence of a specific method on model * class. Default is false. - * - * @var bool */ - protected $mutable = false; + protected bool $mutable = false; /** * Store mutable attribute values to work with `&__get()`. * - * @var array + * @var string[] */ - protected $mutableCache = []; + protected array $mutableCache = []; /** * The model's attributes. * - * @var array + * @var array */ - private $attributes = []; + private array $attributes = []; /** * The model attribute's original state. * - * @var array + * @var array */ - private $originalAttributes = []; + private array $originalAttributes = []; /** * Attributes that are cast to another types when fetched from database. @@ -86,7 +84,7 @@ public static function fill( if ($object instanceof PolymorphableModelInterface) { $class = $object->polymorph(array_merge($object->getDocumentAttributes(), $input)); - if ($class !== get_class($object)) { + if ($class !== $object::class) { $originalAttributes = $object->getDocumentAttributes(); $object = new $class(); @@ -121,7 +119,7 @@ public function hasDocumentAttribute(string $key): bool /** * {@inheritdoc} */ - public function &getDocumentAttribute(string $key) + public function &getDocumentAttribute(string $key): mixed { if ($this->mutable && $this->hasMutatorMethod($key, 'get')) { $this->mutableCache[$key] = $this->{$this->buildMutatorMethod($key, 'get')}(); @@ -166,7 +164,7 @@ public function getDocumentAttributes(): array /** * {@inheritdoc} */ - public function cleanDocumentAttribute(string $key) + public function cleanDocumentAttribute(string $key): void { unset($this->attributes[$key]); @@ -178,7 +176,7 @@ public function cleanDocumentAttribute(string $key) /** * {@inheritdoc} */ - public function setDocumentAttribute(string $key, $value) + public function setDocumentAttribute(string $key, mixed $value): void { if ($this->mutable && $this->hasMutatorMethod($key, 'set')) { $value = $this->{$this->buildMutatorMethod($key, 'set')}($value); @@ -205,11 +203,11 @@ public function setDocumentAttribute(string $key, $value) /** * {@inheritdoc} */ - public function syncOriginalDocumentAttributes() + public function syncOriginalDocumentAttributes(): void { try { $this->originalAttributes = unserialize(serialize($this->getDocumentAttributes())); - } catch (Exception $e) { + } catch (Exception) { $this->originalAttributes = $this->getDocumentAttributes(); } } @@ -236,7 +234,7 @@ public function toArray(): array * @param string $key attribute name * @param string $prefix method prefix to be used (get, set) */ - protected function hasMutatorMethod(string $key, $prefix): bool + protected function hasMutatorMethod(string $key, string $prefix): bool { $method = $this->buildMutatorMethod($key, $prefix); diff --git a/src/Model/HasLegacyAttributesTrait.php b/src/Model/HasLegacyAttributesTrait.php index a4910a7e..f15dcf5b 100644 --- a/src/Model/HasLegacyAttributesTrait.php +++ b/src/Model/HasLegacyAttributesTrait.php @@ -18,42 +18,40 @@ trait HasLegacyAttributesTrait /** * The model's attributes. * - * @var array + * @var array */ - protected $attributes = []; + protected array $attributes = []; /** * The model attribute's original state. * - * @var array + * @var array */ - protected $original = []; + protected array $original = []; /** * Once you put at least one string in this array, only * the attributes specified here will be changed * with the setAttributes method. * - * @var array + * @var string[] */ - protected $fillable = []; + protected array $fillable = []; /** * The attributes that are not mass assignable. The opposite * to the fillable array;. * - * @var array + * @var string[] */ - protected $guarded = []; + protected array $guarded = []; /** * Check if model should mutate attributes checking * the existence of a specific method on model * class. Default is false. - * - * @var bool */ - public $mutable = false; + public bool $mutable = false; /** * Attributes that are cast to another types when fetched from database. @@ -64,10 +62,8 @@ trait HasLegacyAttributesTrait * Get an attribute from the model. * * @param string $key the attribute to be accessed - * - * @return mixed */ - public function getAttribute(string $key) + public function getAttribute(string $key): mixed { $inAttributes = array_key_exists($key, $this->attributes); @@ -79,17 +75,19 @@ public function getAttribute(string $key) if ($inAttributes) { return $this->attributes[$key]; - } elseif ('attributes' == $key) { + } + + if ('attributes' == $key) { return $this->attributes; } + + return null; } /** * Get all attributes from the model. - * - * @return mixed */ - public function getAttributes() + public function getAttributes(): array { return $this->attributes; } @@ -122,7 +120,7 @@ public function fill(array $input, bool $force = false): HasAttributesInterface * * @param string $key name of the attribute to be unset */ - public function cleanAttribute(string $key) + public function cleanAttribute(string $key): void { unset($this->attributes[$key]); } @@ -133,7 +131,7 @@ public function cleanAttribute(string $key) * @param string $key name of the attribute to be set * @param mixed $value value to be set */ - public function setAttribute(string $key, $value) + public function setAttribute(string $key, mixed $value): void { if ($casterName = $this->casts[$key] ?? null) { $caster = CastResolver::resolve($casterName); @@ -146,7 +144,7 @@ public function setAttribute(string $key, $value) /** * Get original attributes. */ - public function originalAttributes() + public function originalAttributes(): array { return $this->original; } @@ -158,7 +156,7 @@ public function originalAttributes() * Ideally should be called once right after retrieving data from * the database. */ - public function syncOriginalAttributes() + public function syncOriginalAttributes(): void { $this->original = $this->attributes; } @@ -166,12 +164,10 @@ public function syncOriginalAttributes() /** * Verify if model has a mutator method defined. * - * @param mixed $key attribute name + * @param string $key attribute name * @param mixed $prefix method prefix to be used - * - * @return bool */ - protected function hasMutatorMethod($key, $prefix) + protected function hasMutatorMethod(string $key, mixed $prefix): bool { $method = $this->buildMutatorMethod($key, $prefix); @@ -181,22 +177,20 @@ protected function hasMutatorMethod($key, $prefix) /** * Create mutator method pattern. * - * @param mixed $key attribute name + * @param string $key attribute name * @param mixed $prefix method prefix to be used * * @return string */ - protected function buildMutatorMethod($key, $prefix) + protected function buildMutatorMethod(string $key, mixed $prefix): string { return $prefix.ucfirst($key).'Attribute'; } /** * Returns the model instance as an Array. - * - * @return array */ - public function toArray() + public function toArray(): array { return $this->getAttributes(); } @@ -204,11 +198,9 @@ public function toArray() /** * Dynamically retrieve attributes on the model. * - * @param mixed $key name of the attribute - * - * @return mixed + * @param string $key name of the attribute */ - public function __get($key) + public function __get(string $key): mixed { if ($this->mutable && $this->hasMutatorMethod($key, 'get')) { return $this->{$this->buildMutatorMethod($key, 'get')}(); @@ -220,10 +212,10 @@ public function __get($key) /** * Dynamically set attributes on the model. * - * @param mixed $key attribute name + * @param string $key attribute name * @param mixed $value value to be set */ - public function __set($key, $value) + public function __set(string $key, mixed $value): void { if ($this->mutable && $this->hasMutatorMethod($key, 'set')) { $value = $this->{$this->buildMutatorMethod($key, 'set')}($value); @@ -236,10 +228,8 @@ public function __set($key, $value) * Determine if an attribute exists on the model. * * @param mixed $key attribute name - * - * @return bool */ - public function __isset($key) + public function __isset(mixed $key):bool { return !is_null($this->{$key}); } @@ -249,7 +239,7 @@ public function __isset($key) * * @param mixed $key attribute name */ - public function __unset($key) + public function __unset(string $key):void { unset($this->attributes[$key]); } @@ -265,7 +255,7 @@ public function hasDocumentAttribute(string $key): bool return $this->hasAttribute($key); } - public function getDocumentAttribute(string $key) + public function getDocumentAttribute(string $key): mixed { return $this->getAttribute($key); } @@ -284,12 +274,12 @@ public function cleanDocumentAttribute(string $key): void } } - public function setDocumentAttribute(string $key, $value): void + public function setDocumentAttribute(string $key, mixed $value): void { $this->setAttribute($key, $value); } - public function syncOriginalDocumentAttributes() + public function syncOriginalDocumentAttributes(): void { $this->syncOriginalAttributes(); } diff --git a/src/Model/Relations/AbstractRelation.php b/src/Model/Relations/AbstractRelation.php index a6379990..e45e249d 100644 --- a/src/Model/Relations/AbstractRelation.php +++ b/src/Model/Relations/AbstractRelation.php @@ -6,58 +6,31 @@ abstract class AbstractRelation implements RelationInterface { - /** - * @var ModelInterface - */ - protected $parent; - - /** - * @var string - */ - protected $model; - - /** - * @var string - */ - protected $field; - - /** - * @var bool - */ - protected $pristine = false; + protected bool $pristine = false; /** * Cached results. - * - * @var mixed */ - protected $results; + protected mixed $results; - /** - * @var string - */ - protected $key = '_id'; + protected string $key = '_id'; - public function __construct(ModelInterface $parent, string $model, string $field) - { - $this->parent = $parent; - $this->model = $model; - $this->field = $field; + public function __construct( + protected ModelInterface $parent, + protected string $model, + protected string $field + ){ } /** * Retrieve Relation Results. - * - * @return mixed */ - abstract public function get(); + abstract public function get(): mixed; /** * Retrieve cached Relation Results. - * - * @return mixed */ - public function &getResults() + public function &getResults(): mixed { if (!$this->pristine()) { $this->results = $this->get(); @@ -77,10 +50,8 @@ protected function pristine(): bool * a new key will be generated and set on the model (while still returning it). * * @param ModelInterface $model the object that the key will be retrieved from - * - * @return ObjectId|mixed */ - protected function getKey(ModelInterface $model) + protected function getKey(ModelInterface $model): mixed { if (!$model->{$this->key}) { $model->{$this->key} = new ObjectId(); diff --git a/src/Model/Relations/EmbedsMany.php b/src/Model/Relations/EmbedsMany.php index fdfe015a..b083d011 100644 --- a/src/Model/Relations/EmbedsMany.php +++ b/src/Model/Relations/EmbedsMany.php @@ -34,8 +34,6 @@ public function addMany(array $entities): void /** * Replace embedded documents. - * - * @param array $entities */ public function replace(array $entities): void { @@ -69,10 +67,7 @@ public function removeAll(): void $this->pristine = false; } - /** - * @return EmbeddedCursor - */ - public function get() + public function get(): EmbeddedCursor { $items = $this->parent->{$this->field} ?? []; diff --git a/src/Model/Relations/EmbedsOne.php b/src/Model/Relations/EmbedsOne.php index 1a18d864..ce6831f7 100644 --- a/src/Model/Relations/EmbedsOne.php +++ b/src/Model/Relations/EmbedsOne.php @@ -17,10 +17,7 @@ public function remove(): void $this->pristine = false; } - /** - * @return ModelInterface|null - */ - public function get() + public function get(): ?ModelInterface { return $this->parent->{$this->field}; } diff --git a/src/Model/Relations/ReferencesMany.php b/src/Model/Relations/ReferencesMany.php index 12247b02..7919980c 100644 --- a/src/Model/Relations/ReferencesMany.php +++ b/src/Model/Relations/ReferencesMany.php @@ -3,15 +3,13 @@ use MongoDB\BSON\ObjectId; use Mongolid\Container\Container; +use Mongolid\Cursor\CursorInterface; use Mongolid\Model\ModelInterface; use Mongolid\Util\ObjectIdUtils; class ReferencesMany extends AbstractRelation { - /** - * @var ModelInterface - */ - protected $modelInstance; + protected ModelInterface $modelInstance; public function __construct(ModelInterface $parent, string $model, string $field, string $key) { @@ -29,10 +27,8 @@ public function attach(ModelInterface $model): void $referencedKey = $this->getKey($model); $fieldValue = (array) $this->parent->{$this->field}; - foreach ($fieldValue as $key) { - if ($key == $referencedKey) { - return; - } + if (in_array($referencedKey, $fieldValue)) { + return; } $fieldValue[] = $referencedKey; @@ -89,7 +85,7 @@ public function detachAll(): void $this->pristine = false; } - public function get() + public function get(): CursorInterface { $referencedKeys = (array) $this->parent->{$this->field}; diff --git a/src/Model/Relations/ReferencesOne.php b/src/Model/Relations/ReferencesOne.php index 2fb017ee..f689d889 100644 --- a/src/Model/Relations/ReferencesOne.php +++ b/src/Model/Relations/ReferencesOne.php @@ -8,10 +8,7 @@ class ReferencesOne extends AbstractRelation { - /** - * @var ModelInterface - */ - protected $modelInstance; + protected ModelInterface $modelInstance; public function __construct(ModelInterface $parent, string $model, string $field, string $key) { @@ -32,7 +29,7 @@ public function detach(): void $this->pristine = false; } - public function get() + public function get(): ?ModelInterface { if (!$referencedKey = $this->parent->{$this->field}) { return null; diff --git a/src/Model/Relations/RelationInterface.php b/src/Model/Relations/RelationInterface.php index 425e3ea9..789b1621 100644 --- a/src/Model/Relations/RelationInterface.php +++ b/src/Model/Relations/RelationInterface.php @@ -5,8 +5,6 @@ interface RelationInterface { /** * Get the results of the relation. - * - * @return mixed */ - public function getResults(); + public function getResults(): mixed; } From eb7ab112e0f218314b7e4b489983798f429da5fe Mon Sep 17 00:00:00 2001 From: JoaoFerrazfs Date: Wed, 2 Oct 2024 10:42:25 -0300 Subject: [PATCH 2/2] refactor: changed interfaces and tests --- src/Model/HasAttributesInterface.php | 4 +-- tests/Stubs/EmbeddedUser.php | 12 +++------ tests/Stubs/Legacy/LegacyRecordUser.php | 17 +++++------- tests/Stubs/Legacy/Product.php | 9 +++++-- tests/Stubs/Legacy/Sku.php | 6 ++++- tests/Stubs/PolymorphedReferencedUser.php | 5 +--- tests/Stubs/Price.php | 5 +--- tests/Stubs/Product.php | 5 +--- tests/Stubs/ReferencedUser.php | 27 +++++++------------ tests/Stubs/ReplaceCollectionModel.php | 15 +++-------- tests/Unit/Model/AbstractModelTest.php | 22 ++++----------- tests/Unit/Model/HasAttributesTraitTest.php | 7 ++--- .../Unit/Model/HasLegacyRelationTraitTest.php | 5 ++-- tests/Unit/Query/BuilderTest.php | 10 ++----- 14 files changed, 50 insertions(+), 99 deletions(-) diff --git a/src/Model/HasAttributesInterface.php b/src/Model/HasAttributesInterface.php index 1b166ba3..6411b6a5 100644 --- a/src/Model/HasAttributesInterface.php +++ b/src/Model/HasAttributesInterface.php @@ -43,7 +43,7 @@ public function cleanDocumentAttribute(string $key): void; * @param string $key name of the attribute to be set * @param mixed $value value to be set */ - public function setDocumentAttribute(string $key, mixed $value): mixed; + public function setDocumentAttribute(string $key, mixed $value): void; /** * Stores original attributes from actual data from attributes @@ -52,7 +52,7 @@ public function setDocumentAttribute(string $key, mixed $value): mixed; * Ideally should be called once right after retrieving data from * the database. */ - public function syncOriginalDocumentAttributes(): mixed; + public function syncOriginalDocumentAttributes(): void; /** * Retrieve original attributes. diff --git a/tests/Stubs/EmbeddedUser.php b/tests/Stubs/EmbeddedUser.php index 0d82cef4..e6fb6d56 100644 --- a/tests/Stubs/EmbeddedUser.php +++ b/tests/Stubs/EmbeddedUser.php @@ -5,15 +5,9 @@ class EmbeddedUser extends AbstractModel { - /** - * @var string - */ - protected $collection = 'users'; - - /** - * @var bool - */ - protected $timestamps = true; + protected ?string $collection = 'users'; + + protected bool $timestamps = true; public function parent() { diff --git a/tests/Stubs/Legacy/LegacyRecordUser.php b/tests/Stubs/Legacy/LegacyRecordUser.php index 3265e86c..e36792a1 100644 --- a/tests/Stubs/Legacy/LegacyRecordUser.php +++ b/tests/Stubs/Legacy/LegacyRecordUser.php @@ -1,6 +1,7 @@ embedsMany(LegacyRecordUser::class, 'siblings'); } - public function grandsons() + public function grandsons(): array { return $this->referencesMany(LegacyRecordUser::class, 'grandsons'); } - public function setSecretAttribute($value) + public function setSecretAttribute($value): string { return 'password_override'; } diff --git a/tests/Stubs/Legacy/Product.php b/tests/Stubs/Legacy/Product.php index 71ab93fe..b26129bb 100644 --- a/tests/Stubs/Legacy/Product.php +++ b/tests/Stubs/Legacy/Product.php @@ -1,6 +1,8 @@ referencesOne(Price::class, '_id'); } - public function skus() + public function skus(): CursorInterface { return $this->embedsMany(Sku::class, 'skus'); } diff --git a/tests/Stubs/Legacy/Sku.php b/tests/Stubs/Legacy/Sku.php index a680181c..f7b9825c 100644 --- a/tests/Stubs/Legacy/Sku.php +++ b/tests/Stubs/Legacy/Sku.php @@ -1,11 +1,15 @@ referencesOne(Shop::class, 'shop_id'); } diff --git a/tests/Stubs/PolymorphedReferencedUser.php b/tests/Stubs/PolymorphedReferencedUser.php index d6a0c8a0..e3871493 100644 --- a/tests/Stubs/PolymorphedReferencedUser.php +++ b/tests/Stubs/PolymorphedReferencedUser.php @@ -3,10 +3,7 @@ class PolymorphedReferencedUser extends ReferencedUser { - /** - * {@inheritdoc} - */ - protected $fillable = [ + protected array $fillable = [ 'type', 'new_field', ]; diff --git a/tests/Stubs/Price.php b/tests/Stubs/Price.php index e079222a..c44aec57 100644 --- a/tests/Stubs/Price.php +++ b/tests/Stubs/Price.php @@ -5,8 +5,5 @@ class Price extends AbstractModel { - /** - * @var string - */ - protected $collection = 'prices'; + protected ?string $collection = 'prices'; } diff --git a/tests/Stubs/Product.php b/tests/Stubs/Product.php index f905b581..96b5b8c8 100644 --- a/tests/Stubs/Product.php +++ b/tests/Stubs/Product.php @@ -5,8 +5,5 @@ class Product extends AbstractModel { - /** - * @var string - */ - protected $collection = 'products'; + protected ?string $collection = 'products'; } diff --git a/tests/Stubs/ReferencedUser.php b/tests/Stubs/ReferencedUser.php index 71cc5a04..ad693a7f 100644 --- a/tests/Stubs/ReferencedUser.php +++ b/tests/Stubs/ReferencedUser.php @@ -3,49 +3,42 @@ use Mongolid\Model\AbstractModel; use Mongolid\Model\PolymorphableModelInterface; +use Mongolid\Model\Relations\ReferencesMany; +use Mongolid\Model\Relations\ReferencesOne; class ReferencedUser extends AbstractModel implements PolymorphableModelInterface { - /** - * @var string - */ - protected $collection = 'users'; + protected ?string $collection = 'users'; - /** - * @var bool - */ - protected $timestamps = false; + protected bool $timestamps = false; - /** - * {@inheritdoc} - */ - protected $fillable = [ + protected array $fillable = [ 'type', 'exclusive', 'other_exclusive', ]; - public function parent() + public function parent(): ReferencesOne { return $this->referencesOne(ReferencedUser::class); } - public function siblings() + public function siblings(): ReferencesMany { return $this->referencesMany(ReferencedUser::class); } - public function son() + public function son(): ReferencesOne { return $this->referencesOne(ReferencedUser::class, 'arbitrary_field', 'code'); } - public function grandsons() + public function grandsons(): ReferencesMany { return $this->referencesMany(ReferencedUser::class, null, 'code'); } - public function invalid() + public function invalid(): string { return 'I am not a relation!'; } diff --git a/tests/Stubs/ReplaceCollectionModel.php b/tests/Stubs/ReplaceCollectionModel.php index c40ecf2a..9245b3d0 100644 --- a/tests/Stubs/ReplaceCollectionModel.php +++ b/tests/Stubs/ReplaceCollectionModel.php @@ -6,20 +6,11 @@ class ReplaceCollectionModel extends AbstractModel { - /** - * {@inheritdoc} - */ - protected $timestamps = false; + protected bool $timestamps = false; - /** - * {@inheritdoc} - */ - protected $collection = 'models'; + protected ?string $collection = 'models'; - /** - * @var Collection - */ - protected $rawCollection; + protected Collection $rawCollection; public function setCollection(Collection $collection): void { diff --git a/tests/Unit/Model/AbstractModelTest.php b/tests/Unit/Model/AbstractModelTest.php index 4eb4b280..a6e2a71f 100644 --- a/tests/Unit/Model/AbstractModelTest.php +++ b/tests/Unit/Model/AbstractModelTest.php @@ -29,12 +29,9 @@ protected function setUp(): void parent::setUp(); $this->model = new class() extends AbstractModel { - /** - * {@inheritdoc} - */ - protected $collection = 'mongolid'; + protected ?string $collection = 'mongolid'; - public function unsetCollection() + public function unsetCollection(): void { unset($this->collection); } @@ -482,10 +479,7 @@ public function testShouldCheckIfMutatedAttributeIsSet(): void // Set $model = new class() extends AbstractModel { - /** - * {@inheritdoc} - */ - public $mutable = true; + public bool $mutable = true; public function getNameDocumentAttribute(): string { @@ -524,10 +518,7 @@ public function testShouldGetAttributeFromMutator(): void // Set $model = new class() extends AbstractModel { - /** - * {@inheritdoc} - */ - public $mutable = true; + public bool $mutable = true; public function getShortNameDocumentAttribute(): string { @@ -571,10 +562,7 @@ public function testShouldSetAttributeFromMutator(): void // Set $model = new class() extends AbstractModel { - /** - * {@inheritdoc} - */ - protected $mutable = true; + protected bool $mutable = true; public function setShortNameDocumentAttribute($value): string { diff --git a/tests/Unit/Model/HasAttributesTraitTest.php b/tests/Unit/Model/HasAttributesTraitTest.php index 87955de4..29faa979 100644 --- a/tests/Unit/Model/HasAttributesTraitTest.php +++ b/tests/Unit/Model/HasAttributesTraitTest.php @@ -368,12 +368,9 @@ public function testShouldCheckIfMutatedAttributeIsSet(): void // Set $model = new class() extends AbstractModel { - /** - * {@inheritdoc} - */ - public $mutable = true; + public bool $mutable = true; - public function getNameDocumentAttribute() + public function getNameDocumentAttribute(): string { return 'John'; } diff --git a/tests/Unit/Model/HasLegacyRelationTraitTest.php b/tests/Unit/Model/HasLegacyRelationTraitTest.php index c02c5725..0ccd73ef 100644 --- a/tests/Unit/Model/HasLegacyRelationTraitTest.php +++ b/tests/Unit/Model/HasLegacyRelationTraitTest.php @@ -19,7 +19,6 @@ public function testReferenceOneShouldNotHitCache($fieldValue, array $expectedQu // Set $model = new Product(); $model->_id = $fieldValue; - $expected = new Price(); $priceModel = $this->instance(Price::class, m::mock(Price::class)->makePartial()); $cacheComponent = $this->instance( CacheComponentInterface::class, @@ -33,13 +32,13 @@ public function testReferenceOneShouldNotHitCache($fieldValue, array $expectedQu $priceModel->expects('first') ->with($expectedQuery, [], true) - ->andReturn($expected); + ->andReturnSelf(); // Actions $result = $model->price(); // Assertions - $this->assertSame($expected, $result); + $this->assertSame($priceModel, $result); } /** diff --git a/tests/Unit/Query/BuilderTest.php b/tests/Unit/Query/BuilderTest.php index 413e995a..031dd55a 100644 --- a/tests/Unit/Query/BuilderTest.php +++ b/tests/Unit/Query/BuilderTest.php @@ -245,18 +245,12 @@ public function testShouldUpdateUnsettingFields(): void $model = new class() extends ReplaceCollectionModel { - /** - * {@inheritdoc} - */ - public $fillable = [ + public array $fillable = [ 'name', 'unchanged', ]; - /** - * {@inheritdoc} - */ - protected $dynamic = false; + protected bool $dynamic = false; }; $collection = m::mock(Collection::class); $operationResult = m::mock();