Skip to content

Commit 972bc0e

Browse files
committed
Use single quoted strings
This reduces cognitive load as there's no need to scan to see if a variable is used within the string.
1 parent d639d32 commit 972bc0e

File tree

2 files changed

+47
-47
lines changed

2 files changed

+47
-47
lines changed

src/PhpRenderer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class PhpRenderer
3939
*/
4040
protected $layout;
4141

42-
public function __construct(string $templatePath = "", array $attributes = [], string $layout = "")
42+
public function __construct(string $templatePath = '', array $attributes = [], string $layout = '')
4343
{
4444
$this->templatePath = rtrim($templatePath, '/\\') . '/';
4545
$this->attributes = $attributes;
@@ -82,7 +82,7 @@ public function setLayout(string $layout): void
8282
} else {
8383
$layoutPath = $this->templatePath . $layout;
8484
if (!is_file($layoutPath)) {
85-
throw new RuntimeException("Layout template `$layout` does not exist");
85+
throw new RuntimeException('Layout template "' . $layout . '" does not exist');
8686
}
8787
$this->layout = $layout;
8888
}
@@ -179,7 +179,7 @@ public function fetchTemplate(string $template, array $data = []): string
179179
}
180180

181181
if (!is_file($this->templatePath . $template)) {
182-
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');
183183
}
184184

185185
$data = array_merge($this->attributes, $data);

tests/PhpRendererTest.php

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,182 +18,182 @@ class PhpRendererTest extends TestCase
1818

1919
public function testRenderer()
2020
{
21-
$renderer = new PhpRenderer(__DIR__ . "/_files/");
21+
$renderer = new PhpRenderer(__DIR__ . '/_files/');
2222

2323
$headers = new Headers();
2424
$body = new Stream(fopen('php://temp', 'r+'));
2525
$response = new Response(200, $headers, $body);
2626

27-
$newResponse = $renderer->render($response, "template.phtml", ["hello" => "Hi"]);
27+
$newResponse = $renderer->render($response, 'template.phtml', ['hello' => 'Hi']);
2828

2929
$newResponse->getBody()->rewind();
3030

31-
$this->assertEquals("Hi", $newResponse->getBody()->getContents());
31+
$this->assertEquals('Hi', $newResponse->getBody()->getContents());
3232
}
3333

3434
public function testRenderConstructor()
3535
{
36-
$renderer = new PhpRenderer(__DIR__ . "/_files");
36+
$renderer = new PhpRenderer(__DIR__ . '/_files');
3737

3838
$headers = new Headers();
3939
$body = new Stream(fopen('php://temp', 'r+'));
4040
$response = new Response(200, $headers, $body);
4141

42-
$newResponse = $renderer->render($response, "template.phtml", ["hello" => "Hi"]);
42+
$newResponse = $renderer->render($response, 'template.phtml', ['hello' => 'Hi']);
4343

4444
$newResponse->getBody()->rewind();
4545

46-
$this->assertEquals("Hi", $newResponse->getBody()->getContents());
46+
$this->assertEquals('Hi', $newResponse->getBody()->getContents());
4747
}
4848

