Skip to content

Commit

Permalink
Merge pull request #245 from leroy-merlin-br/refactor/change-types-an…
Browse files Browse the repository at this point in the history
…d-annotations

refactor: changed types and annotations
  • Loading branch information
JoaoFerrazfs authored Oct 9, 2024
2 parents b53f076 + f55871a commit d40eb2e
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/Model/AttributesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AttributesService
/**
* The model's attributes.
*
* @var string[]
* @var array<string,mixed>
*/
private array $attributes = [];

Expand Down Expand Up @@ -136,7 +136,7 @@ public function cleanDocumentAttribute(string $key): void
}
}

public function setDocumentAttribute(string $key, $value): void
public function setDocumentAttribute(string $key, mixed $value): void
{
if ($this->mutable && $this->hasMutatorMethod($key, 'set')) {
$value = $this->{$this->buildMutatorMethod($key, 'set')}($value);
Expand Down
9 changes: 2 additions & 7 deletions src/Model/DocumentEmbedder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public function embed(mixed $parent, string $field, mixed &$entity): bool
* @param mixed $parent the object where the $entity will be removed
* @param string $field name of the field of the object where the document is
* @param mixed $entity entity that will be removed from $parent
*
* @return bool Success
*/
public function unembed(mixed $parent, string $field, mixed &$entity): bool
{
Expand Down Expand Up @@ -72,10 +70,8 @@ public function attach(mixed $parent, string $field, mixed &$entity): bool
$fieldValue = (array) $parent->$field;
$newId = $this->getId($entity);

foreach ($fieldValue as $id) {
if ($id == $newId) {
return true;
}
if (in_array($newId, $fieldValue)) {
return true;
}

$fieldValue[] = $newId;
Expand Down Expand Up @@ -114,7 +110,6 @@ public function detach(mixed $parent, string $field, mixed &$entity): bool
* _id will be generated and set on the object (while still returning it).
*
* @param mixed $object the object|array that the _id will be retrieved from
*
*/
protected function getId(mixed &$object): mixed
{
Expand Down
6 changes: 2 additions & 4 deletions src/Model/HasLegacyAttributesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ protected function hasMutatorMethod(string $key, mixed $prefix): bool
*
* @param string $key attribute name
* @param mixed $prefix method prefix to be used
*
* @return string
*/
protected function buildMutatorMethod(string $key, mixed $prefix): string
{
Expand Down Expand Up @@ -229,7 +227,7 @@ public function __set(string $key, mixed $value): void
*
* @param mixed $key attribute name
*/
public function __isset(mixed $key):bool
public function __isset(mixed $key): bool
{
return !is_null($this->{$key});
}
Expand All @@ -239,7 +237,7 @@ public function __isset(mixed $key):bool
*
* @param mixed $key attribute name
*/
public function __unset(string $key):void
public function __unset(string $key): void
{
unset($this->attributes[$key]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Relations/AbstractRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(
protected ModelInterface $parent,
protected string $model,
protected string $field
){
) {
}

/**
Expand Down
16 changes: 7 additions & 9 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
* Connection that is going to be used to interact with the database.
*/
protected Connection $connection
){
) {
}

/**
Expand Down Expand Up @@ -277,17 +277,17 @@ public function first(ModelInterface $model, mixed $query = [], array $projectio
* @param array $projection fields to project in MongoDB query
* @param boolean $useCache retrieves the first through a CacheableCursor
*
* @throws ModelNotFoundException If no model was found
* @return ModelInterface|null|array
*
* @return ModelInterface|null
* @throws ModelNotFoundException|NoCollectionNameException If no model was found
*/
public function firstOrFail(ModelInterface $model, mixed $query = [], array $projection = [], bool $useCache = false): mixed
{
if ($result = $this->first($model, $query, $projection, $useCache)) {
return $result;
}

throw (new ModelNotFoundException())->setModel(get_class($model));
throw (new ModelNotFoundException())->setModel($model::class);
}

public function withoutSoftDelete(): self
Expand All @@ -311,7 +311,9 @@ protected function fireEvent(string $event, ModelInterface $model, bool $halt =
{
$event = "mongolid.{$event}: ".$model::class;

$this->eventService ?: $this->eventService = Container::make(EventTriggerService::class);
if (!$this->eventService) {
$this->eventService = Container::make(EventTriggerService::class);
}

return $this->eventService->fire($event, $model, $halt);
}
Expand All @@ -336,8 +338,6 @@ protected function fireEvent(string $event, ModelInterface $model, bool $halt =
* @param array $fields fields to project
*
* @throws InvalidArgumentException If the given $fields are not a valid projection
*
* @return array
*/
protected function prepareProjection(array $fields): array
{
Expand Down Expand Up @@ -420,8 +420,6 @@ private function calculateChanges(array &$changes, array $newData, array $oldDat
*
* @param array $defaultOptions default options array
* @param array $toMergeOptions to merge options array
*
* @return array
*/
private function mergeOptions(array $defaultOptions = [], array $toMergeOptions = []): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Query/BulkWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BulkWrite

public function __construct(
private ModelInterface $model
){
) {
}

public function isEmpty(): bool
Expand Down
5 changes: 2 additions & 3 deletions src/Query/EagerLoader/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ class Cache
use CacheKeyGeneratorTrait;

public function __construct(
private
CacheComponentInterface $cacheComponent
){
private CacheComponentInterface $cacheComponent
) {
}

public function cache(array $eagerLoadedModel): void
Expand Down
2 changes: 1 addition & 1 deletion src/Query/EagerLoader/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(
* from the models passed by.
*/
private array $relatedModels
){
) {
}

/**
Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/DataMapper/DataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,7 @@ public function testDatabaseOperationsShouldBailOutIfTheEventHandlerReturnsFalse
string $operation,
string $dbOperation,
string $eventName
): void
{
): void {
// Arrange
$connection = m::mock(Connection::class);
$mapper = m::mock(DataMapper::class . '[parseToDocument,getCollection]', [$connection]);
Expand Down Expand Up @@ -867,7 +866,7 @@ public function testShouldGetFirstTroughACacheableCursorProjectingFields(): void
$this->assertEquals($entity, $result);
}

public function testShouldParseObjectToDocumentAndPutResultingIdIntoTheGivenObject()
public function testShouldParseObjectToDocumentAndPutResultingIdIntoTheGivenObject(): void
{
// Arrange
$connection = m::mock(Connection::class);
Expand Down
28 changes: 11 additions & 17 deletions tests/Unit/LegacyRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@

class LegacyRecordTest extends TestCase
{
/**
* @var LegacyRecord
*/
protected $entity;
protected LegacyRecord $entity;

/**
* {@inheritdoc}
*/
public function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -159,7 +153,7 @@ public function testShouldGetWithWhereQuery(): void
$cursor = m::mock(CursorInterface::class);

// Act
Container::instance(get_class($entity), $entity);
Container::instance($entity::class, $entity);

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);
Expand All @@ -182,7 +176,7 @@ public function testShouldGetAll(): void
$cursor = m::mock(CursorInterface::class);

// Act
Container::instance(get_class($entity), $entity);
Container::instance($entity::class, $entity);

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);
Expand All @@ -205,7 +199,7 @@ public function testShouldGetFirstWithQuery(): void
$dataMapper = m::mock();

// Act
Container::instance(get_class($entity), $entity);
Container::instance($entity::class, $entity);

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);
Expand All @@ -229,7 +223,7 @@ public function testShouldGetFirstOrFail(): void
$dataMapper = m::mock();

// Act
Container::instance(get_class($entity), $entity);
Container::instance($entity::class, $entity);

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);
Expand All @@ -252,7 +246,7 @@ public function testShouldGetFirstOrNewAndReturnExistingModel(): void
$dataMapper = m::mock();

// Act
Container::instance(get_class($entity), $entity);
Container::instance($entity::class, $entity);

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);
Expand All @@ -275,7 +269,7 @@ public function testShouldGetFirstOrNewAndReturnNewModel(): void
$dataMapper = m::mock();

// Act
Container::instance(get_class($entity), $entity);
Container::instance($entity::class, $entity);

$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);
Expand Down Expand Up @@ -317,12 +311,12 @@ public function testShouldGetSchemaIfFieldsDescribesSchemaFields(): void
$this->assertEquals($fields, $result->fields);
$this->assertEquals($this->entity->dynamic, $result->dynamic);
$this->assertEquals($this->entity->getCollectionName(), $result->collection);
$this->assertEquals(get_class($this->entity), $result->entityClass);
$this->assertEquals($this->entity::class, $result->entityClass);
}

public function testShouldGetDataMapper()
public function testShouldGetDataMapper(): void
{
// Arrage
// Arrange
$entity = m::mock(LegacyRecord::class.'[getSchema]');
$schema = m::mock(Schema::class.'[]');

Expand Down Expand Up @@ -446,7 +440,7 @@ public function testShouldRefreshModels(): void
$this->setProtected($entity, 'collection', 'mongolid');
$entity->_id = $id;
$dataMapper = m::mock();
Container::instance(get_class($entity), $entity);
Container::instance($entity::class, $entity);

// Expectations
$entity->shouldReceive('getDataMapper')
Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public function testUpdateShouldCallInsertWhenObjectHasNoId(
int $writeConcern,
bool $shouldFireEventAfter,
bool $expected
):void {
): void {
// Set
$connection = m::mock(Connection::class);
$builder = new Builder($connection);
Expand Down Expand Up @@ -632,7 +632,6 @@ public function testShouldGetFirstWithQuery(): void
->with($preparedQuery, ['projection' => []])
->andReturn($model);


// Actions
$result = $builder->first($model, $query);

Expand Down Expand Up @@ -811,7 +810,7 @@ public function testShouldGetFirstProjectingFields(): void
/**
* @dataProvider getProjections
*/
public function testPrepareProjectionShouldConvertArray($data, $expectation): void
public function testPrepareProjectionShouldConvertArray(array $data, array $expectation): void
{
// Set
$connection = m::mock(Connection::class);
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/Schema/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,9 @@ public function testShouldRefreshUpdatedAtTimestamps(): void
// Arrange
$schema = new class extends Schema {
};
$value = new UTCDateTime(25);

// Assertion
$result = $schema->updatedAtTimestamp($value);
$result = $schema->updatedAtTimestamp();
$this->assertInstanceOf(UTCDateTime::class, $result);
$this->assertNotEquals(25000, (string) $result);
}
Expand Down

0 comments on commit d40eb2e

Please sign in to comment.