Skip to content

Commit

Permalink
PhpStan warnings have been resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
batumibiz committed Nov 6, 2024
1 parent c84c920 commit 9d52d33
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ public function getFileExtension(): string
*/
public function addPath(string $folder, string $nameSpace = 'main'): self
{
if (empty($folder)) {
if ($folder === '') {
throw new InvalidArgumentException('You must specify folder.');
}

if (empty($nameSpace)) {
if ($nameSpace === '') {
throw new InvalidArgumentException('Namespace cannot be empty.');
}

$folder = rtrim($folder, '/\\');

if (
isset($this->nameSpaces[$nameSpace])
&& in_array($folder, $this->nameSpaces[$nameSpace])
&& in_array($folder, $this->nameSpaces[$nameSpace], true)
) {
throw new InvalidArgumentException(
'The "' . $folder . '" folder in the "' . $nameSpace . '" namespace already exists.'
Expand Down
1 change: 1 addition & 0 deletions src/Template/TemplateData.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class TemplateData
*/
public function add(array $data, array $templates = []): self
{
/** @phpstan-ignore empty.notAllowed */
return empty($templates)
? $this->shareWithAll($data)
: $this->shareWithSome($data, $templates);
Expand Down
28 changes: 13 additions & 15 deletions tests/unit/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public function setUp(): void

public function testGetFileExtension(): void
{
$this->assertEquals('phtml', $this->engine->getFileExtension());
self::assertEquals('phtml', $this->engine->getFileExtension());
}

public function testAddAndGetSeveralFolders(): void
{
$this->engine->addPath('folder1', 'ns1');
$this->engine->addPath('folder2', 'ns1');
$this->engine->addPath(M_PATH_ROOT);
$this->assertContains('folder1', $this->engine->getPath('ns1'));
$this->assertContains('folder2', $this->engine->getPath('ns1'));
$this->assertContains(rtrim(M_PATH_ROOT, '/\\'), $this->engine->getPath('main'));
self::assertContains('folder1', $this->engine->getPath('ns1'));
self::assertContains('folder2', $this->engine->getPath('ns1'));
self::assertContains(rtrim(M_PATH_ROOT, '/\\'), $this->engine->getPath('main'));
}

public function testAddFolderWithEmptyNamespace(): void
Expand Down Expand Up @@ -66,14 +66,14 @@ public function testAddData(): void
{
$this->engine->addData(['name' => 'TestData']);
$data = $this->engine->getTemplateData();
$this->assertEquals('TestData', $data['name']);
self::assertEquals('TestData', $data['name']);
}

public function testAddDataWithTemplates(): void
{
$this->engine->addData(['name' => 'TestData'], ['template1', 'template2']);
$data1 = $this->engine->getTemplateData('template1');
$this->assertEquals('TestData', $data1['name']);
self::assertEquals('TestData', $data1['name']);
}

/**
Expand All @@ -82,7 +82,7 @@ public function testAddDataWithTemplates(): void
public function testRenderTemplate(): void
{
$this->engine->addPath(M_PATH_ROOT);
$this->assertEquals(
self::assertEquals(
'Hello!',
$this->engine->render('main::tpl-data', ['var' => 'Hello!'])
);
Expand All @@ -94,14 +94,12 @@ public function testRenderTemplate(): void
public function testRegisterFunction(): void
{
$this->engine->registerFunction('uppercase', 'strtoupper');
$this->assertIsCallable($this->engine->getFunction('uppercase'));

$this->engine->addPath(M_PATH_ROOT);
$result = $this->engine->render(
'main::tpl-func-uppercase',
['var' => 'abcdefgh']
);
$this->assertEquals('ABCDEFGH', $result);
self::assertEquals('ABCDEFGH', $result);
}

public function testRegisterExistFunction(): void
Expand All @@ -122,7 +120,7 @@ public function testGetFunction(): void
{
$this->engine->registerFunction('uppercase', 'strtoupper');
$function = $this->engine->getFunction('uppercase');
$this->assertEquals('TTT', $function('TTT'));
self::assertEquals('TTT', $function('TTT'));
}

public function testGetInvalidFunction(): void
Expand All @@ -135,14 +133,14 @@ public function testGetInvalidFunction(): void
public function testDoesFunctionExist(): void
{
$this->engine->registerFunction('uppercase', 'strtoupper');
$this->assertTrue($this->engine->doesFunctionExist('uppercase'));
$this->assertFalse($this->engine->doesFunctionExist('some_function_that_does_not_exist'));
self::assertTrue($this->engine->doesFunctionExist('uppercase'));
self::assertFalse($this->engine->doesFunctionExist('some_function_that_does_not_exist'));
}

public function testLoadExtension(): void
{
$this->assertFalse($this->engine->doesFunctionExist('foo'));
self::assertFalse($this->engine->doesFunctionExist('foo'));
$this->engine->loadExtension(new FakeExtension());
$this->assertTrue($this->engine->doesFunctionExist('foo'));
self::assertTrue($this->engine->doesFunctionExist('foo'));
}
}
8 changes: 4 additions & 4 deletions tests/unit/Template/TemplateDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ public function testAddDataToAllTemplates(): void
{
$this->templateData->add(['name' => 'TestData']);
$data = $this->templateData->get();
$this->assertEquals('TestData', $data['name']);
self::assertEquals('TestData', $data['name']);
}

public function testAddDataToOneTemplate(): void
{
$this->templateData->add(['name' => 'TestData'], ['template']);
$data = $this->templateData->get('template');
$this->assertEquals('TestData', $data['name']);
self::assertEquals('TestData', $data['name']);
}

public function testAddDataToOneTemplateAgain(): void
{
$this->templateData->add(['first' => 'Test'], ['template']);
$this->templateData->add(['last' => 'Data'], ['template']);
$data = $this->templateData->get('template');
$this->assertEquals('Data', $data['last']);
self::assertEquals('Data', $data['last']);
}

public function testAddDataToSomeTemplates(): void
{
$this->templateData->add(['name' => 'TestData'], ['template1', 'template2']);
$data = $this->templateData->get('template1');
$this->assertEquals('TestData', $data['name']);
self::assertEquals('TestData', $data['name']);
}
}
6 changes: 3 additions & 3 deletions tests/unit/Template/TemplateNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function setUp(): void
public function testGetPath(): void
{
$name = new TemplateName($this->engine, 'main::tpl-data');
$this->assertEquals(
self::assertEquals(
M_PATH_ROOT . 'tpl-data.phtml',
$name->resolvePath()
);
Expand All @@ -31,7 +31,7 @@ public function testGetPath(): void
public function testGetPathWithoutNamespace(): void
{
$name = new TemplateName($this->engine, 'tpl-data');
$this->assertEquals(
self::assertEquals(
M_PATH_ROOT . 'tpl-data.phtml',
$name->resolvePath()
);
Expand All @@ -52,7 +52,7 @@ public function testGetPathWithMultipleFolders(): void
$engine->addPath('anotherfolder');

$name = new TemplateName($engine, 'tpl-data');
$this->assertEquals(
self::assertEquals(
M_PATH_ROOT . 'tpl-data.phtml',
$name->resolvePath()
);
Expand Down
32 changes: 16 additions & 16 deletions tests/unit/Template/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public function setUp(): void
public function testRender(): void
{
$template = new Template($this->engine, 'main::tpl-empty');
$this->assertEquals('Empty', $template->render());
self::assertEquals('Empty', $template->render());
}

public function testRenderViaToStringMagicMethod(): void
{
$template = new Template($this->engine, 'main::tpl-empty');
$this->assertEquals('Empty', (string) $template);
self::assertEquals('Empty', (string) $template);
}

/**
Expand All @@ -40,9 +40,9 @@ public function testAssignData(): void
$data = ['var' => 'TestData'];
$template = new Template($this->engine, 'main::tpl-data');
$template->data($data);
$this->assertEquals($template->data(), $data);
$this->assertEquals('TestData', $template->render());
$this->assertEquals('Test', $template->render(['var' => 'Test']));
self::assertEquals($template->data(), $data);
self::assertEquals('TestData', $template->render());
self::assertEquals('Test', $template->render(['var' => 'Test']));
}

/**
Expand All @@ -52,7 +52,7 @@ public function testCanCallFunction(): void
{
$this->engine->registerFunction('uppercase', 'strtoupper');
$template = new Template($this->engine, 'main::tpl-func-uppercase');
$this->assertEquals('TESTDATA', $template->render(['var' => 'TestData']));
self::assertEquals('TESTDATA', $template->render(['var' => 'TestData']));
}


Expand Down Expand Up @@ -84,7 +84,7 @@ public function testRenderException(): void
public function testLayout(): void
{
$template = new Template($this->engine, 'main::tpl-layout');
$this->assertEquals('Hello User!', $template->render());
self::assertEquals('Hello User!', $template->render());
}

/**
Expand All @@ -93,7 +93,7 @@ public function testLayout(): void
public function testSectionReplace(): void
{
$template = new Template($this->engine, 'main::tpl-section-replace');
$this->assertEquals('Hello World!', $template->render());
self::assertEquals('Hello World!', $template->render());
}

/**
Expand All @@ -102,7 +102,7 @@ public function testSectionReplace(): void
public function testSectionAppend(): void
{
$template = new Template($this->engine, 'main::tpl-section-append');
$this->assertEquals('Hello Beautiful World!', $template->render());
self::assertEquals('Hello Beautiful World!', $template->render());
}

/**
Expand All @@ -111,7 +111,7 @@ public function testSectionAppend(): void
public function testSection(): void
{
$template = new Template($this->engine, 'main::tpl-section');
$this->assertEquals('Hello All!' . "\n", $template->render());
self::assertEquals('Hello All!' . "\n", $template->render());
}

/**
Expand Down Expand Up @@ -153,7 +153,7 @@ public function testStopSectionBeforeStarting(): void
public function testPushSection(): void
{
$template = new Template($this->engine, 'main::tpl-section-push');
$this->assertEquals('Hello Beautiful World!', $template->render());
self::assertEquals('Hello Beautiful World!', $template->render());
}

/**
Expand All @@ -162,7 +162,7 @@ public function testPushSection(): void
public function testFetchFunction(): void
{
$template = new Template($this->engine, 'main::tpl-fetch');
$this->assertEquals('Empty', $template->render());
self::assertEquals('Empty', $template->render());
}

/**
Expand All @@ -172,7 +172,7 @@ public function testBatchFunction(): void
{
$this->engine->registerFunction('uppercase', 'strtoupper');
$template = new Template($this->engine, 'main::tpl-batch');
$this->assertEquals('testdata', $template->render());
self::assertEquals('testdata', $template->render());
}

/**
Expand All @@ -183,7 +183,7 @@ public function testBatchFunctionWithInvalidFunction(): void
$this->expectException(LogicException::class);
$this->expectExceptionMessage('The batch function could not find the "uppercase" function.');
$template = new Template($this->engine, 'main::tpl-batch');
$this->assertEquals('testdata', $template->render());
self::assertEquals('testdata', $template->render());
}

/**
Expand All @@ -193,7 +193,7 @@ public function testEscapeFunction(): void
{
$data = ['var' => '&"\'<>'];
$template = new Template($this->engine, 'main::tpl-data');
$this->assertEquals('&amp;&quot;&#039;&lt;&gt;', $template->render($data));
self::assertEquals('&amp;&quot;&#039;&lt;&gt;', $template->render($data));
}

/**
Expand All @@ -202,6 +202,6 @@ public function testEscapeFunction(): void
public function testEscapeFunctionBatch(): void
{
$template = new Template($this->engine, 'main::tpl-escape-batch');
$this->assertEquals('&gt;GNORTS/&lt;ATADTSET&gt;GNORTS&lt;', $template->render());
self::assertEquals('&gt;GNORTS/&lt;ATADTSET&gt;GNORTS&lt;', $template->render());
}
}

0 comments on commit 9d52d33

Please sign in to comment.