Passed
Push — master ( 482d42...6f3615 )
by Josh
02:01
created

DeprecatedMethods   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 41
c 1
b 0
f 0
dl 0
loc 104
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A _insertAdjacentText() 0 4 1
A addMissingNamespaceDeclarations() 0 20 2
A _insertAdjacentElement() 0 5 1
A insertAdjacentNode() 0 9 1
A __call() 0 25 4
A insertAdjacentXML() 0 6 1
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 BadMethodCallException;
11
use DOMElement;
12
use DOMException;
13
use DOMNode;
14
use DOMNodeList;
15
use DOMText;
16
use const DOM_SYNTAX_ERR, ENT_NOQUOTES, ENT_XML1;
17
use function array_flip, call_user_func_array, htmlspecialchars, is_callable, preg_match, preg_match_all, preg_replace_callback, strpos, strtolower, substr, ucfirst;
18
19
trait DeprecatedMethods
20
{
21
	use MagicMethods
22
	{
23
		MagicMethods::__call as magicMethodsCall;
24
	}
25
26
	public function __call(string $name, array $arguments)
27
	{
28
		if (preg_match('(^insertAdjacent(?:Element|Text)$)i', $name))
29
		{
30
			$methodName = '_' . $name;
31
32
			return $this->$methodName(...$arguments);
33
		}
34
		if (preg_match('(^(ap|pre)pendText(Sibling|)$)i', $name, $m))
35
		{
36
			$methodName = [
37
				'ap'         => 'append',
38
				'pre'        => 'prepend',
39
				'apsibling'  => 'after',
40
				'presibling' => 'before'
41
			][strtolower($m[1] . $m[2])];
42
43
			return $this->$methodName(...$arguments);
44
		}
45
		if (preg_match('(^(ap|pre)pend(\\w+)Sibling$)i', $name, $m))
46
		{
47
			$name = ['ap' => 'after', 'pre' => 'before'][strtolower($m[1])] . $m[2];
48
		}
49
50
		return $this->magicMethodsCall($name, $arguments);
1 ignored issue
show
Bug introduced by
The method magicMethodsCall() does not exist on s9e\SweetDOM\NodeTraits\DeprecatedMethods. 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

50
		return $this->/** @scrutinizer ignore-call */ magicMethodsCall($name, $arguments);
Loading history...
51
	}
52
53
	/**
54
	* @deprecated
55
	*/
56
	public function insertAdjacentXML(string $where, string $xml): void
57
	{
58
		$fragment = $this->ownerDocument->createDocumentFragment();
59
		$fragment->appendXML($this->addMissingNamespaceDeclarations($xml));
60
61
		$this->insertAdjacentNode($where, $fragment);
62
	}
63
64
	/**
65
	* Add namespace declarations that may be missing in given XML
66
	*
67
	* @param  string $xml Original XML
68
	* @return string      Modified XML
69
	*/
70
	protected function addMissingNamespaceDeclarations(string $xml): string
71
	{
72
		preg_match_all('(xmlns:\\K[-\\w]++(?==))', $xml, $m);
73
		$prefixes = array_flip($m[0]);
74
75
		return preg_replace_callback(
76
			'(<([-\\w]++):[^>]*?\\K\\s*/?>)',
77
			function ($m) use ($prefixes)
78
			{
79
				$return = $m[0];
80
				$prefix = $m[1];
81
				if (!isset($prefixes[$prefix]))
82
				{
83
					$nsURI  = $this->lookupNamespaceURI($prefix);
1 ignored issue
show
Bug introduced by
The method lookupNamespaceURI() does not exist on s9e\SweetDOM\NodeTraits\DeprecatedMethods. 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

83
					/** @scrutinizer ignore-call */ 
84
     $nsURI  = $this->lookupNamespaceURI($prefix);
Loading history...
84
					$return = ' xmlns:' . $prefix . '="' . htmlspecialchars($nsURI, ENT_XML1) . '"' . $return;
85
				}
86
87
				return $return;
88
			},
89
			$xml
90
		);
91
	}
92
93
	/**
94
	* Insert given node relative to this element's position
95
	*
96
	* @param  string  $where One of 'beforebegin', 'afterbegin', 'beforeend', 'afterend'
97
	* @param  DOMNode $node
98
	* @return void
99
	*/
100
	protected function insertAdjacentNode(string $where, DOMNode $node): void
101
	{
102
		match (strtolower($where))
103
		{
104
			'beforebegin' => $this->parentNode?->insertBefore($node, $this),
105
			'beforeend'   => $this->appendChild($node),
1 ignored issue
show
Bug introduced by
The method appendChild() does not exist on s9e\SweetDOM\NodeTraits\DeprecatedMethods. 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

105
			'beforeend'   => $this->/** @scrutinizer ignore-call */ appendChild($node),
Loading history...
106
			'afterend'    => $this->parentNode?->insertBefore($node, $this->nextSibling),
107
			'afterbegin'  => $this->insertBefore($node, $this->firstChild),
1 ignored issue
show
Bug introduced by
The method insertBefore() does not exist on s9e\SweetDOM\NodeTraits\DeprecatedMethods. 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

107
			'afterbegin'  => $this->/** @scrutinizer ignore-call */ insertBefore($node, $this->firstChild),
Loading history...
108
			default       => throw new DOMException("'$where' is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'", DOM_SYNTAX_ERR)
109
		};
110
	}
111
112
	private function _insertAdjacentElement(string $where, self $element): self
113
	{
114
		$this->insertAdjacentNode($where, $element);
0 ignored issues
show
Bug introduced by
$element of type s9e\SweetDOM\NodeTraits\DeprecatedMethods is incompatible with the type DOMNode expected by parameter $node of s9e\SweetDOM\NodeTraits\...s::insertAdjacentNode(). ( Ignorable by Annotation )

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

114
		$this->insertAdjacentNode($where, /** @scrutinizer ignore-type */ $element);
Loading history...
115
116
		return $element;
117
	}
118
119
	private function _insertAdjacentText(string $where, string $text): void
120
	{
121
		$node = $this->ownerDocument->createTextNode($text);
122
		$this->insertAdjacentNode($where, $node);
123
	}
124
}