diff --git a/src/Model/DocumentEmbedder.php b/src/Model/DocumentEmbedder.php index dd7b0301..5df4052f 100644 --- a/src/Model/DocumentEmbedder.php +++ b/src/Model/DocumentEmbedder.php @@ -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 { @@ -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; @@ -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 { diff --git a/src/Model/HasLegacyAttributesTrait.php b/src/Model/HasLegacyAttributesTrait.php index f15dcf5b..c4f279e0 100644 --- a/src/Model/HasLegacyAttributesTrait.php +++ b/src/Model/HasLegacyAttributesTrait.php @@ -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 { @@ -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}); } @@ -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]); } diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 0c8f0a39..f28be041 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -32,7 +32,7 @@ public function __construct( * Connection that is going to be used to interact with the database. */ protected Connection $connection - ){ + ) { } /** @@ -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 @@ -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); } @@ -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 { @@ -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 { diff --git a/src/Query/BulkWrite.php b/src/Query/BulkWrite.php index 4da546d5..b972bb40 100644 --- a/src/Query/BulkWrite.php +++ b/src/Query/BulkWrite.php @@ -22,7 +22,7 @@ class BulkWrite public function __construct( private ModelInterface $model - ){ + ) { } public function isEmpty(): bool diff --git a/tests/Unit/DataMapper/DataMapperTest.php b/tests/Unit/DataMapper/DataMapperTest.php index 9337c497..dd299bee 100644 --- a/tests/Unit/DataMapper/DataMapperTest.php +++ b/tests/Unit/DataMapper/DataMapperTest.php @@ -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]); @@ -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); diff --git a/tests/Unit/LegacyRecordTest.php b/tests/Unit/LegacyRecordTest.php index 76008f0f..f1bb54da 100644 --- a/tests/Unit/LegacyRecordTest.php +++ b/tests/Unit/LegacyRecordTest.php @@ -16,14 +16,8 @@ class LegacyRecordTest extends TestCase { - /** - * @var LegacyRecord - */ - protected $entity; + protected LegacyRecord $entity; - /** - * {@inheritdoc} - */ public function setUp(): void { parent::setUp(); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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.'[]'); @@ -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') diff --git a/tests/Unit/Query/BuilderTest.php b/tests/Unit/Query/BuilderTest.php index 6c88572a..fd2cde67 100644 --- a/tests/Unit/Query/BuilderTest.php +++ b/tests/Unit/Query/BuilderTest.php @@ -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); @@ -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); diff --git a/tests/Unit/Schema/SchemaTest.php b/tests/Unit/Schema/SchemaTest.php index 4b003274..e678bc55 100644 --- a/tests/Unit/Schema/SchemaTest.php +++ b/tests/Unit/Schema/SchemaTest.php @@ -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); } /**