Skip to content

Commit 1237a5a

Browse files
committed
Code cleanup
1 parent aaa435d commit 1237a5a

17 files changed

+55
-39
lines changed

src/Id.php

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace Templado\Engine;
1111

12+
use function implode;
13+
use function preg_match;
1214
use InvalidArgumentException;
1315

1416
final readonly class Id {

src/StaticNodeList.php

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace Templado\Engine;
1111

12+
use function array_values;
13+
use function count;
1214
use function iterator_to_array;
1315
use ArrayIterator;
1416
use Countable;

src/csrfprotection/CSRFProtection.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99
*/
1010
namespace Templado\Engine;
1111

12-
class CSRFProtection {
13-
private readonly string $fieldName;
14-
15-
private readonly string $tokenValue;
16-
17-
public function __construct(string $fieldName, string $tokenValue) {
18-
$this->fieldName = $fieldName;
19-
$this->tokenValue = $tokenValue;
12+
final readonly class CSRFProtection {
13+
public function __construct(
14+
private string $fieldName,
15+
private string $tokenValue
16+
) {
2017
}
2118

2219
public function fieldName(): string {

src/csrfprotection/CSRFProtectionRenderer.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace Templado\Engine;
1111

12+
use function assert;
1213
use function sprintf;
1314
use DOMDocument;
1415
use DOMElement;
@@ -19,7 +20,8 @@ final class CSRFProtectionRenderer {
1920
private CSRFProtection $protection;
2021

2122
private DOMDocument $dom;
22-
private DOMXPath$xp;
23+
24+
private DOMXPath $xp;
2325

2426
public function render(DOMElement $context, CSRFProtection $protection): void {
2527
$this->protection = $protection;

src/document/DocumentCollection.php

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace Templado\Engine;
1111

12+
use function array_push;
13+
use function count;
1214
use ArrayIterator;
1315
use Countable;
1416
use IteratorAggregate;

src/formdata/FormData.php

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
class FormData {
1919
private readonly string $identifier;
20-
2120
private readonly array $values;
2221

2322
/**

src/merger/MergeList.php

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace Templado\Engine;
1111

12+
use function count;
13+
use function sprintf;
1214
use ArrayIterator;
1315
use DOMDocument;
1416

src/merger/Merger.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace Templado\Engine;
1111

12+
use function assert;
13+
use function sprintf;
1214
use DOMDocument;
1315
use DOMElement;
1416
use DOMXPath;
@@ -90,7 +92,7 @@ private function isConnected(DOMElement $context, DOMElement $contextChild): boo
9092
return false;
9193
}
9294

93-
private function mergeIn(DOMElement $contextChild, DOMElement $import): DOMElement {
95+
private function mergeIn(DOMElement $contextChild, DOMElement $import): void {
9496
$workContext = [$import];
9597

9698
if ($import->namespaceURI === Document::XMLNS) {
@@ -101,12 +103,10 @@ private function mergeIn(DOMElement $contextChild, DOMElement $import): DOMEleme
101103
$contextChild->after(...$workContext);
102104
$contextChild->remove();
103105

104-
return $import;
106+
return;
105107
}
106108

107109
$contextChild->append(...$workContext);
108-
109-
return $import;
110110
}
111111

112112
private function shouldReplaceCurrent(DOMElement $import, DOMElement $contextChild): bool {

src/selectors/XPathSelector.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020
use DOMXPath;
2121

2222
class XPathSelector implements Selector {
23-
/** @var string */
24-
private $queryString;
23+
/** @psalm-var array<string, string> */
24+
private array $prefixMap = [];
2525

26-
/** @var array<string, string> */
27-
private $prefixMap = [];
28-
29-
public function __construct(string $query) {
30-
$this->queryString = $query;
26+
public function __construct(
27+
private string $queryString
28+
) {
3129
}
3230

3331
public function registerPrefix(string $prefix, string $uri): void {

src/serializer/EmptyElementsFilter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class EmptyElementsFilter implements Filter {
1616
public function apply(string $content): string {
17-
$tagList = [
17+
static $tagList = [
1818
'base', 'br', 'meta', 'link', 'img', 'input', 'button', 'hr', 'embed',
1919
'param', 'source', 'track', 'area', 'keygen',
2020
];

src/serializer/HTMLSerializer.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
use DOMDocument;
1313

1414
class HTMLSerializer implements Serializer {
15-
private bool $stripRDFaFlag = false;
16-
private bool $keepXMLHeaderFlag = false;
15+
private bool $stripRDFaFlag = false;
16+
17+
private bool $keepXMLHeaderFlag = false;
18+
1719
private bool $namespaceCleaningFlag = true;
18-
private bool $withDoctypeFlag = true;
20+
21+
private bool $withDoctypeFlag = true;
1922

2023
private array $filters = [];
2124

src/serializer/XMLHeaderFilter.php

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
*/
1010
namespace Templado\Engine;
1111

12+
use function preg_replace;
13+
1214
class XMLHeaderFilter implements Filter {
1315
public function apply(string $content): string {
1416
return preg_replace('#^(<\?xml.*\?>\s{0,})#', '', $content);

src/transformation/NamespaceCleaningTransformation.php

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace Templado\Engine;
1111

12+
use function assert;
1213
use DOMAttr;
1314
use DOMDocument;
1415
use DOMElement;
@@ -31,6 +32,7 @@ public function apply(DOMNode $context): void {
3132
$this->enforceProperNamespace($context);
3233
}
3334
}
35+
3436
private function enforceProperNamespace(DOMElement $context): void {
3537
assert($context->ownerDocument instanceof DOMDocument);
3638

src/transformation/TransformationProcessor.php

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace Templado\Engine;
1111

12+
use function assert;
1213
use DOMElement;
1314
use DOMNode;
1415

src/viewmodel/ViewModelRenderer.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212
use function array_key_exists;
1313
use function array_reverse;
14+
use function assert;
15+
use function explode;
1416
use function gettype;
17+
use function implode;
1518
use function is_iterable;
1619
use function is_object;
1720
use function is_string;
1821
use function lcfirst;
1922
use function method_exists;
2023
use function property_exists;
24+
use function sprintf;
2125
use function str_contains;
2226
use DOMDocument;
2327
use DOMElement;
@@ -27,12 +31,16 @@
2731
final class ViewModelRenderer {
2832
/** @psalm-suppress PropertyNotSetInConstructor */
2933
private object $rootModel;
34+
3035
/** @psalm-suppress PropertyNotSetInConstructor */
3136
private DOMNode $pointer;
37+
3238
/** @psalm-suppress PropertyNotSetInConstructor */
3339
private bool $supported;
40+
3441
/** @psalm-suppress PropertyNotSetInConstructor */
3542
private DOMXPath $xp;
43+
3644
private array $prefixModels = [];
3745

3846
public function render(DOMNode $context, object $model): void {
@@ -581,7 +589,7 @@ private function objectApply(DOMElement $context, object $model): void {
581589
$context,
582590
$model,
583591
$name,
584-
\sprintf('Unsupported type "%s" for attribute', gettype($result)),
592+
sprintf('Unsupported type "%s" for attribute', gettype($result)),
585593
),
586594
ViewModelRendererException::WrongTypeForAttribute
587595
);
@@ -643,7 +651,7 @@ private function getModelPath(DOMElement $context): string {
643651
}
644652

645653
private function buildExceptionMessage(?DOMElement $context, object $model, string $method, string $message): string {
646-
return \sprintf(
654+
return sprintf(
647655
'%s: %s (%s)',
648656
$model::class . '::' . $method,
649657
$message,

tests/csrfprotection/CSRFProtectionRendererTest.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
use DOMDocument;
55
use DOMElement;
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\Attributes\UsesClass;
68
use PHPUnit\Framework\TestCase;
79

8-
/**
9-
* @covers \Templado\Engine\CSRFProtectionRenderer
10-
*/
10+
#[CoversClass(CSRFProtectionRenderer::class)]
11+
#[UsesClass(CSRFProtection::class)]
1112
class CSRFProtectionRendererTest extends TestCase {
1213
use DomDocumentsEqualTrait;
1314

@@ -21,11 +22,7 @@ class CSRFProtectionRendererTest extends TestCase {
2122
private $expected;
2223

2324
protected function setUp(): void {
24-
$protection = $this->createMock(CSRFProtection::class);
25-
$protection->method('fieldName')->willReturn('csrf');
26-
$protection->method('tokenValue')->willReturn('secure');
27-
28-
$this->protection = $protection;
25+
$this->protection = new CSRFProtection('csrf', 'secure');
2926
$this->renderer = new CSRFProtectionRenderer();
3027

3128
$this->expected = new DOMDocument();
@@ -39,7 +36,7 @@ public function testUsingContextElementWithoutOwnerDocumentThrowsException(): vo
3936
$this->expectException(CSRFProtectionRendererException::class);
4037
(new CSRFProtectionRenderer())->render(
4138
new DOMElement('dummmy'),
42-
$this->createMock(CSRFProtection::class)
39+
$this->protection
4340
);
4441
}
4542

tests/document/DocumentTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#[UsesClass(Signal::class)]
3535
#[UsesClass(StaticNodeList::class)]
3636
#[UsesClass(TransformationProcessor::class)]
37+
#[UsesClass(CSRFProtection::class)]
3738
#[Small]
3839
class DocumentTest extends TestCase {
3940
use DomDocumentsEqualTrait;
@@ -222,9 +223,7 @@ public function testCSRFProtectionCanBeApplied(): void {
222223
$dom = new DOMDocument();
223224
$dom->loadXML('<?xml version="1.0" ?><html><body><form></form></body></html>');
224225

225-
$protection = $this->createMock(CSRFProtection::class);
226-
$protection->method('fieldName')->willReturn('csrf');
227-
$protection->method('tokenValue')->willReturn('secure');
226+
$protection = new CSRFProtection('csrf', 'secure');
228227

229228
$expected = new DOMDocument();
230229
$expected->loadXML(

0 commit comments

Comments
 (0)