From ed45ce455050f6c2557b3fd8dad78f8cd7805c73 Mon Sep 17 00:00:00 2001 From: Mauro Bonfietti Date: Fri, 4 Sep 2020 00:06:03 -0300 Subject: [PATCH 01/10] Update API version. --- src/Controller/DefaultController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 9ef9fd69..20a62757 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -9,7 +9,7 @@ final class DefaultController extends BaseController { - public const API_VERSION = '1.20.0'; + public const API_VERSION = '1.21.0'; public function getHelp(Request $request, Response $response): Response { From e68f2df3416b34d0b03350fd69d1d17620b69a23 Mon Sep 17 00:00:00 2001 From: Mauro Bonfietti Date: Fri, 4 Sep 2020 00:09:44 -0300 Subject: [PATCH 02/10] Create Dotenv in safe way (getenv not more used). --- src/App/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/App/App.php b/src/App/App.php index 76a12c02..e3c395a7 100644 --- a/src/App/App.php +++ b/src/App/App.php @@ -4,7 +4,7 @@ require __DIR__ . '/../../vendor/autoload.php'; $baseDir = __DIR__ . '/../../'; -$dotenv = Dotenv\Dotenv::createUnsafeImmutable($baseDir); +$dotenv = Dotenv\Dotenv::createImmutable($baseDir); $envFile = $baseDir . '.env'; if (file_exists($envFile)) { $dotenv->load(); From 79defb27f2f399c8580cd45d70cf050c802f8766 Mon Sep 17 00:00:00 2001 From: Mauro Bonfietti Date: Fri, 4 Sep 2020 00:10:30 -0300 Subject: [PATCH 03/10] Minor changes. --- src/Handler/ApiError.php | 2 +- src/Repository/TaskRepository.php | 6 +++--- tests/integration/NoteTest.php | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Handler/ApiError.php b/src/Handler/ApiError.php index 097d404d..0e265096 100644 --- a/src/Handler/ApiError.php +++ b/src/Handler/ApiError.php @@ -23,7 +23,7 @@ public function __invoke( 'code' => $statusCode, ]; $body = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); - $response->getBody()->write($body); + $response->getBody()->write((string) $body); return $response ->withStatus($statusCode) diff --git a/src/Repository/TaskRepository.php b/src/Repository/TaskRepository.php index faaa3c1c..2b74bc1d 100644 --- a/src/Repository/TaskRepository.php +++ b/src/Repository/TaskRepository.php @@ -31,7 +31,7 @@ public function getAllTasks(): array $statement = $this->getDb()->prepare($query); $statement->execute(); - return $statement->fetchAll(); + return (array) $statement->fetchAll(); } public function getAll(int $userId): array @@ -41,7 +41,7 @@ public function getAll(int $userId): array $statement->bindParam('userId', $userId); $statement->execute(); - return $statement->fetchAll(); + return (array) $statement->fetchAll(); } public function search(string $tasksName, int $userId, ?int $status): array @@ -56,7 +56,7 @@ public function search(string $tasksName, int $userId, ?int $status): array } $statement->execute(); - return $statement->fetchAll(); + return (array) $statement->fetchAll(); } public function create(object $task): object diff --git a/tests/integration/NoteTest.php b/tests/integration/NoteTest.php index c6a371a2..b3d90f5c 100644 --- a/tests/integration/NoteTest.php +++ b/tests/integration/NoteTest.php @@ -27,8 +27,8 @@ public function testGetNotes(): void $this->assertStringContainsString('id', $result); $this->assertStringContainsString('name', $result); $this->assertStringContainsString('description', $result); - $this->assertMatchesRegularExpression('{"code":200,"status":"success"}', $value); - $this->assertMatchesRegularExpression('{"name":"[A-Za-z0-9_. ]+","description":"[A-Za-z0-9_. ]+"}', $value); + $this->assertMatchesRegularExpression('{"code":200,"status":"success"}', (string) $value); + $this->assertMatchesRegularExpression('{"name":"[A-Za-z0-9_. ]+","description":"[A-Za-z0-9_. ]+"}', (string) $value); $this->assertStringNotContainsString('error', $result); } @@ -40,7 +40,7 @@ public function testGetNotesByPage(): void $response = $this->runApp('GET', '/api/v1/notes?page=1&perPage=3'); $result = (string) $response->getBody(); - $value = json_encode(json_decode($result)); + $value = (string) json_encode(json_decode($result)); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('application/json', $response->getHeaderLine('Content-Type')); From 9026164e64a8035afc7bccf31b3178860519fd3f Mon Sep 17 00:00:00 2001 From: Mauro Bonfietti Date: Fri, 4 Sep 2020 00:38:17 -0300 Subject: [PATCH 04/10] Add Note Entity. --- src/Entity/Note.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/Entity/Note.php diff --git a/src/Entity/Note.php b/src/Entity/Note.php new file mode 100644 index 00000000..a30afe34 --- /dev/null +++ b/src/Entity/Note.php @@ -0,0 +1,39 @@ +id; + } + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name): void + { + $this->name = $name; + } + + public function getDescription(): string + { + return $this->description; + } + + public function setDescription(string $description): void + { + $this->description = $description; + } +} From 8f4ea9d134bfe36fa6062c6e8fdf8a94471c5a1c Mon Sep 17 00:00:00 2001 From: Mauro Bonfietti Date: Fri, 4 Sep 2020 00:39:39 -0300 Subject: [PATCH 05/10] Use and return a Note Entity object. --- src/Repository/NoteRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Repository/NoteRepository.php b/src/Repository/NoteRepository.php index 5b9fb305..92d04f68 100644 --- a/src/Repository/NoteRepository.php +++ b/src/Repository/NoteRepository.php @@ -14,7 +14,7 @@ public function checkAndGetNote(int $noteId): object $statement = $this->database->prepare($query); $statement->bindParam(':id', $noteId); $statement->execute(); - $note = $statement->fetchObject(); + $note = $statement->fetchObject(\App\Entity\Note::class); if (! $note) { throw new Note('Note not found.', 404); } From 232709182834db7378bdeaddadf25a44b7cbe042 Mon Sep 17 00:00:00 2001 From: Mauro Bonfietti Date: Fri, 4 Sep 2020 18:44:23 -0300 Subject: [PATCH 06/10] Revert "Add Note Entity." This reverts commit 9026164e64a8035afc7bccf31b3178860519fd3f. --- src/Entity/Note.php | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 src/Entity/Note.php diff --git a/src/Entity/Note.php b/src/Entity/Note.php deleted file mode 100644 index a30afe34..00000000 --- a/src/Entity/Note.php +++ /dev/null @@ -1,39 +0,0 @@ -id; - } - - public function getName(): string - { - return $this->name; - } - - public function setName(string $name): void - { - $this->name = $name; - } - - public function getDescription(): string - { - return $this->description; - } - - public function setDescription(string $description): void - { - $this->description = $description; - } -} From 9837388fe8b43764d13a92711106e0607d5d5d0d Mon Sep 17 00:00:00 2001 From: Mauro Bonfietti Date: Fri, 4 Sep 2020 18:45:51 -0300 Subject: [PATCH 07/10] Note Entity Rollback (not ready yet). --- src/Repository/NoteRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Repository/NoteRepository.php b/src/Repository/NoteRepository.php index 92d04f68..5b9fb305 100644 --- a/src/Repository/NoteRepository.php +++ b/src/Repository/NoteRepository.php @@ -14,7 +14,7 @@ public function checkAndGetNote(int $noteId): object $statement = $this->database->prepare($query); $statement->bindParam(':id', $noteId); $statement->execute(); - $note = $statement->fetchObject(\App\Entity\Note::class); + $note = $statement->fetchObject(); if (! $note) { throw new Note('Note not found.', 404); } From f498fe065ddf5d61a5fa4ebc65220dc86ba44b03 Mon Sep 17 00:00:00 2001 From: Mauro Bonfietti Date: Fri, 4 Sep 2020 18:46:53 -0300 Subject: [PATCH 08/10] Minor changes detected with psalm. --- src/Controller/BaseController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Controller/BaseController.php b/src/Controller/BaseController.php index b4f87c33..82ff014f 100644 --- a/src/Controller/BaseController.php +++ b/src/Controller/BaseController.php @@ -16,6 +16,9 @@ public function __construct(Container $container) $this->container = $container; } + /** + * @param array|null|object $message + */ protected function jsonResponse( Response $response, string $status, From 3c4621389fbf6b4897778405bf2dc5d09974ecfc Mon Sep 17 00:00:00 2001 From: Mauro Bonfietti Date: Fri, 4 Sep 2020 18:53:49 -0300 Subject: [PATCH 09/10] Minor changes. --- src/Service/Task/TaskService.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Service/Task/TaskService.php b/src/Service/Task/TaskService.php index 4e7ca495..128377ef 100644 --- a/src/Service/Task/TaskService.php +++ b/src/Service/Task/TaskService.php @@ -29,8 +29,11 @@ public function getOne(int $taskId, int $userId): object return $task; } - public function search(string $tasksName, int $userId, $status): array - { + public function search( + string $tasksName, + int $userId, + ?string $status + ): array { if ($status !== null) { $status = (int) $status; } From 6d84509aa0737da32c792d56eff7c092938555c7 Mon Sep 17 00:00:00 2001 From: Mauro Bonfietti Date: Fri, 4 Sep 2020 19:03:16 -0300 Subject: [PATCH 10/10] Minor changes. --- src/Service/Note/Create.php | 2 +- src/Service/Note/Update.php | 2 +- src/Service/Task/TaskService.php | 4 ++-- src/Service/User/Base.php | 4 ++-- src/Service/User/UserService.php | 6 +++--- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Service/Note/Create.php b/src/Service/Note/Create.php index 759c6d7b..bed6aab0 100644 --- a/src/Service/Note/Create.php +++ b/src/Service/Note/Create.php @@ -10,7 +10,7 @@ final class Create extends Base { public function create(array $input): object { - $data = json_decode(json_encode($input), false); + $data = json_decode((string) json_encode($input), false); if (! isset($data->name)) { throw new Note('Invalid data: name is required.', 400); } diff --git a/src/Service/Note/Update.php b/src/Service/Note/Update.php index cca32e89..f789a248 100644 --- a/src/Service/Note/Update.php +++ b/src/Service/Note/Update.php @@ -9,7 +9,7 @@ final class Update extends Base public function update(array $input, int $noteId): object { $note = $this->getOneFromDb($noteId); - $data = json_decode(json_encode($input), false); + $data = json_decode((string) json_encode($input), false); if (isset($data->name)) { $note->name = self::validateNoteName($data->name); } diff --git a/src/Service/Task/TaskService.php b/src/Service/Task/TaskService.php index 128377ef..c4217b8a 100644 --- a/src/Service/Task/TaskService.php +++ b/src/Service/Task/TaskService.php @@ -43,7 +43,7 @@ public function search( public function create(array $input): object { - $data = json_decode(json_encode($input), false); + $data = json_decode((string) json_encode($input), false); if (! isset($data->name)) { throw new Task('The field "name" is required.', 400); } @@ -86,7 +86,7 @@ public function delete(int $taskId, int $userId): void private function validateTask(array $input, int $taskId): object { $task = $this->getTaskFromDb($taskId, (int) $input['decoded']->sub); - $data = json_decode(json_encode($input), false); + $data = json_decode((string) json_encode($input), false); if (! isset($data->name) && ! isset($data->status)) { throw new Task('Enter the data to update the task.', 400); } diff --git a/src/Service/User/Base.php b/src/Service/User/Base.php index b34d8920..953d2ae4 100644 --- a/src/Service/User/Base.php +++ b/src/Service/User/Base.php @@ -42,7 +42,7 @@ protected static function validateEmail(string $emailValue): string throw new User('Invalid email', 400); } - return $email; + return (string) $email; } protected function getUserFromCache(int $userId): object @@ -51,7 +51,7 @@ protected function getUserFromCache(int $userId): object $key = $this->redisService->generateKey($redisKey); if ($this->redisService->exists($key)) { $data = $this->redisService->get($key); - $user = json_decode(json_encode($data), false); + $user = json_decode((string) json_encode($data), false); } else { $user = $this->getUserFromDb($userId); $this->redisService->setex($key, $user); diff --git a/src/Service/User/UserService.php b/src/Service/User/UserService.php index 5c09cfeb..cd4c2179 100644 --- a/src/Service/User/UserService.php +++ b/src/Service/User/UserService.php @@ -65,7 +65,7 @@ public function create(array $input): object public function update(array $input, int $userId): object { $user = $this->getUserFromDb($userId); - $data = json_decode(json_encode($input), false); + $data = json_decode((string) json_encode($input), false); if (! isset($data->name) && ! isset($data->email)) { throw new User('Enter the data to update the user.', 400); } @@ -95,7 +95,7 @@ public function delete(int $userId): void public function login(array $input): string { - $data = json_decode(json_encode($input), false); + $data = json_decode((string) json_encode($input), false); if (! isset($data->email)) { throw new User('The field "email" is required.', 400); } @@ -117,7 +117,7 @@ public function login(array $input): string private function validateUserData(array $input): object { - $user = json_decode(json_encode($input), false); + $user = json_decode((string) json_encode($input), false); if (! isset($user->name)) { throw new User('The field "name" is required.', 400); }