Issues (50)

src/Node/PrototypeElementTrait.php (10 issues)

1
<?php
2
/**
3
 * Trait PrototypeElementTrait
4
 *
5
 * @filesource   PrototypeElementTrait.php
6
 * @created      08.05.2017
7
 * @package      chillerlan\PrototypeDOM\Node
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 *
12
 * @noinspection PhpParamsInspection
13
 * @noinspection PhpIncompatibleReturnTypeInspection
14
 */
15
16
namespace chillerlan\PrototypeDOM\Node;
17
18
use function array_key_exists, call_user_func_array, is_array;
19
20
/**
21
 * @implements \chillerlan\PrototypeDOM\Node\PrototypeElement
22
 */
23
trait PrototypeElementTrait{
24
	use PrototypeTraversalTrait;
25
26
	/**
27
	 * @inheritDoc
28
	 */
29
	public function wrap(PrototypeElement $wrapper):PrototypeElement{
30
		return $wrapper->insert($this->replace($wrapper));
31
	}
32
33
	/**
34
	 * @inheritDoc
35
	 */
36
	public function update($content):PrototypeElement{
37
		return $this->purge()->insert($content);
0 ignored issues
show
The method insert() does not exist on chillerlan\PrototypeDOM\Node\PrototypeNode. It seems like you code against a sub-type of chillerlan\PrototypeDOM\Node\PrototypeNode such as chillerlan\PrototypeDOM\Node\PrototypeElement. ( Ignorable by Annotation )

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

37
		return $this->purge()->/** @scrutinizer ignore-call */ insert($content);
Loading history...
38
	}
39
40
	/**
41
	 * @inheritDoc
42
	 */
43
	public function insert($content):PrototypeElement{
44
45
		if(!is_array($content)){
46
47
			foreach($this->ownerDocument->toNodeList($content) as $node){
48
				$this->insert_bottom($node);
0 ignored issues
show
$node of type DOMNode|null is incompatible with the type chillerlan\PrototypeDOM\Node\PrototypeElement expected by parameter $node of chillerlan\PrototypeDOM\...tTrait::insert_bottom(). ( Ignorable by Annotation )

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

48
				$this->insert_bottom(/** @scrutinizer ignore-type */ $node);
Loading history...
49
			}
50
51
			return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\Node\PrototypeElementTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeElement.
Loading history...
52
		}
53
54
		foreach(['before', 'after', 'top', 'bottom'] as $pos){
55
56
			if(!array_key_exists($pos, $content)){
57
				continue;
58
			}
59
60
			$nodes = $this->ownerDocument->toNodeList($content[$pos]);
61
62
			if($pos === 'top' && $this->hasChildNodes() || $pos === 'after' && $this->nextSibling){
0 ignored issues
show
Consider adding parentheses for clarity. Current Interpretation: ($pos === 'top' && $this...' && $this->nextSibling, Probably Intended Meaning: $pos === 'top' && ($this... && $this->nextSibling)
Loading history...
It seems like hasChildNodes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

62
			if($pos === 'top' && $this->/** @scrutinizer ignore-call */ hasChildNodes() || $pos === 'after' && $this->nextSibling){
Loading history...
63
				$nodes->reverse();
64
			}
65
66
			foreach($nodes as $node){
67
				call_user_func_array([$this, 'insert_'.$pos], [$node]);
68
			}
69
70
		}
71
72
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\Node\PrototypeElementTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeElement.
Loading history...
73
	}
74
75
	/**
76
	 * @inheritDoc
77
	 */
78
	public function insert_before(PrototypeElement $node, PrototypeElement $refNode = null):PrototypeElement{
79
80
		if($this->parentNode){
81
			$this->parentNode->insertBefore($this->importNode($node), $refNode ?? $this);
0 ignored issues
show
It seems like $refNode ?? $this can also be of type chillerlan\PrototypeDOM\Node\PrototypeElementTrait; however, parameter $child of DOMNode::insertBefore() does only seem to accept DOMNode|null, 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

81
			$this->parentNode->insertBefore($this->importNode($node), /** @scrutinizer ignore-type */ $refNode ?? $this);
Loading history...
82
		}
83
84
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\Node\PrototypeElementTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeElement.
Loading history...
85
	}
86
87
	/**
88
	 * @inheritDoc
89
	 */
90
	public function insert_after(PrototypeElement $node):PrototypeElement{
91
92
		if(!$this->nextSibling && $this->parentNode){
93
			return $this->parentNode->insert_bottom($node); // @codeCoverageIgnore
94
		}
95
96
		return $this->nextSibling->insert_before($node);
97
	}
98
99
	/**
100
	 * @inheritDoc
101
	 */
102
	public function insert_top(PrototypeElement $node):PrototypeElement{
103
104
		if($this->hasChildNodes()){
105
			return $this->firstChild->insert_before($node, $this->firstChild);
106
		}
107
108
		return $this->insert_bottom($node);
109
	}
110
111
	/**
112
	 * @inheritDoc
113
	 */
114
	public function insert_bottom(PrototypeElement $node):PrototypeElement{
115
		$this->appendChild($this->importNode($node));
0 ignored issues
show
It seems like appendChild() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

115
		$this->/** @scrutinizer ignore-call */ 
116
         appendChild($this->importNode($node));
Loading history...
116
117
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\Node\PrototypeElementTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeElement.
Loading history...
118
	}
119
120
}
121