From 964301c7895bc1240874499f3e48b9e276bbd13c Mon Sep 17 00:00:00 2001 From: JoaoFerrazfs Date: Wed, 2 Oct 2024 16:56:21 -0300 Subject: [PATCH] refactor: added new reactor code standard --- src/Cursor/CacheableCursor.php | 6 ++-- src/Cursor/SchemaCacheableCursor.php | 6 ++-- src/DataMapper/SchemaMapper.php | 7 +++-- src/LegacyRecord.php | 29 ++++++++----------- src/Model/AbstractModel.php | 3 +- src/Model/AttributesService.php | 2 +- src/Model/Casts/DateTime/BaseDateTimeCast.php | 4 +-- src/Model/Casts/DateTime/DateTimeCast.php | 2 +- .../Casts/DateTime/ImmutableDateTimeCast.php | 2 +- src/Model/HasAttributesTrait.php | 2 +- src/Model/HasLegacyAttributesTrait.php | 9 ++++-- src/Model/HasLegacyRelationsTrait.php | 12 ++++---- src/Schema/Schema.php | 2 +- 13 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/Cursor/CacheableCursor.php b/src/Cursor/CacheableCursor.php index fbb3cb78..033ee871 100644 --- a/src/Cursor/CacheableCursor.php +++ b/src/Cursor/CacheableCursor.php @@ -52,10 +52,12 @@ class CacheableCursor extends Cursor protected function getCursor(): Iterator { // Returns original (non-cached) cursor - if ($this->ignoreCache || $this->position >= self::DOCUMENT_LIMIT) { + if ($this->ignoreCache) { + return $this->getOriginalCursor(); + } + if ($this->position >= self::DOCUMENT_LIMIT) { return $this->getOriginalCursor(); } - // Returns cached set of documents if ($this->documents) { return $this->documents; diff --git a/src/Cursor/SchemaCacheableCursor.php b/src/Cursor/SchemaCacheableCursor.php index 1d250618..0b1cb6c4 100644 --- a/src/Cursor/SchemaCacheableCursor.php +++ b/src/Cursor/SchemaCacheableCursor.php @@ -53,10 +53,12 @@ class SchemaCacheableCursor extends SchemaCursor protected function getCursor(): Iterator { // Returns original (non-cached) cursor - if ($this->ignoreCache || $this->position >= self::DOCUMENT_LIMIT) { + if ($this->ignoreCache) { + return $this->getOriginalCursor(); + } + if ($this->position >= self::DOCUMENT_LIMIT) { return $this->getOriginalCursor(); } - // Returns cached set of documents if ($this->documents) { return $this->documents; diff --git a/src/DataMapper/SchemaMapper.php b/src/DataMapper/SchemaMapper.php index eb631aad..eb1d4fe6 100644 --- a/src/DataMapper/SchemaMapper.php +++ b/src/DataMapper/SchemaMapper.php @@ -82,7 +82,10 @@ public function parseField(mixed $value, string $fieldType): mixed return $this->schema->$fieldType($value); } // Returns null or an empty array - if (null === $value || is_array($value) && empty($value)) { + if (null === $value) { + return $value; + } + if (is_array($value) && empty($value)) { return $value; } @@ -106,8 +109,6 @@ public function parseField(mixed $value, string $fieldType): mixed * * @param mixed $value value to be casted * @param string $type type to which the $value should be casted to - * - * @return mixed */ protected function cast(mixed $value, string $type): mixed { diff --git a/src/LegacyRecord.php b/src/LegacyRecord.php index 856a0d2f..fd5dc5e2 100644 --- a/src/LegacyRecord.php +++ b/src/LegacyRecord.php @@ -33,7 +33,7 @@ class LegacyRecord implements ModelInterface, HasSchemaInterface * * @var string */ - protected $collection = null; + protected $collection; /** * @see https://docs.mongodb.com/manual/reference/write-concern/ @@ -227,19 +227,19 @@ public static function firstOrNew($id) * * @return mixed */ - public function __call($method, $parameters) + public function __call(mixed $method, mixed $parameters) { $value = $parameters[0] ?? null; // Alias to attach - if ('attachTo' == substr($method, 0, 8)) { + if (str_starts_with($method, 'attachTo')) { $field = lcfirst(substr($method, 8)); return $this->attach($field, $value); } // Alias to embed - if ('embedTo' == substr($method, 0, 7)) { + if (str_starts_with($method, 'embedTo')) { $field = lcfirst(substr($method, 7)); return $this->embed($field, $value); @@ -248,7 +248,7 @@ public function __call($method, $parameters) throw new BadMethodCallException( sprintf( 'The following method can not be reached or does not exist: %s@%s', - get_class($this), + static::class, $method ) ); @@ -277,7 +277,7 @@ public function getCollectionName(): string throw new NoCollectionNameException(); } - return $this->collection ? $this->collection : $this->getSchema()->collection; + return $this->collection ?: $this->getSchema()->collection; } /** @@ -308,7 +308,7 @@ public function getSchema(): Schema } $schema = new DynamicSchema(); - $schema->entityClass = get_class($this); + $schema->entityClass = static::class; $schema->fields = $this->fields; $schema->dynamic = $this->dynamic; $schema->collection = $this->collection; @@ -319,16 +319,16 @@ public function getSchema(): Schema /** * Will check if the current value of $fields property is the name of a * Schema class and instantiate it if possible. - * - * @return Schema|null */ - protected function instantiateSchemaInFields() + protected function instantiateSchemaInFields(): ?Schema { if (is_string($this->fields)) { if (is_subclass_of($instance = Container::make($this->fields), Schema::class)) { return $instance; } } + + return null; } /** @@ -364,7 +364,7 @@ protected function execute(string $action) */ protected static function getDataMapperInstance() { - $instance = Container::make(get_called_class()); + $instance = Container::make(static::class); if (!$instance->getCollectionName()) { throw new NoCollectionNameException(); @@ -380,7 +380,6 @@ public function getCollection(): Collection } /** - * @return array|object * @throws BindingResolutionException */ public function bsonSerialize(): object|array @@ -389,10 +388,6 @@ public function bsonSerialize(): object|array ->map($this, array_merge($this->fillable, $this->guarded), $this->dynamic, $this->timestamps); } - /** - * @param array $data - * @return void - */ public function bsonUnserialize(array $data): void { $this->fill($data, true); @@ -405,6 +400,6 @@ public function bsonUnserialize(array $data): void */ public function fresh(): self { - return $this->first($this->_id); + return static::first($this->_id); } } diff --git a/src/Model/AbstractModel.php b/src/Model/AbstractModel.php index 81f197cd..3bb4c36b 100644 --- a/src/Model/AbstractModel.php +++ b/src/Model/AbstractModel.php @@ -171,11 +171,10 @@ public function delete(): bool /** * Query model on database to retrieve an updated version of its attributes. - * @return self */ public function fresh(): self { - return $this->first($this->_id); + return static::first($this->_id); } /** diff --git a/src/Model/AttributesService.php b/src/Model/AttributesService.php index 86b4e25e..eb8e6c69 100644 --- a/src/Model/AttributesService.php +++ b/src/Model/AttributesService.php @@ -77,7 +77,7 @@ public static function fill( if ($force || ((!$object->fillable || in_array($key, $object->fillable)) && !in_array($key, $object->guarded))) { if ($value instanceof stdClass) { - $value = json_decode(json_encode($value), true); // cast to array + $value = json_decode(json_encode($value, JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR); // cast to array } $object->setDocumentAttribute($key, $value); diff --git a/src/Model/Casts/DateTime/BaseDateTimeCast.php b/src/Model/Casts/DateTime/BaseDateTimeCast.php index 91d39e06..18d89700 100644 --- a/src/Model/Casts/DateTime/BaseDateTimeCast.php +++ b/src/Model/Casts/DateTime/BaseDateTimeCast.php @@ -10,12 +10,12 @@ abstract class BaseDateTimeCast implements CastInterface { /** - * @param UTCDateTime|null $value + * @param UTCDateTime $value */ abstract public function get(mixed $value): ?DateTimeInterface; /** - * @param DateTimeInterface|UTCDateTimeInterface|null $value + * @param DateTimeInterface|UTCDateTimeInterface $value */ public function set(mixed $value): UTCDateTime|null { diff --git a/src/Model/Casts/DateTime/DateTimeCast.php b/src/Model/Casts/DateTime/DateTimeCast.php index 1ba6fac9..522ad063 100644 --- a/src/Model/Casts/DateTime/DateTimeCast.php +++ b/src/Model/Casts/DateTime/DateTimeCast.php @@ -9,7 +9,7 @@ class DateTimeCast extends BaseDateTimeCast { /** - * @param UTCDateTime|null $value + * @param UTCDateTime $value */ public function get(mixed $value): ?DateTime { diff --git a/src/Model/Casts/DateTime/ImmutableDateTimeCast.php b/src/Model/Casts/DateTime/ImmutableDateTimeCast.php index 793659ab..fb02aa0c 100644 --- a/src/Model/Casts/DateTime/ImmutableDateTimeCast.php +++ b/src/Model/Casts/DateTime/ImmutableDateTimeCast.php @@ -9,7 +9,7 @@ class ImmutableDateTimeCast extends BaseDateTimeCast { /** - * @param UTCDateTime|null $value + * @param UTCDateTime $value */ public function get(mixed $value): ?DateTimeImmutable { diff --git a/src/Model/HasAttributesTrait.php b/src/Model/HasAttributesTrait.php index 4387056b..d68f25ca 100644 --- a/src/Model/HasAttributesTrait.php +++ b/src/Model/HasAttributesTrait.php @@ -98,7 +98,7 @@ public static function fill( if ($force || ((!$object->fillable || in_array($key, $object->fillable)) && !in_array($key, $object->guarded))) { if ($value instanceof stdClass) { - $value = json_decode(json_encode($value), true); // cast to array + $value = json_decode(json_encode($value, JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR); // cast to array } $object->setDocumentAttribute($key, $value); diff --git a/src/Model/HasLegacyAttributesTrait.php b/src/Model/HasLegacyAttributesTrait.php index c4f279e0..95bea0a0 100644 --- a/src/Model/HasLegacyAttributesTrait.php +++ b/src/Model/HasLegacyAttributesTrait.php @@ -106,10 +106,13 @@ public function fill(array $input, bool $force = false): HasAttributesInterface continue; } - - if ((empty($this->fillable) || in_array($key, $this->fillable)) && !in_array($key, $this->guarded)) { - $this->setAttribute($key, $value); + if (!(empty($this->fillable) || in_array($key, $this->fillable))) { + continue; + } + if (in_array($key, $this->guarded)) { + continue; } + $this->setAttribute($key, $value); } return $this; diff --git a/src/Model/HasLegacyRelationsTrait.php b/src/Model/HasLegacyRelationsTrait.php index 82cc89d6..506b682a 100644 --- a/src/Model/HasLegacyRelationsTrait.php +++ b/src/Model/HasLegacyRelationsTrait.php @@ -96,10 +96,8 @@ protected function referencesMany(string $entity, string $field, bool $cacheable * * @param string $entity class of the entity or of the schema of the entity * @param string $field field where the embedded document is stored - * - * @return LegacyRecord|Schema|null */ - protected function embedsOne(string $entity, string $field) + protected function embedsOne(string $entity, string $field): \Mongolid\LegacyRecord|\Mongolid\Schema\Schema|null { if (is_subclass_of($entity, Schema::class)) { $entity = (new $entity())->entityClass; @@ -144,7 +142,7 @@ protected function embedsMany(string $entity, string $field) * @param string $field field to where the $obj will be embedded * @param mixed $obj document or model instance */ - public function embed(string $field, &$obj) + public function embed(string $field, mixed &$obj): void { $embedder = Container::make(DocumentEmbedder::class); $embedder->embed($this, $field, $obj); @@ -157,7 +155,7 @@ public function embed(string $field, &$obj) * @param string $field name of the field where the $obj is embeded * @param mixed $obj document, model instance or _id */ - public function unembed(string $field, &$obj) + public function unembed(string $field, mixed &$obj): void { $embedder = Container::make(DocumentEmbedder::class); $embedder->unembed($this, $field, $obj); @@ -170,7 +168,7 @@ public function unembed(string $field, &$obj) * @param string $field name of the field where the reference will be stored * @param mixed $obj document, model instance or _id to be referenced */ - public function attach(string $field, &$obj) + public function attach(string $field, mixed &$obj): void { $embedder = Container::make(DocumentEmbedder::class); $embedder->attach($this, $field, $obj); @@ -183,7 +181,7 @@ public function attach(string $field, &$obj) * @param string $field field where the reference is stored * @param mixed $obj document, model instance or _id that have been referenced by $field */ - public function detach(string $field, &$obj) + public function detach(string $field, mixed &$obj): void { $embedder = Container::make(DocumentEmbedder::class); $embedder->detach($this, $field, $obj); diff --git a/src/Schema/Schema.php b/src/Schema/Schema.php index 002ae4c2..c3935cc1 100644 --- a/src/Schema/Schema.php +++ b/src/Schema/Schema.php @@ -93,7 +93,7 @@ public function sequence(?int $value = null): int /** * Prepares the field to be the datetime that the document has been created. * - * @param mixed|null $value value that will be evaluated + * @param mixed $value value that will be evaluated */ public function createdAtTimestamp(mixed $value): UTCDateTime {