Passed
Push — master ( 8e4c14...93bf08 )
by smiley
06:58
created

PrototypeHTMLElementTrait::magic_set_class()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Trait PrototypeHTMLElementTrait
4
 *
5
 * @filesource   PrototypeHTMLElementTrait.php
6
 * @created      11.05.2017
7
 * @package      chillerlan\PrototypeDOM\Node
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 *
12
 * @noinspection PhpIncompatibleReturnTypeInspection
13
 */
14
15
namespace chillerlan\PrototypeDOM\Node;
16
17
use function array_key_exists, array_merge, explode, implode, in_array, strtolower, trim;
18
19
/**
20
 *
21
 */
22
trait PrototypeHTMLElementTrait{
23
	use PrototypeElementTrait;
24
25
	public function getID():string{
26
		return trim($this->getAttribute('id'));
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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

26
		return trim($this->/** @scrutinizer ignore-call */ getAttribute('id'));
Loading history...
27
	}
28
29
	public function setID(string $id):PrototypeHTMLElement{
30
		$this->setAttribute('id', $id);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() 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

30
		$this->/** @scrutinizer ignore-call */ 
31
         setAttribute('id', $id);
Loading history...
31
32
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\...ototypeHTMLElementTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeHTMLElement.
Loading history...
33
	}
34
35
	public function getClassName():string{
36
		return trim($this->getAttribute('class'));
37
	}
38
39
	public function setClassName(string $class):PrototypeHTMLElement{
40
		$this->setAttribute('class', $class);
41
42
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\...ototypeHTMLElementTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeHTMLElement.
Loading history...
43
	}
44
45
	public function getHref():string{
46
		return trim($this->getAttribute('href'));
47
	}
48
49
	public function setHref(string $href):PrototypeHTMLElement{
50
		$this->setAttribute('href', $href);
51
52
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\...ototypeHTMLElementTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeHTMLElement.
Loading history...
53
	}
54
55
	public function getSrc():string{
56
		return trim($this->getAttribute('src'));
57
	}
58
59
	public function setSrc(string $src):PrototypeHTMLElement{
60
		$this->setAttribute('src', $src);
61
62
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\...ototypeHTMLElementTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeHTMLElement.
Loading history...
63
	}
64
65
	/**
66
	 * @inheritDoc
67
	 */
68
	public function identify(string $newID = null):string{
69
		$oldID = $this->getAttribute('id');
70
71
		if($newID !== null){
72
			$this->setAttribute('id', $newID);
73
		}
74
75
		return $oldID;
76
	}
77
78
	/**
79
	 * @inheritDoc
80
	 */
81
	public function classNames():array{
82
83
		if(!$this->hasAttributes()){
0 ignored issues
show
Bug introduced by
It seems like hasAttributes() 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

83
		if(!$this->/** @scrutinizer ignore-call */ hasAttributes()){
Loading history...
84
			return [];
85
		}
86
87
		$classnames        = explode(' ', $this->getClassName());
88
		$currentClassnames = [];
89
90
		foreach($classnames as $classname){
91
			$classname = trim($classname);
92
93
			if(!empty($classname)){
94
				$currentClassnames[] = $classname;
95
			}
96
97
		}
98
99
		return $currentClassnames;
100
	}
101
102
	/**
103
	 * @inheritDoc
104
	 */
105
	public function hasClassName(string $classname):bool{
106
		return in_array($classname, $this->classNames(), true);
107
	}
108
109
	/**
110
	 * @inheritDoc
111
	 */
112
	public function addClassName(string $classname):PrototypeHTMLElement{
113
		return $this->addClassNames([$classname]);
0 ignored issues
show
Bug introduced by
The method addClassNames() does not exist on chillerlan\PrototypeDOM\...ototypeHTMLElementTrait. Did you maybe mean addClassName()? ( Ignorable by Annotation )

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

113
		return $this->/** @scrutinizer ignore-call */ addClassNames([$classname]);

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...
114
	}
115
116
	/**
117
	 * @inheritDoc
118
	 */
119
	public function removeClassName(string $classname):PrototypeHTMLElement{
120
		return $this->removeClassNames([$classname]);
0 ignored issues
show
Bug introduced by
The method removeClassNames() does not exist on chillerlan\PrototypeDOM\...ototypeHTMLElementTrait. Did you maybe mean remove()? ( Ignorable by Annotation )

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

120
		return $this->/** @scrutinizer ignore-call */ removeClassNames([$classname]);

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...
121
	}
122
123
	/**
124
	 * @inheritDoc
125
	 */
126
	public function toggleClassName(string $classname):PrototypeHTMLElement{
127
128
		if($this->hasClassName($classname)){
129
			return $this->removeClassName($classname);
130
		}
131
132
		return $this->addClassName($classname);
133
	}
134
135
	/**
136
	 * @inheritDoc
137
	 */
138
	public function getStyle(string $property):?string{
139
		$currentStyle = $this->getStyles();
0 ignored issues
show
Bug introduced by
The method getStyles() does not exist on chillerlan\PrototypeDOM\...ototypeHTMLElementTrait. Did you maybe mean getStyle()? ( Ignorable by Annotation )

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

139
		/** @scrutinizer ignore-call */ 
140
  $currentStyle = $this->getStyles();

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...
140
141
		if(array_key_exists(strtolower($property), $currentStyle)){
142
			return $currentStyle[$property];
143
		}
144
145
		return null;
146
	}
147
148
	/**
149
	 * @inheritDoc
150
	 */
151
	public function setStyle(array $style, bool $replace = null):PrototypeHTMLElement{
152
		$currentStyle = $this->getStyles();
153
154
		if($replace !== true){
155
			$style = array_merge($currentStyle, $style);
156
		}
157
158
		foreach($style as $property => $value){
159
			$style[$property] = $property.': '.$value.';';
160
		}
161
162
		$this->setAttribute('style', implode(' ', $style));
163
164
		return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type chillerlan\PrototypeDOM\...ototypeHTMLElementTrait which is incompatible with the type-hinted return chillerlan\PrototypeDOM\Node\PrototypeHTMLElement.
Loading history...
165
	}
166
167
}
168