4949
public function testAttributeMerging()
5050
{
5151

52-
$renderer = new PhpRenderer(__DIR__ . "/_files/", [
53-
"hello" => "Hello"
52+
$renderer = new PhpRenderer(__DIR__ . '/_files/', [
53+
'hello' => 'Hello'
5454
]);
5555

5656
$headers = new Headers();
5757
$body = new Stream(fopen('php://temp', 'r+'));
5858
$response = new Response(200, $headers, $body);
5959

60-
$newResponse = $renderer->render($response, "template.phtml", [
61-
"hello" => "Hi"
60+
$newResponse = $renderer->render($response, 'template.phtml', [
61+
'hello' => 'Hi'
6262
]);
6363
$newResponse->getBody()->rewind();
64-
$this->assertEquals("Hi", $newResponse->getBody()->getContents());
64+
$this->assertEquals('Hi', $newResponse->getBody()->getContents());
6565
}
6666

6767
public function testExceptionInTemplate()
6868
{
69-
$renderer = new PhpRenderer(__DIR__ . "/_files/");
69+
$renderer = new PhpRenderer(__DIR__ . '/_files/');
7070

7171
$headers = new Headers();
7272
$body = new Stream(fopen('php://temp', 'r+'));
7373
$response = new Response(200, $headers, $body);
7474

7575
try {
76-
$newResponse = $renderer->render($response, "exception_layout.phtml");
76+
$newResponse = $renderer->render($response, 'exception_layout.phtml');
7777
} catch (Throwable $t) {
7878
// Simulates an error template
79-
$newResponse = $renderer->render($response, "template.phtml", [
80-
"hello" => "Hi"
79+
$newResponse = $renderer->render($response, 'template.phtml', [
80+
'hello' => 'Hi'
8181
]);
8282
}
8383

8484
$newResponse->getBody()->rewind();
8585

86-
$this->assertEquals("Hi", $newResponse->getBody()->getContents());
86+
$this->assertEquals('Hi', $newResponse->getBody()->getContents());
8787
}
8888

8989
public function testExceptionForTemplateInData()
9090
{
91-
$renderer = new PhpRenderer(__DIR__ . "/_files/");
91+
$renderer = new PhpRenderer(__DIR__ . '/_files/');
9292

9393
$headers = new Headers();
9494
$body = new Stream(fopen('php://temp', 'r+'));
9595
$response = new Response(200, $headers, $body);
9696

9797
$this->expectException(InvalidArgumentException::class);
98-
$renderer->render($response, "template.phtml", [
99-
"template" => "Hi"
98+
$renderer->render($response, 'template.phtml', [
99+
'template' => 'Hi'
100100
]);
101101
}
102102

103103
public function testTemplateNotFound()
104104
{
105-
$renderer = new PhpRenderer(__DIR__ . "/_files/");
105+
$renderer = new PhpRenderer(__DIR__ . '/_files/');
106106

107107
$headers = new Headers();
108108
$body = new Stream(fopen('php://temp', 'r+'));
109109
$response = new Response(200, $headers, $body);
110110

111111
$this->expectException(RuntimeException::class);
112-
$renderer->render($response, "adfadftemplate.phtml", []);
112+
$renderer->render($response, 'adfadftemplate.phtml', []);
113113
}
114114

115115
public function testLayout()
116116
{
117-
$renderer = new PhpRenderer(__DIR__ . "/_files/", ["title" => "My App"]);
118-
$renderer->setLayout("layout.phtml");
117+
$renderer = new PhpRenderer(__DIR__ . '/_files/', ['title' => 'My App']);
118+
$renderer->setLayout('layout.phtml');
119119

120120
$headers = new Headers();
121121
$body = new Stream(fopen('php://temp', 'r+'));
122122
$response = new Response(200, $headers, $body);
123123

124-
$newResponse = $renderer->render($response, "template.phtml", ["title" => "Hello - My App", "hello" => "Hi"]);
124+
$newResponse = $renderer->render($response, 'template.phtml', ['title' => 'Hello - My App', 'hello' => 'Hi']);
125125

126126
$newResponse->getBody()->rewind();
127127

128-
$this->assertEquals("<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer"
129-
. "</footer></body></html>", $newResponse->getBody()->getContents());
128+
$this->assertEquals('<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer'
129+
. '</footer></body></html>', $newResponse->getBody()->getContents());
130130
}
131131

132132
public function testLayoutConstructor()
133133
{
134-
$renderer = new PhpRenderer(__DIR__ . "/_files", ["title" => "My App"], "layout.phtml");
134+
$renderer = new PhpRenderer(__DIR__ . '/_files', ['title' => 'My App'], 'layout.phtml');
135135

136136
$headers = new Headers();
137137
$body = new Stream(fopen('php://temp', 'r+'));
138138
$response = new Response(200, $headers, $body);
139139

140-
$newResponse = $renderer->render($response, "template.phtml", ["title" => "Hello - My App", "hello" => "Hi"]);
140+
$newResponse = $renderer->render($response, 'template.phtml', ['title' => 'Hello - My App', 'hello' => 'Hi']);
141141

142142
$newResponse->getBody()->rewind();
143143

144-
$this->assertEquals("<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer"
145-
. "</footer></body></html>", $newResponse->getBody()->getContents());
144+
$this->assertEquals('<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer'
145+
. '</footer></body></html>', $newResponse->getBody()->getContents());
146146
}
147147

148148
public function testExceptionInLayout()
149149
{
150-
$renderer = new PhpRenderer(__DIR__ . "/_files/");
151-
$renderer->setLayout("exception_layout.phtml");
150+
$renderer = new PhpRenderer(__DIR__ . '/_files/');
151+
$renderer->setLayout('exception_layout.phtml');
152152

153153
$headers = new Headers();
154154
$body = new Stream(fopen('php://temp', 'r+'));
155155
$response = new Response(200, $headers, $body);
156156

157157
try {
158-
$newResponse = $renderer->render($response, "template.phtml");
158+
$newResponse = $renderer->render($response, 'template.phtml');
159159
} catch (Throwable $t) { // PHP 7+
160160
// Simulates an error template
161161
$renderer->setLayout('');
162-
$newResponse = $renderer->render($response, "template.phtml", [
163-
"hello" => "Hi"
162+
$newResponse = $renderer->render($response, 'template.phtml', [
163+
'hello' => 'Hi'
164164
]);
165165
}
166166

167167
$newResponse->getBody()->rewind();
168168

169-
$this->assertEquals("Hi", $newResponse->getBody()->getContents());
169+
$this->assertEquals('Hi', $newResponse->getBody()->getContents());
170170
}
171171

172172
public function testLayoutNotFound()
173173
{
174-
$renderer = new PhpRenderer(__DIR__ . "/_files/");
174+
$renderer = new PhpRenderer(__DIR__ . '/_files/');
175175
$this->expectException(RuntimeException::class);
176-
$renderer->setLayout("non-existent_layout.phtml");
176+
$renderer->setLayout('non-existent_layout.phtml');
177177
}
178178

179179
public function testContentDataKeyShouldBeIgnored()
180180
{
181-
$renderer = new PhpRenderer(__DIR__ . "/_files/");
182-
$renderer->setLayout("layout.phtml");
181+
$renderer = new PhpRenderer(__DIR__ . '/_files/');
182+
$renderer->setLayout('layout.phtml');
183183

184184
$headers = new Headers();
185185
$body = new Stream(fopen('php://temp', 'r+'));
186186
$response = new Response(200, $headers, $body);
187187

188188
$newResponse = $renderer->render(
189189
$response,
190-
"template.phtml",
191-
["title" => "Hello - My App", "hello" => "Hi", "content" => "Ho"]
190+
'template.phtml',
191+
['title' => 'Hello - My App', 'hello' => 'Hi', 'content' => 'Ho']
192192
);
193193

194194
$newResponse->getBody()->rewind();
195195

196-
$this->assertEquals("<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer"
197-
. "</footer></body></html>", $newResponse->getBody()->getContents());
196+
$this->assertEquals('<html><head><title>Hello - My App</title></head><body>Hi<footer>This is the footer'
197+
. '</footer></body></html>', $newResponse->getBody()->getContents());
198198
}
199199
}

0 commit comments

Comments
 (0)