Passed
Push — master ( 6f3615...06ac50 )
by Josh
01:46
created

PolyfillMethods::_insertAdjacentText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php declare(strict_types=1);
2
3
/**
4
* @package   s9e\SweetDOM
5
* @copyright Copyright (c) The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\SweetDOM\NodeTraits;
9
10
use DOMElement;
11
use DOMException;
12
use DOMNode;
13
use const DOM_SYNTAX_ERR;
14
use function preg_match, strtolower;
15
16
trait PolyfillMethods
17
{
18
	use MagicMethods
19
	{
20
		MagicMethods::__call as magicMethodsCall;
21
	}
22
23
	public function __call(string $name, array $arguments)
24
	{
25
		if (preg_match('(^insertAdjacent(?:Element|Text)$)i', $name))
26
		{
27
			$methodName = '_' . $name;
28
29
			return $this->$methodName(...$arguments);
30
		}
31
32
		return $this->magicMethodsCall($name, $arguments);
0 ignored issues
show
Bug introduced by
The method magicMethodsCall() does not exist on s9e\SweetDOM\NodeTraits\PolyfillMethods. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

32
		return $this->/** @scrutinizer ignore-call */ magicMethodsCall($name, $arguments);
Loading history...
33
	}
34
35
	/**
36
	* Insert given node relative to this element's position
37
	*
38
	* @param  string  $where One of 'beforebegin', 'afterbegin', 'beforeend', 'afterend'
39
	* @param  DOMNode $node
40
	* @return void
41
	*/
42
	protected function insertAdjacentNode(string $where, DOMNode $node): void
43
	{
44
		match (strtolower($where))
45
		{
46
			'beforebegin' => $this->parentNode?->insertBefore($node, $this),
47
			'beforeend'   => $this->appendChild($node),
1 ignored issue
show
Bug introduced by
The method appendChild() does not exist on s9e\SweetDOM\NodeTraits\PolyfillMethods. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

47
			'beforeend'   => $this->/** @scrutinizer ignore-call */ appendChild($node),
Loading history...
48
			'afterend'    => $this->parentNode?->insertBefore($node, $this->nextSibling),
49
			'afterbegin'  => $this->insertBefore($node, $this->firstChild),
1 ignored issue
show
Bug introduced by
The method insertBefore() does not exist on s9e\SweetDOM\NodeTraits\PolyfillMethods. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

49
			'afterbegin'  => $this->/** @scrutinizer ignore-call */ insertBefore($node, $this->firstChild),
Loading history...
50
			default       => throw new DOMException("'$where' is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'", DOM_SYNTAX_ERR)
51
		};
52
	}
53
54
	private function _insertAdjacentElement(string $where, DOMElement $element): DOMElement
55
	{
56
		$this->insertAdjacentNode($where, $element);
57
58
		return $element;
59
	}
60
61
	private function _insertAdjacentText(string $where, string $text): void
62
	{
63
		$node = $this->ownerDocument->createTextNode($text);
64
		$this->insertAdjacentNode($where, $node);
65
	}
66
}