CSRFProtectionRenderer::createField()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php declare(strict_types = 1);
2
namespace Templado\Engine;
3
4
use DOMElement;
5
use DOMXPath;
6
7
/** @psalm-suppress MissingConstructor */
8
class CSRFProtectionRenderer {
9
10
    /** @var CSRFProtection */
11
    private $protection;
12
13
    /** @var DOMXPath */
14
    private $xp;
15
16 12
    public function render(DOMElement $context, CSRFProtection $protection): void {
17 12
        $this->protection = $protection;
18 12
        $this->xp         = new DOMXPath($context->ownerDocument);
0 ignored issues
show
Bug introduced by Arne Blankerts
It seems like $context->ownerDocument can also be of type null; however, parameter $document of DOMXPath::__construct() does only seem to accept DOMDocument, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
        $this->xp         = new DOMXPath(/** @scrutinizer ignore-type */ $context->ownerDocument);
Loading history...
19
20 12
        foreach ($context->getElementsByTagName('form') as $form) {
21 12
            $this->getCSRFField($form)->setAttribute(
22 12
                'value',
23 12
                $protection->getTokenValue()
24
            );
25
        }
26 12
    }
27
28 12
    private function getCSRFField(DOMElement $form): DOMElement {
29 12
        $nodeList = $this->xp->query(
30 12
            \sprintf('.//*[local-name() = "input" and @name="%s"]', $this->protection->getFieldName()),
31 4
            $form
32
        );
33
34 12
        if ($nodeList->length === 0) {
35 6
            return $this->createField($form);
36
        }
37
38
        /** @psalm-var \DOMElement */
39 6
        return $nodeList->item(0);
0 ignored issues
show
Bug Best Practice introduced by Arne Blankerts
The expression return $nodeList->item(0) returns the type null which is incompatible with the type-hinted return DOMElement.
Loading history...
40
    }
41
42 6
    private function createField(DOMElement $form): DOMElement {
43 6
        if ($form->namespaceURI !== null) {
44 3
            $input = $form->ownerDocument->createElementNS($form->namespaceURI, 'input');
0 ignored issues
show
Bug introduced by Arne Blankerts
The method createElementNS() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
            /** @scrutinizer ignore-call */ 
45
            $input = $form->ownerDocument->createElementNS($form->namespaceURI, 'input');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
        } else {
46 3
            $input = $form->ownerDocument->createElement('input');
47
        }
48
49 6
        $form->insertBefore($input, $form->firstChild);
50 6
        $input->setAttribute('type', 'hidden');
51 6
        $input->setAttribute('name', $this->protection->getFieldName());
52
53 6
        return $input;
54
    }
55
}
56