Skip to content

Commit

Permalink
Improve exception message
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed May 20, 2023
1 parent 6f05243 commit 5371935
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
23 changes: 13 additions & 10 deletions system/View/Cells/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace CodeIgniter\View\Cells;

use CodeIgniter\Traits\PropertiesTrait;
use LogicException;
use ReflectionClass;
use RuntimeException;

/**
* Class Cell
Expand Down Expand Up @@ -66,7 +66,7 @@ public function setView(string $view)
* current scope and captures the output buffer instead of
* relying on the view service.
*
* @throws RuntimeException
* @throws LogicException
*/
final protected function view(?string $view, array $data = []): string
{
Expand All @@ -90,17 +90,20 @@ final protected function view(?string $view, array $data = []): string
$view = $directory . $view . '.php';
}

foreach ([$view, $possibleView1 ?? '', $possibleView2 ?? ''] as $candidateView) {
if (is_file($candidateView)) {
$foundView = $candidateView;
break;
}
}
$candidateViews = array_filter(
[$view, $possibleView1 ?? '', $possibleView2 ?? ''],
static fn (string $path): bool => $path !== '' && is_file($path)
);

if (! isset($foundView)) {
throw new RuntimeException('Cannot locate the view file for the cell.');
if ($candidateViews === []) {
throw new LogicException(sprintf(
'Cannot locate the view file for the "%s" cell.',
static::class
));
}

$foundView = $candidateViews[0];

return (function () use ($properties, $foundView): string {
extract($properties);
ob_start();
Expand Down
6 changes: 3 additions & 3 deletions tests/system/View/ControlledCellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\View\Exceptions\ViewException;
use RuntimeException;
use LogicException;
use Tests\Support\View\Cells\AdditionCell;
use Tests\Support\View\Cells\AwesomeCell;
use Tests\Support\View\Cells\BadCell;
Expand Down Expand Up @@ -69,8 +69,8 @@ public function testCellThroughRenderMethodWithExtraData()

public function testCellThrowsExceptionWhenCannotFindTheViewFile()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Cannot locate the view file for the cell.');
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Cannot locate the view file for the "Tests\\Support\\View\\Cells\\BadCell" cell.');

view_cell(BadCell::class);
}
Expand Down

0 comments on commit 5371935

Please sign in to comment.