From fc4f7500a928bf99bff807d736716b41f6112efc Mon Sep 17 00:00:00 2001 From: JoaoFerrazfs Date: Tue, 1 Oct 2024 15:57:32 -0300 Subject: [PATCH] refactor: changed types and annotations --- src/DataMapper/DataMapper.php | 114 ++++++++------------- src/DataMapper/EntityAssembler.php | 18 ++-- src/DataMapper/SchemaMapper.php | 47 +++------ tests/Unit/DataMapper/DataMapperTest.php | 71 ++++++------- tests/Unit/DataMapper/SchemaMapperTest.php | 27 ++--- 5 files changed, 118 insertions(+), 159 deletions(-) diff --git a/src/DataMapper/DataMapper.php b/src/DataMapper/DataMapper.php index 8424118a..5ce983b4 100644 --- a/src/DataMapper/DataMapper.php +++ b/src/DataMapper/DataMapper.php @@ -7,7 +7,6 @@ use Mongolid\Connection\Connection; use Mongolid\Container\Container; use Mongolid\Cursor\CursorInterface; -use Mongolid\Cursor\EagerLoadedCursor; use Mongolid\Cursor\SchemaCacheableCursor; use Mongolid\Cursor\SchemaCursor; use Mongolid\Event\EventTriggerService; @@ -20,7 +19,7 @@ /** * The DataMapper class will abstract how an Entity is persisted and retrieved * from the database. - * The DataMapper will always use a Schema trough the SchemaMapper to parse the + * The DataMapper will always use a Schema through the SchemaMapper to parse the * document in and out of the database. */ class DataMapper implements HasSchemaInterface @@ -29,47 +28,35 @@ class DataMapper implements HasSchemaInterface /** * Name of the schema class to be used. - * - * @var string */ - public $schemaClass = Schema::class; + public string $schemaClass = Schema::class; /** * Schema object. Will be set after the $schemaClass. - * - * @var Schema - */ - protected $schema; - - /** - * Connections that are going to be used to interact with the database. - * - * @var Connection */ - protected $connection; + protected ?Schema $schema = null; /** * Have the responsibility of assembling the data coming from the database into actual entities. - * - * @var EntityAssembler */ - protected $assembler; + protected ?EntityAssembler $assembler = null; /** * In order to dispatch events when necessary. - * - * @var EventTriggerService */ - protected $eventService; + protected ?EventTriggerService $eventService = null; /** - * @var array + * @var array */ - private $pullNullValues = []; + private array $pullNullValues = []; - public function __construct(Connection $connection) - { - $this->connection = $connection; + public function __construct( + /** + * Connections that are going to be used to interact with the database. + */ + protected Connection $connection + ) { } /** @@ -83,7 +70,7 @@ public function __construct(Connection $connection) * * @return bool Success (but always false if write concern is Unacknowledged) */ - public function save($entity, array $options = []) + public function save(mixed $entity, array $options = []): bool { // If the "saving" event returns false we'll bail out of the save and return // false, indicating that the save failed. This gives an opportunities to @@ -125,7 +112,7 @@ public function save($entity, array $options = []) * * @return bool Success (but always false if write concern is Unacknowledged) */ - public function insert($entity, array $options = [], bool $fireEvents = true): bool + public function insert(mixed $entity, array $options = [], bool $fireEvents = true): bool { if ($fireEvents && false === $this->fireEvent('inserting', $entity, true)) { return false; @@ -163,7 +150,7 @@ public function insert($entity, array $options = [], bool $fireEvents = true): b * * @return bool Success (but always false if write concern is Unacknowledged) */ - public function update($entity, array $options = []): bool + public function update(mixed $entity, array $options = []): bool { if (false === $this->fireEvent('updating', $entity, true)) { return false; @@ -220,7 +207,7 @@ public function update($entity, array $options = []): bool * * @return bool Success (but always false if write concern is Unacknowledged) */ - public function delete($entity, array $options = []): bool + public function delete(mixed $entity, array $options = []): bool { if (false === $this->fireEvent('deleting', $entity, true)) { return false; @@ -253,7 +240,7 @@ public function delete($entity, array $options = []): bool * @param bool $cacheable retrieves a SchemaCacheableCursor instead */ public function where( - $query = [], + mixed $query = [], array $projection = [], bool $cacheable = false ): CursorInterface { @@ -301,10 +288,10 @@ public function all(): CursorInterface * @return mixed First document matching query as an $this->schema->entityClass object */ public function first( - $query = [], + mixed $query = [], array $projection = [], bool $cacheable = false - ) { + ): mixed { if ($cacheable) { return $this->where($query, $projection, true)->first(); } @@ -322,12 +309,10 @@ public function first( ); if (!$document) { - return; + return null; } - $model = $this->getAssembler()->assemble($document, $this->schema); - - return $model; + return $this->getAssembler()->assemble($document, $this->schema); } /** @@ -343,10 +328,10 @@ public function first( * @return mixed First document matching query as an $this->schema->entityClass object */ public function firstOrFail( - $query = [], + mixed $query = [], array $projection = [], bool $cacheable = false - ) { + ): mixed { if ($result = $this->first($query, $projection, $cacheable)) { return $result; } @@ -368,7 +353,7 @@ public function withoutSoftDelete(): self * * @return array Document */ - protected function parseToDocument($entity) + protected function parseToDocument(mixed $entity): array { $schemaMapper = $this->getSchemaMapper(); $parsedDocument = $schemaMapper->map($entity); @@ -384,10 +369,8 @@ protected function parseToDocument($entity) /** * Returns a SchemaMapper with the $schema or $schemaClass instance. - * - * @return SchemaMapper */ - protected function getSchemaMapper() + protected function getSchemaMapper(): SchemaMapper { if (!$this->schema) { $this->schema = Container::make($this->schemaClass); @@ -407,20 +390,16 @@ public function getCollection(): Collection 'typeMap' => ['array' => 'array', 'document' => 'array'] ]; - $collection = $this->connection + return $this->connection ->getClient() ->selectDatabase($database, $options) ->selectCollection($collectionName); - - return $collection; } /** * Retrieves an EntityAssembler instance. - * - * @return EntityAssembler */ - protected function getAssembler() + protected function getAssembler(): EntityAssembler { if (!$this->assembler) { $this->assembler = Container::make(EntityAssembler::class); @@ -438,11 +417,13 @@ protected function getAssembler() * * @return mixed event handler return */ - protected function fireEvent(string $event, $entity, bool $halt = false) + protected function fireEvent(string $event, mixed $entity, bool $halt = false): mixed { - $event = "mongolid.{$event}: ".get_class($entity); + $event = "mongolid.{$event}: ".$entity::class; - $this->eventService ? $this->eventService : $this->eventService = Container::make(EventTriggerService::class); + if (!$this->eventService) { + $this->eventService = Container::make(EventTriggerService::class); + } return $this->eventService->fire($event, $entity, $halt); } @@ -468,9 +449,9 @@ protected function fireEvent(string $event, $entity, bool $halt = false) * * @throws InvalidArgumentException if the given $fields are not a valid projection * - * @return array + * @return array */ - protected function prepareProjection(array $fields) + protected function prepareProjection(array $fields): array { $projection = []; foreach ($fields as $key => $value) { @@ -489,7 +470,7 @@ protected function prepareProjection(array $fields) if (is_int($key) && is_string($value)) { $key = $value; - if (0 === strpos($value, '-')) { + if (str_starts_with($value, '-')) { $key = substr($key, 1); $value = false; } else { @@ -518,20 +499,16 @@ protected function prepareProjection(array $fields) * * @param array $defaultOptions default options array * @param array $toMergeOptions to merge options array - * - * @return array */ - private function mergeOptions(array $defaultOptions = [], array $toMergeOptions = []) + private function mergeOptions(array $defaultOptions = [], array $toMergeOptions = []): array { return array_merge($defaultOptions, $toMergeOptions); } /** * Perform actions on object before firing the after event. - * - * @param mixed $entity */ - private function afterSuccess($entity) + private function afterSuccess(mixed $entity): void { if ($entity instanceof ModelInterface) { $entity->syncOriginalDocumentAttributes(); @@ -548,10 +525,8 @@ public function getSchema(): Schema /** * Set a Schema object that describes an Entity in MongoDB. - * - * @param Schema $schema */ - public function setSchema(Schema $schema) + public function setSchema(Schema $schema): void { $this->schema = $schema; } @@ -580,7 +555,7 @@ private function getUpdateData($model, array $data): array * @see https://jira.mongodb.org/browse/SERVER-1014 * @see https://github.com/bjori/mongo-php-transistor/blob/70f5af00795d67f4d5a8c397e831435814df9937/src/Transistor.php#L108 */ - private function calculateChanges(array &$changes, array $newData, array $oldData, string $keyfix = '') + private function calculateChanges(array &$changes, array $newData, array $oldData, string $keyfix = ''): void { foreach ($newData as $k => $v) { if (is_null($v)) { @@ -610,22 +585,19 @@ private function calculateChanges(array &$changes, array $newData, array $oldDat $this->pullNullValues[rtrim($keyfix, '.')] = null; } $changes['$unset']["{$keyfix}{$k}"] = ''; - continue; } } } private function filterNullValues(array $data): array { - $filtered = array_filter( + $filtered = array_filter( $data, - function ($value) { - return !is_null($value); - } + fn($value): bool => !is_null($value) ); if ($data == array_values($data)) { - $filtered = array_values($filtered); + return array_values($filtered); } return $filtered; diff --git a/src/DataMapper/EntityAssembler.php b/src/DataMapper/EntityAssembler.php index 9f420c9b..647ffd0e 100644 --- a/src/DataMapper/EntityAssembler.php +++ b/src/DataMapper/EntityAssembler.php @@ -25,10 +25,8 @@ class EntityAssembler * * @param array|object $document the attributes that will be used to compose the entity * @param Schema $schema schema that will be used to map each field - * - * @return mixed */ - public function assemble($document, Schema $schema) + public function assemble(array|object $document, Schema $schema): mixed { $entityClass = $schema->entityClass; $model = Container::make($entityClass); @@ -36,7 +34,7 @@ public function assemble($document, Schema $schema) foreach ($document as $field => $value) { $fieldType = $schema->fields[$field] ?? null; - if ($fieldType && 'schema.' == substr($fieldType, 0, 7)) { + if ($fieldType && str_starts_with($fieldType, 'schema.')) { $value = $this->assembleDocumentsRecursively($value, substr($fieldType, 7)); } @@ -58,12 +56,12 @@ public function assemble($document, Schema $schema) * * @return mixed the result of $entity->polymorph or the $entity itself */ - protected function morphingTime(ModelInterface $entity) + protected function morphingTime(ModelInterface $entity): mixed { if ($entity instanceof PolymorphableModelInterface) { $class = $entity->polymorph($entity->getDocumentAttributes()); - if ($class !== get_class($entity)) { + if ($class !== $entity::class) { $originalAttributes = $entity->getDocumentAttributes(); $entity = Container::make($class); $entity->fill($originalAttributes, true); @@ -80,7 +78,7 @@ protected function morphingTime(ModelInterface $entity) * * @return mixed the entity with original attributes */ - protected function prepareOriginalAttributes($entity) + protected function prepareOriginalAttributes(mixed $entity): mixed { if ($entity instanceof ModelInterface) { $entity->syncOriginalDocumentAttributes(); @@ -94,15 +92,13 @@ protected function prepareOriginalAttributes($entity) * * @param mixed $value a value of an embeded field containing entity data to be assembled * @param string $schemaClass the schemaClass to be used when assembling the entities within $value - * - * @return mixed */ - protected function assembleDocumentsRecursively($value, string $schemaClass) + protected function assembleDocumentsRecursively(mixed $value, string $schemaClass): ?array { $value = (array) $value; if (empty($value)) { - return; + return null; } $schema = Container::make($schemaClass); diff --git a/src/DataMapper/SchemaMapper.php b/src/DataMapper/SchemaMapper.php index f77eb9fc..eb631aad 100644 --- a/src/DataMapper/SchemaMapper.php +++ b/src/DataMapper/SchemaMapper.php @@ -16,13 +16,6 @@ */ class SchemaMapper { - /** - * The actual schema to maps the data. - * - * @var Schema - */ - public $schema; - /** * Types that can be casted. * @@ -30,14 +23,17 @@ class SchemaMapper * * @var string[] */ - protected $castableTypes = ['int', 'integer', 'bool', 'boolean', 'float', 'double', 'real', 'string']; + protected array $castableTypes = ['int', 'integer', 'bool', 'boolean', 'float', 'double', 'real', 'string']; /** * @param Schema $schema schema that will be used to map each field */ - public function __construct(Schema $schema) - { - $this->schema = $schema; + public function __construct( + /** + * The actual schema to maps the data. + */ + public Schema $schema + ) { } /** @@ -45,10 +41,8 @@ public function __construct(Schema $schema) * * @param array|object $data array or object with the fields that should * be mapped to $this->schema specifications - * - * @return array */ - public function map($data) + public function map(array|object $data): array { $data = $this->parseToArray($data); $this->clearDynamic($data); @@ -66,7 +60,7 @@ public function map($data) * * @param array $data Reference of the fields. The passed array will be modified. */ - protected function clearDynamic(array &$data) + protected function clearDynamic(array &$data): void { if (!$this->schema->dynamic) { $data = array_intersect_key($data, $this->schema->fields); @@ -74,20 +68,19 @@ protected function clearDynamic(array &$data) } /** - * Parse a value based on a field yype of the schema. + * Parse a value based on a field type of the schema. * * @param mixed $value value to be parsed * @param string $fieldType description of how the field should be treated * * @return mixed $value Value parsed to match $type */ - public function parseField($value, string $fieldType) + public function parseField(mixed $value, string $fieldType): mixed { // Uses $fieldType method of the schema to parse the value if (method_exists($this->schema, $fieldType)) { return $this->schema->$fieldType($value); } - // Returns null or an empty array if (null === $value || is_array($value) && empty($value)) { return $value; @@ -99,7 +92,7 @@ public function parseField($value, string $fieldType) } // If the field type points to another schema. - if ('schema.' == substr($fieldType, 0, 7)) { + if (str_starts_with($fieldType, 'schema.')) { return $this->mapToSchema($value, substr($fieldType, 7)); } @@ -107,7 +100,7 @@ public function parseField($value, string $fieldType) } /** - * Uses PHP's settype to cast a value to a type. + * Uses PHP's set type to cast a value to a type. * * @see http://php.net/manual/pt_BR/function.settype.php * @@ -116,7 +109,7 @@ public function parseField($value, string $fieldType) * * @return mixed */ - protected function cast($value, string $type) + protected function cast(mixed $value, string $type): mixed { settype($value, $type); @@ -129,10 +122,8 @@ protected function cast($value, string $type) * * @param mixed $value value that will be mapped * @param string $schemaClass class that will be passed to the new SchemaMapper constructor - * - * @return mixed */ - protected function mapToSchema($value, string $schemaClass) + protected function mapToSchema(mixed $value, string $schemaClass): array { $value = (array) $value; $schema = Container::make($schemaClass); @@ -153,17 +144,13 @@ protected function mapToSchema($value, string $schemaClass) * Parses an object to an array before sending it to the SchemaMapper. * * @param mixed $object the object that will be transformed into an array - * - * @return array */ - protected function parseToArray($object): array + protected function parseToArray(mixed $object): array { if (!is_array($object)) { - $attributes = method_exists($object, 'getAttributes') + return method_exists($object, 'getAttributes') ? $object->getAttributes() : get_object_vars($object); - - return $attributes; } return $object; diff --git a/tests/Unit/DataMapper/DataMapperTest.php b/tests/Unit/DataMapper/DataMapperTest.php index f388914f..379aa5a0 100644 --- a/tests/Unit/DataMapper/DataMapperTest.php +++ b/tests/Unit/DataMapper/DataMapperTest.php @@ -21,7 +21,7 @@ class DataMapperTest extends TestCase { - protected m\MockInterface|EventTriggerService $eventService; + protected ?m\MockInterface $eventService = null; public function tearDown(): void { @@ -33,7 +33,7 @@ public function tearDown(): void /** * @dataProvider getWriteConcernVariations */ - public function testShouldSave($entity, $writeConcern, $shouldFireEventAfter, $expected) + public function testShouldSave(object $entity, int $writeConcern, bool $shouldFireEventAfter, bool $expected): void { // Arrange $connection = m::mock(Connection::class); @@ -94,7 +94,7 @@ public function testShouldSave($entity, $writeConcern, $shouldFireEventAfter, $e /** * @dataProvider getWriteConcernVariations */ - public function testShouldInsert($entity, $writeConcern, $shouldFireEventAfter, $expected) + public function testShouldInsert(object $entity, int $writeConcern, bool $shouldFireEventAfter, bool $expected): void { // Arrange $connection = m::mock(Connection::class); @@ -152,7 +152,7 @@ public function testShouldInsert($entity, $writeConcern, $shouldFireEventAfter, /** * @dataProvider getWriteConcernVariations */ - public function testShouldInsertWithoutFiringEvents($entity, $writeConcern, $shouldFireEventAfter, $expected) + public function testShouldInsertWithoutFiringEvents(object $entity, int $writeConcern, bool $shouldFireEventAfter, bool $expected): void { // Arrange $connection = m::mock(Connection::class); @@ -205,7 +205,7 @@ public function testShouldInsertWithoutFiringEvents($entity, $writeConcern, $sho /** * @dataProvider getWriteConcernVariations */ - public function testShouldUpdate($entity, $writeConcern, $shouldFireEventAfter, $expected) + public function testShouldUpdate(object $entity, int $writeConcern, bool $shouldFireEventAfter, bool $expected): void { // Arrange $connection = m::mock(Connection::class); @@ -268,7 +268,7 @@ public function testShouldUpdate($entity, $writeConcern, $shouldFireEventAfter, $this->assertEquals($expected, $mapper->update($entity, $options)); } - public function testDifferentialUpdateShouldWork() + public function testDifferentialUpdateShouldWork(): void { // Arrange $entity = m::mock(ModelInterface::class); @@ -361,7 +361,7 @@ public function testDifferentialUpdateShouldWork() $this->assertTrue($mapper->update($entity, $options)); } - public function testDifferentialUpdateShouldWorkHandlingNullValuesInArrays() + public function testDifferentialUpdateShouldWorkHandlingNullValuesInArrays(): void { // Arrange $entity = m::mock(ModelInterface::class); @@ -476,7 +476,7 @@ public function testDifferentialUpdateShouldWorkHandlingNullValuesInArrays() $this->assertTrue($mapper->update($entity, $options)); } - public function testDifferentialUpdateShouldReturnTrueIfThereIsNothingToUpdate() + public function testDifferentialUpdateShouldReturnTrueIfThereIsNothingToUpdate(): void { // Arrange $entity = m::mock(ModelInterface::class); @@ -527,7 +527,7 @@ public function testUpdateShouldCallInsertWhenObjectHasNoId( $writeConcern, $shouldFireEventAfter, $expected - ) { + ): void { // Arrange $connection = m::mock(Connection::class); $mapper = m::mock(DataMapper::class.'[parseToDocument,getCollection]', [$connection]); @@ -589,11 +589,11 @@ public function testUpdateShouldCallInsertWhenObjectHasNoId( /** * @dataProvider getWriteConcernVariations */ - public function testShouldDelete($entity, $writeConcern, $shouldFireEventAfter, $expected) + public function testShouldDelete(object $entity, int $writeConcern, bool $shouldFireEventAfter, bool $expected): void { // Arrange $connection = m::mock(Connection::class); - $mapper = m::mock(DataMapper::class.'[parseToDocument,getCollection]', [$connection]); + $mapper = m::mock(DataMapper::class . '[parseToDocument,getCollection]', [$connection]); $collection = m::mock(Collection::class); $parsedObject = ['_id' => 123]; @@ -621,7 +621,7 @@ public function testShouldDelete($entity, $writeConcern, $shouldFireEventAfter, $operationResult->shouldReceive('isAcknowledged') ->once() - ->andReturn((bool) $writeConcern); + ->andReturn((bool)$writeConcern); $operationResult->shouldReceive('getDeletedCount') ->andReturn(1); @@ -648,13 +648,14 @@ public function testShouldDelete($entity, $writeConcern, $shouldFireEventAfter, * @dataProvider eventsToBailOperations */ public function testDatabaseOperationsShouldBailOutIfTheEventHandlerReturnsFalse( - $operation, - $dbOperation, - $eventName - ) { + string $operation, + string $dbOperation, + string $eventName + ): void + { // Arrange $connection = m::mock(Connection::class); - $mapper = m::mock(DataMapper::class.'[parseToDocument,getCollection]', [$connection]); + $mapper = m::mock(DataMapper::class . '[parseToDocument,getCollection]', [$connection]); $collection = m::mock(Collection::class); $entity = m::mock(); @@ -681,7 +682,7 @@ public function testDatabaseOperationsShouldBailOutIfTheEventHandlerReturnsFalse $this->assertFalse($result); } - public function testShouldGetWithWhereQuery() + public function testShouldGetWithWhereQuery(): void { // Arrange $connection = m::mock(Connection::class); @@ -718,7 +719,7 @@ public function testShouldGetWithWhereQuery() $this->assertEquals($schema, $cacheableResult->entitySchema); } - public function testShouldGetAll() + public function testShouldGetAll(): void { // Arrange $connection = m::mock(Connection::class); @@ -738,7 +739,7 @@ public function testShouldGetAll() $this->assertEquals($mongolidCursor, $result); } - public function testShouldGetNullIfFirstCantFindAnything() + public function testShouldGetNullIfFirstCantFindAnything(): void { // Arrange $connection = m::mock(Connection::class); @@ -774,7 +775,7 @@ public function testShouldGetNullIfFirstCantFindAnything() $this->assertNull($result); } - public function testShouldGetFirstProjectingFields() + public function testShouldGetFirstProjectingFields(): void { // Arrange $connection = m::mock(Connection::class); @@ -813,7 +814,7 @@ public function testShouldGetFirstProjectingFields() $this->assertNull($result); } - public function testShouldGetFirstTroughACacheableCursor() + public function testShouldGetFirstTroughACacheableCursor(): void { // Arrange $connection = m::mock(Connection::class); @@ -839,7 +840,7 @@ public function testShouldGetFirstTroughACacheableCursor() $this->assertEquals($entity, $result); } - public function testShouldGetFirstTroughACacheableCursorProjectingFields() + public function testShouldGetFirstTroughACacheableCursorProjectingFields(): void { // Arrange $connection = m::mock(Connection::class); @@ -873,7 +874,7 @@ public function testShouldParseObjectToDocumentAndPutResultingIdIntoTheGivenObje $mapper = m::mock(DataMapper::class.'[getSchemaMapper]', [$connection]); $entity = m::mock(); $parsedDocument = ['a_field' => 123, '_id' => 'bacon']; - $schemaMapper = m::mock(Schema::class.'[]'); + $schemaMapper = m::mock(SchemaMapper::class.'[]'); $mapper->shouldAllowMockingProtectedMethods(); @@ -898,7 +899,7 @@ public function testShouldParseObjectToDocumentAndPutResultingIdIntoTheGivenObje ); } - public function testShouldGetSchemaMapper() + public function testShouldGetSchemaMapper(): void { // Arrange $connection = m::mock(Connection::class); @@ -916,7 +917,7 @@ public function testShouldGetSchemaMapper() $this->assertEquals($schema, $result->schema); } - public function testShouldGetRawCollection() + public function testShouldGetRawCollection(): void { // Arrange $connection = m::mock(Connection::class); @@ -952,7 +953,7 @@ public function testShouldGetRawCollection() /** * @dataProvider getProjections */ - public function testPrepareProjectionShouldConvertArray($data, $expectation) + public function testPrepareProjectionShouldConvertArray(array $data, array $expectation): void { // Arrange $connection = m::mock(Connection::class); @@ -965,7 +966,7 @@ public function testPrepareProjectionShouldConvertArray($data, $expectation) $this->assertEquals($expectation, $result); } - public function testPrepareProjectionShouldThrownAnException() + public function testPrepareProjectionShouldThrownAnException(): void { // Arrange $connection = m::mock(Connection::class); @@ -979,9 +980,9 @@ public function testPrepareProjectionShouldThrownAnException() $this->callProtected($mapper, 'prepareProjection', [$data]); } - protected function getEventService() + protected function getEventService(): m\MockInterface { - if (!($this->eventService ?? false)) { + if (!$this->eventService) { $this->eventService = m::mock(EventTriggerService::class); Container::instance(EventTriggerService::class, $this->eventService); } @@ -989,7 +990,7 @@ protected function getEventService() return $this->eventService; } - protected function expectEventToBeFired($event, $entity, bool $halt, $return = true) + protected function expectEventToBeFired(string $event, object $entity, bool $halt, bool $return = true): void { $event = 'mongolid.'.$event.': '.get_class($entity); @@ -1000,7 +1001,7 @@ protected function expectEventToBeFired($event, $entity, bool $halt, $return = t ->andReturn($return); } - protected function expectEventNotToBeFired($event, $entity) + protected function expectEventNotToBeFired(string $event, object $entity): void { $event = 'mongolid.'.$event.': '.get_class($entity); @@ -1009,7 +1010,7 @@ protected function expectEventNotToBeFired($event, $entity) ->never(); } - public function eventsToBailOperations() + public function eventsToBailOperations(): array { return [ 'Saving event' => [ @@ -1038,7 +1039,7 @@ public function eventsToBailOperations() ]; } - public function getWriteConcernVariations() + public function getWriteConcernVariations(): array { return [ 'acknowledged write concern with plain object' => [ @@ -1071,7 +1072,7 @@ public function getWriteConcernVariations() /** * Retrieves projections that should be replaced by mapper. */ - public function getProjections() + public function getProjections(): array { return [ 'Should return self array' => [ diff --git a/tests/Unit/DataMapper/SchemaMapperTest.php b/tests/Unit/DataMapper/SchemaMapperTest.php index 95e731ab..e5dae598 100644 --- a/tests/Unit/DataMapper/SchemaMapperTest.php +++ b/tests/Unit/DataMapper/SchemaMapperTest.php @@ -15,7 +15,7 @@ public function tearDown(): void m::close(); } - public function testShouldMapToFieldsOfSchema() + public function testShouldMapToFieldsOfSchema(): void { // Arrange $schema = m::mock(Schema::class); @@ -58,7 +58,7 @@ public function testShouldMapToFieldsOfSchema() ); } - public function testShouldClearDynamicFieldsIfSchemaIsNotDynamic() + public function testShouldClearDynamicFieldsIfSchemaIsNotDynamic(): void { // Arrange $schema = m::mock(Schema::class); @@ -85,7 +85,7 @@ public function testShouldClearDynamicFieldsIfSchemaIsNotDynamic() ); } - public function testShouldNotClearDynamicFieldsIfSchemaIsDynamic() + public function testShouldNotClearDynamicFieldsIfSchemaIsDynamic(): void { // Arrange $schema = m::mock(Schema::class); @@ -113,7 +113,7 @@ public function testShouldNotClearDynamicFieldsIfSchemaIsDynamic() ); } - public function testShouldParseFieldIntoCastableType() + public function testShouldParseFieldIntoCastableType(): void { // Arrange $schema = m::mock(Schema::class); @@ -131,7 +131,7 @@ public function testShouldParseFieldIntoCastableType() ); } - public function testShouldParseFieldIntoAnotherMappedSchemaIfTypeBeginsWithSchema() + public function testShouldParseFieldIntoAnotherMappedSchemaIfTypeBeginsWithSchema(): void { // Arrange $schema = m::mock(Schema::class); @@ -154,7 +154,7 @@ public function testShouldParseFieldIntoAnotherMappedSchemaIfTypeBeginsWithSchem ); } - public function testShouldParseFieldUsingAMethodInSchemaIfTypeIsAnUnknownString() + public function testShouldParseFieldUsingAMethodInSchemaIfTypeIsAnUnknownString(): void { // Arrange $schemaClass = new class() extends Schema { @@ -174,7 +174,7 @@ public function pumpkinPoint($value) ); } - public function testShouldMapAnArrayValueToAnotherSchema() + public function testShouldMapAnArrayValueToAnotherSchema(): void { // Arrange $schema = m::mock(Schema::class); @@ -187,7 +187,7 @@ public function testShouldMapAnArrayValueToAnotherSchema() Container::instance('Xd\MySchema', $mySchema); // When instantiating the SchemaMapper with the specified $param as dependency - Container::bind(SchemaMapper::class, function ($container, $params) use ($value, $mySchema, $test) { + Container::bind(SchemaMapper::class, function ($container, $params) use ($value, $mySchema, $test): \Mockery\LegacyMockInterface { // Check if mySchema has been injected correctly $test->assertSame($mySchema, $params['schema']); @@ -212,7 +212,7 @@ public function testShouldMapAnArrayValueToAnotherSchema() ); } - public function testShouldParseToArrayGettingObjectAttributes() + public function testShouldParseToArrayGettingObjectAttributes(): void { // Arrange $schema = m::mock(Schema::class); @@ -226,7 +226,7 @@ public function testShouldParseToArrayGettingObjectAttributes() ); } - public function testShouldParseToArrayIfIsAnArray() + public function testShouldParseToArrayIfIsAnArray(): void { // Arrange $schema = m::mock(Schema::class); @@ -240,13 +240,16 @@ public function testShouldParseToArrayIfIsAnArray() ); } - public function testShouldGetAttributesWhenGetAttributesMethodIsAvailable() + public function testShouldGetAttributesWhenGetAttributesMethodIsAvailable(): void { // Arrange $schema = m::mock(Schema::class); $schemaMapper = new SchemaMapper($schema); $object = new class() { - public function getAttributes() + /** + * @return array{foo: string} + */ + public function getAttributes(): array { return ['foo' => 'bar']; }