From 473d9e269403e9eb6b3d0ee053004355cf4542ca Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 29 Oct 2023 10:35:20 +0900 Subject: [PATCH 1/6] refactor: use return instead of echo --- app/Controllers/DungeonController.php | 5 ++++- app/Controllers/HeroController.php | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Controllers/DungeonController.php b/app/Controllers/DungeonController.php index 6e7b178..e671069 100644 --- a/app/Controllers/DungeonController.php +++ b/app/Controllers/DungeonController.php @@ -4,6 +4,7 @@ use App\Models\DungeonModel; use App\Models\HeroModel; +use CodeIgniter\HTTP\ResponseInterface; class DungeonController extends BaseController { @@ -28,6 +29,8 @@ public function __construct() * The $id parameter is the hero's ID, and is * passed in from the route definition as $1, * since it is the first placeholder in the route. + * + * @return ResponseInterface|string */ public function show(int $id) { @@ -37,7 +40,7 @@ public function show(int $id) return redirect()->back()->with('error', 'Dungeon not found'); } - echo view('dungeon', [ + return view('dungeon', [ 'dungeon' => $dungeon, 'monsters' => $dungeon->monsters(3), ]); diff --git a/app/Controllers/HeroController.php b/app/Controllers/HeroController.php index 5e789c8..b3138f1 100644 --- a/app/Controllers/HeroController.php +++ b/app/Controllers/HeroController.php @@ -14,7 +14,7 @@ class HeroController extends BaseController * passed in from the route definition as $1, * since it is the first placeholder in the route. */ - public function show(int $id) + public function show(int $id): string { // When you only need to use a model in a single place, // you can simply get a new instance here. It will use @@ -30,10 +30,10 @@ public function show(int $id) throw new PageNotFoundException('Hero not found'); } - // Display a view file, passing the variables + // Return a view, passing the variables // you want to access in the view within the // second parameter. - echo view('hero', [ + return view('hero', [ 'hero' => $hero, ]); } From 45462039c1146e613268a8ed367c7c42badc5b5f Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 29 Oct 2023 10:44:16 +0900 Subject: [PATCH 2/6] refactor: replace $heroes with $dungeons --- app/Controllers/DungeonController.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/Controllers/DungeonController.php b/app/Controllers/DungeonController.php index e671069..a60a21e 100644 --- a/app/Controllers/DungeonController.php +++ b/app/Controllers/DungeonController.php @@ -3,30 +3,29 @@ namespace App\Controllers; use App\Models\DungeonModel; -use App\Models\HeroModel; use CodeIgniter\HTTP\ResponseInterface; class DungeonController extends BaseController { /** - * HeroModel instance. + * DungeonModel instance. */ - protected $heroes; + protected DungeonModel $dungeons; public function __construct() { - // Assign the model to $this->heroes + // Assign the model to $this->dungeons // so that it's available throughout this class // Use the `model()` helper method to return // a single instance of the model no matter // how many times we call it - $this->heroes = model(DungeonModel::class); + $this->dungeons = model(DungeonModel::class); } /** - * View a single hero's details. + * View a single dungeon's details. * - * The $id parameter is the hero's ID, and is + * The $id parameter is the dungeon's ID, and is * passed in from the route definition as $1, * since it is the first placeholder in the route. * @@ -34,7 +33,7 @@ public function __construct() */ public function show(int $id) { - $dungeon = $this->heroes->find($id); + $dungeon = $this->dungeons->find($id); if ($dungeon === null) { return redirect()->back()->with('error', 'Dungeon not found'); From 8e187668343fc0d140eb9a40d7e121dcd78c6240 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 29 Oct 2023 10:51:42 +0900 Subject: [PATCH 3/6] chore: add codeigniter/phpstan-codeigniter composer require --dev codeigniter/phpstan-codeigniter --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2369ffa..f74e89b 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,8 @@ "psr/container": "1.1.2", "codeigniter4/devkit": "^1.0", "tatter/patches": "^2.1", - "kint-php/kint": "^5.1" + "kint-php/kint": "^5.1", + "codeigniter/phpstan-codeigniter": "^1.4" }, "minimum-stability": "dev", "prefer-stable": true, From 899e545bc20a37b9142db5aa17b368548c41752d Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 15 Feb 2024 16:35:56 +0900 Subject: [PATCH 4/6] docs: add @var to fix PHPStan error --- tests/database/FakerTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/database/FakerTest.php b/tests/database/FakerTest.php index 858a33a..a2836b5 100644 --- a/tests/database/FakerTest.php +++ b/tests/database/FakerTest.php @@ -44,6 +44,7 @@ public function testMakesValidMonster() public function testMakesMonsterWithDungeon() { $monster = $this->fabricator->make(); + /** @var Dungeon $dungeon */ $dungeon = model(DungeonModel::class)->find($monster->dungeon_id); $this->assertInstanceOf(Dungeon::class, $dungeon); From d9d2aa9bbad5aeefde6f53027f9194f568a8c5c4 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 16 Feb 2024 07:56:31 +0900 Subject: [PATCH 5/6] docs: add/remove PHPDoc types --- app/Entities/Monster.php | 2 ++ tests/database/FakerTest.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Entities/Monster.php b/app/Entities/Monster.php index 488bd85..a9d6c30 100644 --- a/app/Entities/Monster.php +++ b/app/Entities/Monster.php @@ -9,6 +9,8 @@ * * This class represents a single row in the * `monsters` database. + * + * @property int $dungeon_id */ class Monster extends Entity { diff --git a/tests/database/FakerTest.php b/tests/database/FakerTest.php index a2836b5..8639b31 100644 --- a/tests/database/FakerTest.php +++ b/tests/database/FakerTest.php @@ -43,8 +43,8 @@ public function testMakesValidMonster() // Since our Faker uses Fabricator counts for its dungeon_id we should always have a valid dungeon available public function testMakesMonsterWithDungeon() { + /** @var Monster $monster */ $monster = $this->fabricator->make(); - /** @var Dungeon $dungeon */ $dungeon = model(DungeonModel::class)->find($monster->dungeon_id); $this->assertInstanceOf(Dungeon::class, $dungeon); From 82d86c25d100913e95e05618064f2fb25b2ae4bc Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 16 Feb 2024 08:09:23 +0900 Subject: [PATCH 6/6] docs: add @var types --- phpstan.neon.dist | 1 - tests/database/FakerTest.php | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index ef2ca5f..7d27c67 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -11,7 +11,6 @@ parameters: - app/Views/* ignoreErrors: - '#Call to an undefined method CodeIgniter\\Database\\ConnectionInterface::(en|dis)ableForeignKeyChecks#' - - '#Cannot access property [\$a-z_]+ on (array|object)#' universalObjectCratesClasses: - CodeIgniter\Entity - CodeIgniter\Entity\Entity diff --git a/tests/database/FakerTest.php b/tests/database/FakerTest.php index 8639b31..c86ebab 100644 --- a/tests/database/FakerTest.php +++ b/tests/database/FakerTest.php @@ -34,6 +34,7 @@ protected function setUp(): void public function testMakesValidMonster() { // We can use make() to generate a random dataset defined in our Faker + /** @var Monster $monster */ $monster = $this->fabricator->make(); $this->assertInstanceOf(Monster::class, $monster); @@ -53,6 +54,7 @@ public function testMakesMonsterWithDungeon() public function testCreateAddsToDatabase() { // create() generates a random dataset just like make() but also adds it to the database for us + /** @var Monster $monster */ $monster = $this->fabricator->create(); $this->assertIsInt($monster->id);