Skip to content

Commit 4e8c907

Browse files
committed
Update exception handling for PHP 7
No need to catch Throwable and Exception any more. Also import and document exceptions used.
1 parent b5d82a6 commit 4e8c907

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

src/PhpRenderer.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use InvalidArgumentException;
1515
use Psr\Http\Message\ResponseInterface;
16+
use RuntimeException;
17+
use Throwable;
1618

1719
/**
1820
* Class PhpRenderer
@@ -49,8 +51,9 @@ public function __construct(string $templatePath = "", array $attributes = [], s
4951
*
5052
* @note $data cannot contain template as a key
5153
*
52-
* @throws \InvalidArgumentException
53-
* @throws \RuntimeException if $templatePath . $template does not exist
54+
* @throws InvalidArgumentException
55+
* @throws RuntimeException if $templatePath . $template does not exist
56+
* @throws Throwable
5457
*/
5558
public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface
5659
{
@@ -74,12 +77,12 @@ public function getLayout(): string
7477
*/
7578
public function setLayout(string $layout): void
7679
{
77-
if ($layout === "" || $layout === null) {
80+
if ($layout === '' || $layout === null) {
7881
$this->layout = null;
7982
} else {
8083
$layoutPath = $this->templatePath . $layout;
8184
if (!is_file($layoutPath)) {
82-
throw new \RuntimeException("Layout template `$layout` does not exist");
85+
throw new RuntimeException("Layout template `$layout` does not exist");
8386
}
8487
$this->layout = $layout;
8588
}
@@ -144,8 +147,9 @@ public function setTemplatePath(string $templatePath): void
144147
*
145148
* @note $data cannot contain template as a key
146149
*
147-
* @throws \InvalidArgumentException
148-
* @throws \RuntimeException
150+
* @throws InvalidArgumentException
151+
* @throws RuntimeException
152+
* @throws Throwable
149153
*/
150154
public function fetch(string $template, array $data = [], bool $useLayout = false): string
151155
{
@@ -164,17 +168,18 @@ public function fetch(string $template, array $data = [], bool $useLayout = fals
164168
*
165169
* @note $data cannot contain template as a key
166170
*
167-
* @throws \InvalidArgumentException
168-
* @throws \RuntimeException
171+
* @throws InvalidArgumentException
172+
* @throws RuntimeException
173+
* @throws Throwable
169174
*/
170175
public function fetchTemplate(string $template, array $data = []): string
171176
{
172177
if (isset($data['template'])) {
173-
throw new \InvalidArgumentException("Duplicate template key found");
178+
throw new InvalidArgumentException('Duplicate template key found');
174179
}
175180

176181
if (!is_file($this->templatePath . $template)) {
177-
throw new \RuntimeException("View cannot render `$template` because the template does not exist");
182+
throw new RuntimeException("View cannot render `$template` because the template does not exist");
178183
}
179184

180185
$data = array_merge($this->attributes, $data);
@@ -183,10 +188,7 @@ public function fetchTemplate(string $template, array $data = []): string
183188
ob_start();
184189
$this->protectedIncludeScope($this->templatePath . $template, $data);
185190
$output = ob_get_clean();
186-
} catch (\Throwable $e) { // PHP 7+
187-
ob_end_clean();
188-
throw $e;
189-
} catch (\Exception $e) { // PHP < 7
191+
} catch (Throwable $e) {
190192
ob_end_clean();
191193
throw $e;
192194
}

tests/PhpRendererTest.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
namespace Slim\ViewsTest;
66

7+
use InvalidArgumentException;
78
use PHPUnit\Framework\TestCase;
9+
use RuntimeException;
810
use Slim\Psr7\Headers;
911
use Slim\Psr7\Response;
1012
use Slim\Psr7\Stream;
@@ -78,12 +80,7 @@ public function testExceptionInTemplate()
7880

7981
try {
8082
$newResponse = $renderer->render($response, "exception_layout.phtml");
81-
} catch (Throwable $t) { // PHP 7+
82-
// Simulates an error template
83-
$newResponse = $renderer->render($response, "template.phtml", [
84-
"hello" => "Hi"
85-
]);
86-
} catch (Exception $e) { // PHP < 7
83+
} catch (Throwable $t) {
8784
// Simulates an error template
8885
$newResponse = $renderer->render($response, "template.phtml", [
8986
"hello" => "Hi"
@@ -176,12 +173,6 @@ public function testExceptionInLayout()
176173
$newResponse = $renderer->render($response, "template.phtml", [
177174
"hello" => "Hi"
178175
]);
179-
} catch (Exception $e) { // PHP < 7
180-
// Simulates an error template
181-
$renderer->setLayout(null);
182-
$newResponse = $renderer->render($response, "template.phtml", [
183-
"hello" => "Hi"
184-
]);
185176
}
186177

187178
$newResponse->getBody()->rewind();

0 commit comments

Comments
 (0)