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

DeprecatedMethods::insertAdjacentNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 9
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 DOMException;
11
use const ENT_COMPAT, ENT_XML1;
12
use function array_flip, htmlspecialchars, preg_match, preg_match_all, preg_replace_callback, strtolower;
13
14
trait DeprecatedMethods
15
{
16
	use PolyfillMethods
17
	{
18
		PolyfillMethods::__call as polyfillMethodsCall;
19
	}
20
21
	public function __call(string $name, array $arguments)
22
	{
23
		if (preg_match('(^(ap|pre)pendText(Sibling|)$)i', $name, $m))
24
		{
25
			$methodName = [
26
				'ap'         => 'append',
27
				'pre'        => 'prepend',
28
				'apsibling'  => 'after',
29
				'presibling' => 'before'
30
			][strtolower($m[1] . $m[2])];
31
32
			return $this->$methodName(...$arguments);
33
		}
34
		if (preg_match('(^(ap|pre)pend(\\w+)Sibling$)i', $name, $m))
35
		{
36
			$name = ['ap' => 'after', 'pre' => 'before'][strtolower($m[1])] . $m[2];
37
		}
38
39
		return $this->polyfillMethodsCall($name, $arguments);
1 ignored issue
show
Bug introduced by
The method polyfillMethodsCall() 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

39
		return $this->/** @scrutinizer ignore-call */ polyfillMethodsCall($name, $arguments);
Loading history...
40
	}
41
42
	/**
43
	* @deprecated
44
	*/
45
	public function insertAdjacentXML(string $where, string $xml): void
46
	{
47
		$fragment = $this->ownerDocument->createDocumentFragment();
48
		$fragment->appendXML($this->addMissingNamespaceDeclarations($xml));
49
50
		$this->insertAdjacentNode($where, $fragment);
51
	}
52
53
	/**
54
	* Add namespace declarations that may be missing in given XML
55
	*
56
	* @param  string $xml Original XML
57
	* @return string      Modified XML
58
	*/
59
	protected function addMissingNamespaceDeclarations(string $xml): string
60
	{
61
		preg_match_all('(xmlns:\\K[-\\w]++(?==))', $xml, $m);
62
		$prefixes = array_flip($m[0]);
63
64
		return preg_replace_callback(
65
			'(<([-\\w]++):[^>]*?\\K\\s*/?>)',
66
			function ($m) use ($prefixes)
67
			{
68
				$return = $m[0];
69
				$prefix = $m[1];
70
				if (!isset($prefixes[$prefix]))
71
				{
72
					$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

72
					/** @scrutinizer ignore-call */ 
73
     $nsURI  = $this->lookupNamespaceURI($prefix);
Loading history...
73
					$return = ' xmlns:' . $prefix . '="' . htmlspecialchars($nsURI, ENT_COMPAT | ENT_XML1) . '"' . $return;
1 ignored issue
show
Bug introduced by
It seems like $nsURI can also be of type DOMNode; however, parameter $string of htmlspecialchars() does only seem to accept string, 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

73
					$return = ' xmlns:' . $prefix . '="' . htmlspecialchars(/** @scrutinizer ignore-type */ $nsURI, ENT_COMPAT | ENT_XML1) . '"' . $return;
Loading history...
74
				}
75
76
				return $return;
77
			},
78
			$xml
79
		);
80
	}
81
}