Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
theseer committed Aug 9, 2023
1 parent aaa435d commit 1237a5a
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
namespace Templado\Engine;

use function implode;
use function preg_match;
use InvalidArgumentException;

final readonly class Id {
Expand Down
2 changes: 2 additions & 0 deletions src/StaticNodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
namespace Templado\Engine;

use function array_values;
use function count;
use function iterator_to_array;
use ArrayIterator;
use Countable;
Expand Down
13 changes: 5 additions & 8 deletions src/csrfprotection/CSRFProtection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
*/
namespace Templado\Engine;

class CSRFProtection {
private readonly string $fieldName;

private readonly string $tokenValue;

public function __construct(string $fieldName, string $tokenValue) {
$this->fieldName = $fieldName;
$this->tokenValue = $tokenValue;
final readonly class CSRFProtection {
public function __construct(
private string $fieldName,
private string $tokenValue
) {
}

public function fieldName(): string {
Expand Down
4 changes: 3 additions & 1 deletion src/csrfprotection/CSRFProtectionRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace Templado\Engine;

use function assert;
use function sprintf;
use DOMDocument;
use DOMElement;
Expand All @@ -19,7 +20,8 @@ final class CSRFProtectionRenderer {
private CSRFProtection $protection;

private DOMDocument $dom;
private DOMXPath$xp;

private DOMXPath $xp;

public function render(DOMElement $context, CSRFProtection $protection): void {
$this->protection = $protection;
Expand Down
2 changes: 2 additions & 0 deletions src/document/DocumentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
namespace Templado\Engine;

use function array_push;
use function count;
use ArrayIterator;
use Countable;
use IteratorAggregate;
Expand Down
1 change: 0 additions & 1 deletion src/formdata/FormData.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

class FormData {
private readonly string $identifier;

private readonly array $values;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/merger/MergeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
namespace Templado\Engine;

use function count;
use function sprintf;
use ArrayIterator;
use DOMDocument;

Expand Down
8 changes: 4 additions & 4 deletions src/merger/Merger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
namespace Templado\Engine;

use function assert;
use function sprintf;
use DOMDocument;
use DOMElement;
use DOMXPath;
Expand Down Expand Up @@ -90,7 +92,7 @@ private function isConnected(DOMElement $context, DOMElement $contextChild): boo
return false;
}

private function mergeIn(DOMElement $contextChild, DOMElement $import): DOMElement {
private function mergeIn(DOMElement $contextChild, DOMElement $import): void {
$workContext = [$import];

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

return $import;
return;
}

$contextChild->append(...$workContext);

return $import;
}

private function shouldReplaceCurrent(DOMElement $import, DOMElement $contextChild): bool {
Expand Down
12 changes: 5 additions & 7 deletions src/selectors/XPathSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
use DOMXPath;

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

/** @var array<string, string> */
private $prefixMap = [];

public function __construct(string $query) {
$this->queryString = $query;
public function __construct(
private string $queryString
) {
}

public function registerPrefix(string $prefix, string $uri): void {
Expand Down
2 changes: 1 addition & 1 deletion src/serializer/EmptyElementsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class EmptyElementsFilter implements Filter {
public function apply(string $content): string {
$tagList = [
static $tagList = [
'base', 'br', 'meta', 'link', 'img', 'input', 'button', 'hr', 'embed',
'param', 'source', 'track', 'area', 'keygen',
];
Expand Down
9 changes: 6 additions & 3 deletions src/serializer/HTMLSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
use DOMDocument;

class HTMLSerializer implements Serializer {
private bool $stripRDFaFlag = false;
private bool $keepXMLHeaderFlag = false;
private bool $stripRDFaFlag = false;

private bool $keepXMLHeaderFlag = false;

private bool $namespaceCleaningFlag = true;
private bool $withDoctypeFlag = true;

private bool $withDoctypeFlag = true;

private array $filters = [];

Expand Down
2 changes: 2 additions & 0 deletions src/serializer/XMLHeaderFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
namespace Templado\Engine;

use function preg_replace;

class XMLHeaderFilter implements Filter {
public function apply(string $content): string {
return preg_replace('#^(<\?xml.*\?>\s{0,})#', '', $content);
Expand Down
2 changes: 2 additions & 0 deletions src/transformation/NamespaceCleaningTransformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace Templado\Engine;

use function assert;
use DOMAttr;
use DOMDocument;
use DOMElement;
Expand All @@ -31,6 +32,7 @@ public function apply(DOMNode $context): void {
$this->enforceProperNamespace($context);
}
}

private function enforceProperNamespace(DOMElement $context): void {
assert($context->ownerDocument instanceof DOMDocument);

Expand Down
1 change: 1 addition & 0 deletions src/transformation/TransformationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
namespace Templado\Engine;

use function assert;
use DOMElement;
use DOMNode;

Expand Down
12 changes: 10 additions & 2 deletions src/viewmodel/ViewModelRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

use function array_key_exists;
use function array_reverse;
use function assert;
use function explode;
use function gettype;
use function implode;
use function is_iterable;
use function is_object;
use function is_string;
use function lcfirst;
use function method_exists;
use function property_exists;
use function sprintf;
use function str_contains;
use DOMDocument;
use DOMElement;
Expand All @@ -27,12 +31,16 @@
final class ViewModelRenderer {
/** @psalm-suppress PropertyNotSetInConstructor */
private object $rootModel;

/** @psalm-suppress PropertyNotSetInConstructor */
private DOMNode $pointer;

/** @psalm-suppress PropertyNotSetInConstructor */
private bool $supported;

/** @psalm-suppress PropertyNotSetInConstructor */
private DOMXPath $xp;

private array $prefixModels = [];

public function render(DOMNode $context, object $model): void {
Expand Down Expand Up @@ -581,7 +589,7 @@ private function objectApply(DOMElement $context, object $model): void {
$context,
$model,
$name,
\sprintf('Unsupported type "%s" for attribute', gettype($result)),
sprintf('Unsupported type "%s" for attribute', gettype($result)),
),
ViewModelRendererException::WrongTypeForAttribute
);
Expand Down Expand Up @@ -643,7 +651,7 @@ private function getModelPath(DOMElement $context): string {
}

private function buildExceptionMessage(?DOMElement $context, object $model, string $method, string $message): string {
return \sprintf(
return sprintf(
'%s: %s (%s)',
$model::class . '::' . $method,
$message,
Expand Down
15 changes: 6 additions & 9 deletions tests/csrfprotection/CSRFProtectionRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

use DOMDocument;
use DOMElement;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

/**
* @covers \Templado\Engine\CSRFProtectionRenderer
*/
#[CoversClass(CSRFProtectionRenderer::class)]
#[UsesClass(CSRFProtection::class)]
class CSRFProtectionRendererTest extends TestCase {
use DomDocumentsEqualTrait;

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

protected function setUp(): void {
$protection = $this->createMock(CSRFProtection::class);
$protection->method('fieldName')->willReturn('csrf');
$protection->method('tokenValue')->willReturn('secure');

$this->protection = $protection;
$this->protection = new CSRFProtection('csrf', 'secure');
$this->renderer = new CSRFProtectionRenderer();

$this->expected = new DOMDocument();
Expand All @@ -39,7 +36,7 @@ public function testUsingContextElementWithoutOwnerDocumentThrowsException(): vo
$this->expectException(CSRFProtectionRendererException::class);
(new CSRFProtectionRenderer())->render(
new DOMElement('dummmy'),
$this->createMock(CSRFProtection::class)
$this->protection
);
}

Expand Down
5 changes: 2 additions & 3 deletions tests/document/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#[UsesClass(Signal::class)]
#[UsesClass(StaticNodeList::class)]
#[UsesClass(TransformationProcessor::class)]
#[UsesClass(CSRFProtection::class)]
#[Small]
class DocumentTest extends TestCase {
use DomDocumentsEqualTrait;
Expand Down Expand Up @@ -222,9 +223,7 @@ public function testCSRFProtectionCanBeApplied(): void {
$dom = new DOMDocument();
$dom->loadXML('<?xml version="1.0" ?><html><body><form></form></body></html>');

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

$expected = new DOMDocument();
$expected->loadXML(
Expand Down

0 comments on commit 1237a5a

Please sign in to comment.