Skip to content

Commit 2a5acbd

Browse files
author
Daniyal Hamid
committed
Refactoring + Stricter Type hinting + Updated tests
1 parent 5516939 commit 2a5acbd

File tree

8 files changed

+19
-48
lines changed

8 files changed

+19
-48
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"scripts": {
2222
"style": "vendor/bin/phpcs --standard=PSR12 src",
23+
"style-fix": "vendor/bin/phpcbf --standard=PSR12 src",
2324
"check": "vendor/bin/phpstan analyse src --level=5 -c phpstan.neon",
2425
"md": "vendor/bin/phpmd src text cleancode,unusedcode,codesize,design,naming",
2526
"test": "vendor/bin/phpunit --configuration phpunit.xml --testsuite bitframe_renderer"

src/Data.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ class Data
2727

2828
protected array $templateSpecificVars = [];
2929

30-
/**
31-
* @param array $data
32-
* @param null|string|array $templates
33-
*
34-
* @return $this
35-
*/
36-
public function add(array $data, $templates = null): self
30+
public function add(array $data, null|string|array $templates = null): self
3731
{
3832
if (null === $templates) {
3933
$this->shareWithAll($data);

src/RenderContext.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ class RenderContext
2222
/** @var callable */
2323
private $fetchTpl;
2424

25-
private array $data;
26-
27-
private ?Sections $sections;
28-
2925
public ?string $currSectionName = null;
3026

3127
public string $newSectionMode = Sections::ADD;
@@ -34,18 +30,13 @@ class RenderContext
3430

3531
private array $parentData = [];
3632

37-
private string $childContent;
38-
3933
public function __construct(
4034
callable $fetchTpl,
41-
array $data = [],
42-
?Sections $sections = null,
43-
string $childContent = ''
35+
private array $data = [],
36+
private ?Sections $sections = null,
37+
private string $childContent = '',
4438
) {
4539
$this->fetchTpl = $fetchTpl;
46-
$this->data = $data;
47-
$this->sections = $sections;
48-
$this->childContent = $childContent;
4940
}
5041

5142
public function parent(string $name, array $data = []): void
@@ -126,7 +117,7 @@ public function fetch(string $name, array $data = []): string
126117
*
127118
* @return mixed
128119
*/
129-
public function apply($subject, string $functions)
120+
public function apply(mixed $subject, string $functions): mixed
130121
{
131122
$functionsList = explode('|', $functions);
132123

src/Renderer.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,12 @@ class Renderer
2525

2626
protected string $fileExt;
2727

28-
protected array $paths = [];
29-
3028
protected Data $data;
3129

3230
public function __construct(
33-
array $paths,
34-
string $fileExt = self::DEFAULT_FILE_EXT
31+
protected array $paths = [],
32+
string $fileExt = self::DEFAULT_FILE_EXT,
3533
) {
36-
$this->paths = $paths;
3734
$this->fileExt = $fileExt ?: self::DEFAULT_FILE_EXT;
3835
$this->data = new Data();
3936
}
@@ -43,13 +40,7 @@ public function createTemplate(string $name, ?Sections $sections = null): Templa
4340
return new Template($name, $this, $sections);
4441
}
4542

46-
/**
47-
* @param array $data
48-
* @param null|string|array $tplNames
49-
*
50-
* @return $this
51-
*/
52-
public function withData(array $data, $tplNames = null): self
43+
public function withData(array $data, null|string|array $tplNames = null): self
5344
{
5445
$this->data->add($data, $tplNames);
5546
return $this;

src/Sections.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ final class Sections
2323
/** @var string */
2424
public const PREPEND = 'prepend';
2525

26-
private array $sections;
27-
28-
public function __construct(array $sections = [])
26+
public function __construct(private array $sections = [])
2927
{
30-
$this->sections = $sections;
3128
}
3229

3330
public function add(string $name, string $content): void

src/Template.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class Template
3838
/** @var string */
3939
private const NAME_SEPARATOR = '::';
4040

41-
protected Renderer $engine;
42-
4341
protected string $filePath;
4442

4543
protected array $data = [];
@@ -50,10 +48,9 @@ class Template
5048

5149
public function __construct(
5250
string $name,
53-
Renderer $engine,
54-
?Sections $sections = null
51+
protected Renderer $engine,
52+
?Sections $sections = null,
5553
) {
56-
$this->engine = $engine;
5754
$this->setFilePathFromName($name)->withData($this->engine->getData($name));
5855
$this->sections = $sections ?? new Sections();
5956
}
@@ -133,7 +130,7 @@ public function getPath(): string
133130
*
134131
* @return mixed
135132
*/
136-
public function getVar(string $name, $default = null)
133+
public function getVar(string $name, mixed $default = null): mixed
137134
{
138135
return $this->getData()[$name] ?? $default;
139136
}
@@ -180,7 +177,7 @@ private function setFilePathFromName(string $name): self
180177
*
181178
* @throws Throwable
182179
*/
183-
private function buffer(callable $wrap)
180+
private function buffer(callable $wrap): false|string
184181
{
185182
$level = ob_get_level();
186183

test/DataTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use stdClass;
1616
use PHPUnit\Framework\TestCase;
1717
use BitFrame\Renderer\Data;
18-
use InvalidArgumentException;
18+
use TypeError;
1919

2020
use function array_merge;
2121

@@ -87,7 +87,7 @@ public function dataProvider(): array
8787
public function testAddAndGet(
8888
array $sharedVars,
8989
array $tplSpecificVars,
90-
$template
90+
$template,
9191
): void {
9292
$this->data->add($sharedVars);
9393
$this->data->add($tplSpecificVars, $template);
@@ -142,9 +142,9 @@ public function invalidTemplateProvider(): array
142142
*
143143
* @param mixed $template
144144
*/
145-
public function testAddingInvalidTemplateTypeShouldThrowException($template): void
145+
public function testAddingInvalidTemplateTypeShouldThrowException(mixed $template): void
146146
{
147-
$this->expectException(InvalidArgumentException::class);
147+
$this->expectException(TypeError::class);
148148
$this->data->add(['foo' => 'bar'], $template);
149149
}
150150
}

test/RendererTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function dataProvider(): array
140140
public function testAddAndGetData(
141141
array $sharedVars,
142142
array $tplSpecificVars,
143-
$template
143+
$template,
144144
): void {
145145
$this->renderer->withData($sharedVars);
146146
$this->renderer->withData($tplSpecificVars, $template);

0 commit comments

Comments
 (0)