diff --git a/composer.json b/composer.json index cc135be..29b5fb2 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,8 @@ "require-dev": { "phpunit/phpunit": "^9.0", "mockery/mockery": "^1.2", - "friendsofphp/php-cs-fixer": "^3.94" + "friendsofphp/php-cs-fixer": "^3.94", + "rector/rector": "^2.3" }, "autoload": { "psr-4": { @@ -39,6 +40,8 @@ ], "cs:check": "vendor/bin/php-cs-fixer check", "cs:fix": "vendor/bin/php-cs-fixer fix", + "rector:check": "vendor/bin/rector process --dry-run", + "rector:fix": "vendor/bin/rector process", "test": "vendor/bin/phpunit --stderr --coverage-clover coverage.xml" }, "config": { diff --git a/helpers/functions.php b/helpers/functions.php index 5b0fe4b..c06f771 100644 --- a/helpers/functions.php +++ b/helpers/functions.php @@ -9,7 +9,7 @@ * @author Arman Ag. * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ use Quantum\HttpClient\Exceptions\HttpClientException; @@ -99,7 +99,7 @@ function save_remote_image(string $imageUrl, string $userDirectory, string $imag * @throws BaseException * @throws ConfigException */ -function create_user_directory(string $uuid) +function create_user_directory(string $uuid): void { $userDirectory = uploads_dir() . DS . $uuid; @@ -111,16 +111,16 @@ function create_user_directory(string $uuid) /** * Cleans up text for titles/descriptions. * @param string $text - * @return array|string|string[] + * @return string */ -function textCleanUp(string $text) +function textCleanUp(string $text): string { return str_replace(['"', '\'', '-'], '', $text); } /** * Encodes current query string to URL-safe base64. - * @param string $query + * @param string|null $query * @return string */ function nav_ref_encode(?string $query): string diff --git a/migrations/create_table_comments_1698145440.php b/migrations/create_table_comments_1698145440.php index c0fe8fc..dad02ed 100644 --- a/migrations/create_table_comments_1698145440.php +++ b/migrations/create_table_comments_1698145440.php @@ -5,7 +5,7 @@ class Create_table_comments_1698145440 extends QtMigration { - public function up(?TableFactory $tableFactory) + public function up(?TableFactory $tableFactory): void { $table = $tableFactory->create('comments'); $table->addColumn('id', 'integer')->primary()->autoIncrement(); @@ -16,7 +16,7 @@ public function up(?TableFactory $tableFactory) $table->addColumn('created_at', 'timestamp'); } - public function down(?TableFactory $tableFactory) + public function down(?TableFactory $tableFactory): void { $tableFactory->drop('comments'); } diff --git a/migrations/create_table_posts_1669639752.php b/migrations/create_table_posts_1669639752.php index f7473e7..2699e34 100644 --- a/migrations/create_table_posts_1669639752.php +++ b/migrations/create_table_posts_1669639752.php @@ -5,7 +5,7 @@ class Create_table_posts_1669639752 extends QtMigration { - public function up(?TableFactory $tableFactory) + public function up(?TableFactory $tableFactory): void { $table = $tableFactory->create('posts'); $table->addColumn('id', 'integer')->primary()->autoIncrement(); @@ -17,7 +17,7 @@ public function up(?TableFactory $tableFactory) $table->addColumn('updated_at', 'timestamp'); } - public function down(?TableFactory $tableFactory) + public function down(?TableFactory $tableFactory): void { $tableFactory->drop('posts'); } diff --git a/migrations/create_table_users_1669639740.php b/migrations/create_table_users_1669639740.php index dfea921..53f06a9 100644 --- a/migrations/create_table_users_1669639740.php +++ b/migrations/create_table_users_1669639740.php @@ -5,7 +5,7 @@ class Create_table_users_1669639740 extends QtMigration { - public function up(?TableFactory $tableFactory) + public function up(?TableFactory $tableFactory): void { $table = $tableFactory->create('users'); $table->addColumn('id', 'integer')->primary()->autoIncrement(); @@ -25,7 +25,7 @@ public function up(?TableFactory $tableFactory) $table->addColumn('otp_expires', 'timestamp')->nullable(); } - public function down(?TableFactory $tableFactory) + public function down(?TableFactory $tableFactory): void { $tableFactory->drop('users'); } diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..05e9572 --- /dev/null +++ b/rector.php @@ -0,0 +1,28 @@ +withPaths([ + __DIR__ . '/helpers', + __DIR__ . '/hooks', + __DIR__ . '/libraries', + __DIR__ . '/migrations', + __DIR__ . '/public/index.php', + __DIR__ . '/shared', + ]) + ->withSkip([ + __DIR__ . '/modules', + ]) + ->withSets([ + LevelSetList::UP_TO_PHP_80, + SetList::TYPE_DECLARATION, + ]) + ->withRules([ + DeclareStrictTypesRector::class, + ]); diff --git a/shared/Commands/CommandValidationTrait.php b/shared/Commands/CommandValidationTrait.php index 5a7f7cf..8fb6817 100644 --- a/shared/Commands/CommandValidationTrait.php +++ b/shared/Commands/CommandValidationTrait.php @@ -1,5 +1,7 @@ deleteAllComments(); diff --git a/shared/Commands/DemoCommand.php b/shared/Commands/DemoCommand.php index a49df88..a3f04d5 100644 --- a/shared/Commands/DemoCommand.php +++ b/shared/Commands/DemoCommand.php @@ -1,5 +1,7 @@ 'Creating API module', - 'action' => function () { + 'action' => function (): void { $this->createModule('Api', 'DemoApi', false); }, ], [ 'message' => 'Creating Web module', - 'action' => function () { + 'action' => function (): void { $this->createModule('Web', 'DemoWeb', true); }, ], [ 'message' => 'Cleaning up uploads & rebuilding database', - 'action' => function () { + 'action' => function (): void { $this->resetDatabase(); }, ], [ 'message' => 'Generating users', - 'action' => function () { + 'action' => function (): void { $this->generatedUsers = $this->generateUsers(); }, ], [ 'message' => 'Generating posts', - 'action' => function () { + 'action' => function (): void { $this->generatedPosts = $this->generatePosts($this->generatedUsers); }, ], [ 'message' => 'Generating comments', - 'action' => function () { + 'action' => function (): void { $this->generateComments($this->generatedUsers, $this->generatedPosts); }, ], @@ -295,7 +297,7 @@ private function generateComments(array $users, array $posts): void $this->runCommandInternally(self::COMMANDS['comment_create'], [ 'post_uuid' => $post['uuid'], 'user_uuid' => $commentUser['uuid'], - 'content' => textCleanUp($this->faker->realText(rand(20, 100))), + 'content' => textCleanUp($this->faker->realText(random_int(20, 100))), ]); } } @@ -374,9 +376,7 @@ private function generatePostData(array $user): array */ private function getRandomUserExcept(array $users, string $excludeUuid): array { - $candidates = array_filter($users, function ($u) use ($excludeUuid) { - return $u['uuid'] !== $excludeUuid; - }); + $candidates = array_filter($users, fn (array $u): bool => $u['uuid'] !== $excludeUuid); $candidates = array_values($candidates); return $candidates[array_rand($candidates)]; @@ -454,7 +454,7 @@ private function resetDatabase(): void * @param array $arguments * @throws ExceptionInterface */ - private function runCommandInternally(string $commandName, array $arguments = []) + private function runCommandInternally(string $commandName, array $arguments = []): void { $command = $this->getApplication()->find($commandName); $command->run(new ArrayInput($arguments), new NullOutput()); @@ -464,7 +464,7 @@ private function runCommandInternally(string $commandName, array $arguments = [] * @param string $commandName * @param array $arguments */ - private function runCommandExternally(string $commandName, array $arguments = []) + private function runCommandExternally(string $commandName, array $arguments = []): void { $command = $this->buildCommand($commandName, $arguments); $this->runExternalProcess($command); diff --git a/shared/Commands/PostCreateCommand.php b/shared/Commands/PostCreateCommand.php index 1700590..8aba24d 100644 --- a/shared/Commands/PostCreateCommand.php +++ b/shared/Commands/PostCreateCommand.php @@ -1,5 +1,7 @@ deleteAllPosts(); diff --git a/shared/Commands/PostShowCommand.php b/shared/Commands/PostShowCommand.php index db9dd5d..8ee6ce4 100644 --- a/shared/Commands/PostShowCommand.php +++ b/shared/Commands/PostShowCommand.php @@ -1,5 +1,7 @@ * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Commands; diff --git a/shared/Commands/PostUpdateCommand.php b/shared/Commands/PostUpdateCommand.php index 639c782..613d1a3 100644 --- a/shared/Commands/PostUpdateCommand.php +++ b/shared/Commands/PostUpdateCommand.php @@ -1,5 +1,7 @@ deleteAllUsers(); diff --git a/shared/Commands/UserShowCommand.php b/shared/Commands/UserShowCommand.php index bef7d4a..07e910a 100644 --- a/shared/Commands/UserShowCommand.php +++ b/shared/Commands/UserShowCommand.php @@ -1,5 +1,7 @@ * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Commands; diff --git a/shared/DTOs/CommentDTO.php b/shared/DTOs/CommentDTO.php index 8da7d7f..e0a82fe 100644 --- a/shared/DTOs/CommentDTO.php +++ b/shared/DTOs/CommentDTO.php @@ -1,5 +1,7 @@ $this->title, 'content' => $this->content, 'image' => $this->image, - ], function ($value) { - return $value !== null; - }); + ], fn (?string $value): bool => $value !== null); } } diff --git a/shared/DTOs/UserDTO.php b/shared/DTOs/UserDTO.php index 1a64468..747433b 100644 --- a/shared/DTOs/UserDTO.php +++ b/shared/DTOs/UserDTO.php @@ -1,5 +1,7 @@ $this->lastname, 'role' => $this->role, 'image' => $this->image, - ], function ($value) { - return $value !== null; - }); + ], fn (?string $value): bool => $value !== null); } } diff --git a/shared/Enums/Role.php b/shared/Enums/Role.php index 24c3bb3..5701e33 100644 --- a/shared/Enums/Role.php +++ b/shared/Enums/Role.php @@ -1,5 +1,7 @@ * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Enums; diff --git a/shared/Models/Comment.php b/shared/Models/Comment.php index 8b8e368..59f286f 100644 --- a/shared/Models/Comment.php +++ b/shared/Models/Comment.php @@ -1,5 +1,7 @@ * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Models; diff --git a/shared/Services/AuthService.php b/shared/Services/AuthService.php index 8cbee97..ad2486a 100644 --- a/shared/Services/AuthService.php +++ b/shared/Services/AuthService.php @@ -1,5 +1,7 @@ createUserDirectory($data['uuid']); @@ -139,7 +141,7 @@ public function delete(string $uuid): bool /** * Delete all users */ - public function deleteAllUsers() + public function deleteAllUsers(): void { $this->model->truncate(); } @@ -178,7 +180,7 @@ public function userSchema(): array * @throws BaseException * @throws ConfigException */ - private function createUserDirectory(string $uuid) + private function createUserDirectory(string $uuid): void { $userDirectory = uploads_dir() . DS . $uuid; diff --git a/shared/Services/CommentService.php b/shared/Services/CommentService.php index bd41d40..a3add55 100644 --- a/shared/Services/CommentService.php +++ b/shared/Services/CommentService.php @@ -1,5 +1,7 @@ * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) * @link http://quantum.softberg.org/ - * @since 2.9.9 + * @since 3.0.0 */ namespace Shared\Services; @@ -17,6 +19,7 @@ use Quantum\Model\Exceptions\ModelException; use Shared\Transformers\CommentTransformer; use Quantum\App\Exceptions\BaseException; +use Quantum\Model\ModelCollection; use Quantum\Service\QtService; use Shared\DTOs\CommentDTO; use Shared\Models\Comment; @@ -32,11 +35,7 @@ class CommentService extends QtService * @var Comment */ private $model; - - /** - * @var CommentTransformer - */ - private $transformer; + private CommentTransformer $transformer; /** * @param CommentTransformer $transformer @@ -56,7 +55,7 @@ public function __construct(CommentTransformer $transformer) * @throws BaseException * @throws ModelException */ - public function getCommentsByPost(string $postUuid) + public function getCommentsByPost(string $postUuid): ModelCollection { return $this->model ->joinTo(model(User::class)) @@ -115,7 +114,7 @@ public function deleteComment(string $uuid): bool * Delete all comments * @throws ModelException */ - public function deleteAllComments() + public function deleteAllComments(): void { $this->model->truncate(); } diff --git a/shared/Services/PostService.php b/shared/Services/PostService.php index 1fdca3b..eaf0896 100644 --- a/shared/Services/PostService.php +++ b/shared/Services/PostService.php @@ -1,5 +1,7 @@ model->truncate(); } @@ -233,7 +235,7 @@ public function saveImage(UploadedFile $uploadedFile, string $imageDirectory, st * @throws ReflectionException * @throws ConfigException */ - public function deleteImage(string $imagePath) + public function deleteImage(string $imagePath): void { $fs = FileSystemFactory::get(); diff --git a/shared/Transformers/CommentTransformer.php b/shared/Transformers/CommentTransformer.php index 582e007..b1542f6 100644 --- a/shared/Transformers/CommentTransformer.php +++ b/shared/Transformers/CommentTransformer.php @@ -1,5 +1,7 @@