Completed
Push — master ( d8ec9e...ff7676 )
by smiley
02:32
created

HTMLElementTrait::magic_set_src()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Trait HTMLElementTrait
4
 *
5
 * @filesource   HTMLElementTrait.php
6
 * @created      11.05.2017
7
 * @package      chillerlan\PrototypeDOM\Traits
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\PrototypeDOM\Traits;
14
15
use chillerlan\PrototypeDOM\Node\PrototypeHTMLElement;
16
17
/**
18
 * @property string $id
19
 * @property string $class
20
 * @property string $href
21
 * @property string $src
22
 * @property string $innerHTML
23
 */
24
trait HTMLElementTrait{
25
	use Magic, ElementTrait;
26
27 12
	protected function magic_get_id():string {
28 12
		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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
	}
30
31 1
	protected function magic_set_id($id) {
32 1
		return $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33
	}
34
35 6
	protected function magic_get_class():string {
36 6
		return trim($this->getAttribute('class'));
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
	}
38
39 3
	protected function magic_set_class($class) {
40 3
		return $this->setAttribute('class', $class);
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
41
	}
42
43
	protected function magic_get_href():string {
44
		return trim($this->getAttribute('href'));
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
	}
46
47
	protected function magic_set_href($class) {
48
		return $this->setAttribute('href', $class);
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49
	}
50
51
	protected function magic_get_src():string {
52
		return trim($this->getAttribute('src'));
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
53
	}
54
55
	protected function magic_set_src($class) {
56
		return $this->setAttribute('src', $class);
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
57
	}
58
59 3
	protected function magic_get_innerHTML():string {
60 3
		return $this->inspect();
61
	}
62
63
	/**
64
	 * http://api.prototypejs.org/dom/Element/identify/
65
	 *
66
	 * @param string|null $newID
67
	 *
68
	 * @return string
69
	 */
70 1
	public function identify(string $newID = null):string {
71 1
		$oldID = $this->id;
72
73 1
		if(!is_null($newID)){
74 1
			$this->id = $newID;
75
		}
76
77 1
		return $oldID;
78
	}
79
80
	/**
81
	 * @link http://api.prototypejs.org/dom/Element/classNames/
82
	 *
83
	 * @return array
84
	 */
85 6
	public function classNames():array{
86 6
		$currentClassnames = [];
87
88 6
		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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
89 6
			$classnames = explode(' ', $this->class);
90
91 6
			if(!empty($classnames)){
92
93 6
				foreach($classnames as $classname){
94
95 6
					if(empty($classname)){
96 5
						continue;
97
					}
98
99 6
					$currentClassnames[] = $classname;
100
				}
101
102
			}
103
104
		}
105
106 6
		return $currentClassnames;
107
	}
108
109
	/**
110
	 * @link http://api.prototypejs.org/dom/Element/hasClassName/
111
	 *
112
	 * @param string $classname
113
	 *
114
	 * @return bool
115
	 */
116 5
	public function hasClassName(string $classname):bool{
117 5
		return in_array($classname, $this->classNames(), true);
118
	}
119
120
	/**
121
	 * @link http://api.prototypejs.org/dom/Element/addClassName/
122
	 *
123
	 * @param string $classname
124
	 *
125
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
126
	 */
127 2
	public function addClassName(string $classname):PrototypeHTMLElement{
128 2
		return $this->addClassNames([$classname]);
0 ignored issues
show
Bug introduced by
The method addClassNames() does not exist on chillerlan\PrototypeDOM\Traits\HTMLElementTrait. Did you maybe mean classNames()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
129
	}
130
131
	/**
132
	 * @link http://api.prototypejs.org/dom/Element/removeClassName/
133
	 *
134
	 * @param string $classname
135
	 *
136
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
137
	 */
138 2
	public function removeClassName(string $classname):PrototypeHTMLElement{
139 2
		return $this->removeClassNames([$classname]);
0 ignored issues
show
Bug introduced by
The method removeClassNames() does not exist on chillerlan\PrototypeDOM\Traits\HTMLElementTrait. Did you maybe mean classNames()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
140
	}
141
142
	/**
143
	 * @link http://api.prototypejs.org/dom/Element/toggleClassName/
144
	 *
145
	 * @param string $classname
146
	 *
147
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
148
	 */
149 1
	public function toggleClassName(string $classname):PrototypeHTMLElement{
150
151 1
		return $this->hasClassName($classname)
152 1
			? $this->removeClassName($classname)
153 1
			: $this->addClassName($classname);
154
	}
155
156
	/**
157
	 * @link http://api.prototypejs.org/dom/Element/getStyle/
158
	 *
159
	 * @param string $property
160
	 *
161
	 * @return null|string
162
	 */
163 2
	public function getStyle(string $property){
164 2
		$currentStyle = $this->getStyles();
0 ignored issues
show
Bug introduced by
The method getStyles() does not exist on chillerlan\PrototypeDOM\Traits\HTMLElementTrait. Did you maybe mean getStyle()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
165
166 2
		if(array_key_exists(strtolower($property), $currentStyle)){
167 2
			return $currentStyle[$property];
168
		}
169
170 2
		return null;
171
	}
172
173
	/**
174
	 * @link http://api.prototypejs.org/dom/Element/setStyle/
175
	 *
176
	 * @param array $style
177
	 * @param bool  $replace
178
	 *
179
	 * @return \chillerlan\PrototypeDOM\Node\PrototypeHTMLElement
180
	 */
181 2
	public function setStyle(array $style, bool $replace = false):PrototypeHTMLElement{
182 2
		$currentStyle = $this->getStyles();
0 ignored issues
show
Bug introduced by
The method getStyles() does not exist on chillerlan\PrototypeDOM\Traits\HTMLElementTrait. Did you maybe mean getStyle()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
183
184 2
		if(!$replace){
185 2
			$style = array_merge($currentStyle, $style);
186
		}
187
188 2
		foreach($style as $property => $value){
189 2
			$style[$property] = $property.': '.$value.';';
190
		}
191
192 2
		$this->setAttribute('style', implode(' ', $style));
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
193
194 2
		return $this;
195
	}
196
197
198
}
199