Skip to content

Commit

Permalink
refactor: added new reactor code standard
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFerrazfs committed Oct 2, 2024
1 parent cd1df09 commit 964301c
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 43 deletions.
6 changes: 4 additions & 2 deletions src/Cursor/CacheableCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/Cursor/SchemaCacheableCursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/DataMapper/SchemaMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
{
Expand Down
29 changes: 12 additions & 17 deletions src/LegacyRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -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);
Expand All @@ -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
)
);
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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();
Expand All @@ -380,7 +380,6 @@ public function getCollection(): Collection
}

/**
* @return array|object
* @throws BindingResolutionException
*/
public function bsonSerialize(): object|array
Expand All @@ -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);
Expand All @@ -405,6 +400,6 @@ public function bsonUnserialize(array $data): void
*/
public function fresh(): self
{
return $this->first($this->_id);
return static::first($this->_id);
}
}
3 changes: 1 addition & 2 deletions src/Model/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Model/AttributesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Model/Casts/DateTime/BaseDateTimeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Casts/DateTime/DateTimeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class DateTimeCast extends BaseDateTimeCast
{
/**
* @param UTCDateTime|null $value
* @param UTCDateTime $value
*/
public function get(mixed $value): ?DateTime
{
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Casts/DateTime/ImmutableDateTimeCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class ImmutableDateTimeCast extends BaseDateTimeCast
{
/**
* @param UTCDateTime|null $value
* @param UTCDateTime $value
*/
public function get(mixed $value): ?DateTimeImmutable
{
Expand Down
2 changes: 1 addition & 1 deletion src/Model/HasAttributesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions src/Model/HasLegacyAttributesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 5 additions & 7 deletions src/Model/HasLegacyRelationsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 964301c

Please sign in to comment.