Skip to content

Commit b437661

Browse files
committed
Add PHP7.1+ type-hints
1 parent 2436599 commit b437661

File tree

2 files changed

+28
-67
lines changed

2 files changed

+28
-67
lines changed

src/PhpRenderer.php

Lines changed: 25 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
36
* Slim Framework (http://slimframework.com)
47
*
@@ -34,14 +37,7 @@ class PhpRenderer
3437
*/
3538
protected $layout;
3639

37-
/**
38-
* SlimRenderer constructor.
39-
*
40-
* @param string $templatePath
41-
* @param array $attributes
42-
* @param string $layout
43-
*/
44-
public function __construct($templatePath = "", $attributes = [], $layout = "")
40+
public function __construct(string $templatePath = "", array $attributes = [], string $layout = "")
4541
{
4642
$this->templatePath = rtrim($templatePath, '/\\') . '/';
4743
$this->attributes = $attributes;
@@ -51,20 +47,12 @@ public function __construct($templatePath = "", $attributes = [], $layout = "")
5147
/**
5248
* Render a template
5349
*
54-
* $data cannot contain template as a key
55-
*
56-
* throws RuntimeException if $templatePath . $template does not exist
57-
*
58-
* @param ResponseInterface $response
59-
* @param string $template
60-
* @param array $data
61-
*
62-
* @return ResponseInterface
50+
* @note $data cannot contain template as a key
6351
*
6452
* @throws \InvalidArgumentException
65-
* @throws \RuntimeException
53+
* @throws \RuntimeException if $templatePath . $template does not exist
6654
*/
67-
public function render(ResponseInterface $response, $template, array $data = [])
55+
public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface
6856
{
6957
$output = $this->fetch($template, $data, true);
7058

@@ -75,20 +63,16 @@ public function render(ResponseInterface $response, $template, array $data = [])
7563

7664
/**
7765
* Get layout template
78-
*
79-
* @return string
8066
*/
81-
public function getLayout()
67+
public function getLayout(): string
8268
{
8369
return $this->layout;
8470
}
8571

8672
/**
8773
* Set layout template
88-
*
89-
* @param string $layout
9074
*/
91-
public function setLayout($layout)
75+
public function setLayout(string $layout): void
9276
{
9377
if ($layout === "" || $layout === null) {
9478
$this->layout = null;
@@ -103,18 +87,14 @@ public function setLayout($layout)
10387

10488
/**
10589
* Get the attributes for the renderer
106-
*
107-
* @return array
10890
*/
109-
public function getAttributes()
91+
public function getAttributes(): array
11092
{
11193
return $this->attributes;
11294
}
11395

11496
/**
11597
* Set the attributes for the renderer
116-
*
117-
* @param array $attributes
11898
*/
11999
public function setAttributes(array $attributes)
120100
{
@@ -123,21 +103,19 @@ public function setAttributes(array $attributes)
123103

124104
/**
125105
* Add an attribute
126-
*
127-
* @param $key
128-
* @param $value
129106
*/
130-
public function addAttribute($key, $value) {
107+
public function addAttribute(string $key, $value): void
108+
{
131109
$this->attributes[$key] = $value;
132110
}
133111

134112
/**
135113
* Retrieve an attribute
136114
*
137-
* @param $key
138115
* @return mixed
139116
*/
140-
public function getAttribute($key) {
117+
public function getAttribute(string $key)
118+
{
141119
if (!isset($this->attributes[$key])) {
142120
return false;
143121
}
@@ -147,42 +125,30 @@ public function getAttribute($key) {
147125

148126
/**
149127
* Get the template path
150-
*
151-
* @return string
152128
*/
153-
public function getTemplatePath()
129+
public function getTemplatePath(): string
154130
{
155131
return $this->templatePath;
156132
}
157133

158134
/**
159135
* Set the template path
160-
*
161-
* @param string $templatePath
162136
*/
163-
public function setTemplatePath($templatePath)
137+
public function setTemplatePath(string $templatePath): void
164138
{
165139
$this->templatePath = rtrim($templatePath, '/\\') . '/';
166140
}
167141

168142
/**
169143
* Renders a template and returns the result as a string
170144
*
171-
* cannot contain template as a key
172-
*
173-
* throws RuntimeException if $templatePath . $template does not exist
174-
*
175-
* @param $template
176-
* @param array $data
177-
* @param bool $useLayout
178-
*
179-
* @return mixed
145+
* @note $data cannot contain template as a key
180146
*
181147
* @throws \InvalidArgumentException
182148
* @throws \RuntimeException
183149
*/
184-
public function fetch($template, array $data = [], $useLayout = false) {
185-
150+
public function fetch(string $template, array $data = [], bool $useLayout = false): string
151+
{
186152
$output = $this->fetchTemplate($template, $data);
187153

188154
if ($this->layout !== null && $useLayout) {
@@ -196,19 +162,13 @@ public function fetch($template, array $data = [], $useLayout = false) {
196162
/**
197163
* Renders a template and returns the result as a string
198164
*
199-
* cannot contain template as a key
200-
*
201-
* throws RuntimeException if $templatePath . $template does not exist
202-
*
203-
* @param $template
204-
* @param array $data
205-
*
206-
* @return mixed
165+
* @note $data cannot contain template as a key
207166
*
208167
* @throws \InvalidArgumentException
209168
* @throws \RuntimeException
210169
*/
211-
public function fetchTemplate($template, array $data = []) {
170+
public function fetchTemplate(string $template, array $data = []): string
171+
{
212172
if (isset($data['template'])) {
213173
throw new \InvalidArgumentException("Duplicate template key found");
214174
}
@@ -223,7 +183,6 @@ public function fetchTemplate($template, array $data = []) {
223183
ob_start();
224184
$this->protectedIncludeScope($this->templatePath . $template, $data);
225185
$output = ob_get_clean();
226-
227186
} catch(\Throwable $e) { // PHP 7+
228187
ob_end_clean();
229188
throw $e;
@@ -236,10 +195,10 @@ public function fetchTemplate($template, array $data = []) {
236195
}
237196

238197
/**
239-
* @param string $template
240-
* @param array $data
198+
* Include template within a separate scope for extracted $data
241199
*/
242-
protected function protectedIncludeScope ($template, array $data) {
200+
protected function protectedIncludeScope (string $template, array $data): void
201+
{
243202
extract($data);
244203
include func_get_arg(0);
245204
}

tests/PhpRendererTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use PHPUnit\Framework\TestCase;
46
use Slim\Psr7\Headers;
57
use Slim\Psr7\Response;
@@ -156,7 +158,7 @@ public function testExceptionInLayout() {
156158
$newResponse = $renderer->render($response, "template.phtml");
157159
} catch (Throwable $t) { // PHP 7+
158160
// Simulates an error template
159-
$renderer->setLayout(null);
161+
$renderer->setLayout('');
160162
$newResponse = $renderer->render($response, "template.phtml", [
161163
"hello" => "Hi"
162164
]);

0 commit comments

Comments
 (0)