Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions app/Controllers/DungeonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,43 @@
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.
*
* @return ResponseInterface|string
*/
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');
}

echo view('dungeon', [
return view('dungeon', [
'dungeon' => $dungeon,
'monsters' => $dungeon->monsters(3),
]);
Expand Down
6 changes: 3 additions & 3 deletions app/Controllers/HeroController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
]);
}
Expand Down
2 changes: 2 additions & 0 deletions app/Entities/Monster.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*
* This class represents a single row in the
* `monsters` database.
*
* @property int $dungeon_id
*/
class Monster extends Entity
{
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/database/FakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -43,6 +44,7 @@ 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();
$dungeon = model(DungeonModel::class)->find($monster->dungeon_id);

Expand All @@ -52,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);

Expand Down