Skip to content

Commit

Permalink
refactor: changed types and annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFerrazfs committed Oct 2, 2024
1 parent 8c47ed6 commit 9f0ace3
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 44 deletions.
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
12 changes: 5 additions & 7 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 @@ -287,7 +287,7 @@ public function firstOrFail(ModelInterface $model, mixed $query = [], array $pro
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 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
4 changes: 2 additions & 2 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 @@ -811,7 +811,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
5 changes: 2 additions & 3 deletions tests/Unit/Schema/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,11 @@ 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);
$this->assertNotEquals(25000, (string)$result);
}

/**
Expand Down

0 comments on commit 9f0ace3

Please sign in to comment.