Completed
Push — master ( 6831db...b65ced )
by Jean-Christophe
03:10
created

BaseHtmlPropertiesTrait::getProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Ajax\common\html\traits;
3
4
use Ajax\service\JString;
5
6
/**
7
 * @author jc
8
 *
9
 */
10
trait BaseHtmlPropertiesTrait{
11
12
	protected $properties=array ();
13
	abstract protected function ctrl($name, $value, $typeCtrl);
14
	public function getProperties() {
15
		return $this->properties;
16
	}
17
18
	public function setProperties($properties) {
19
		$this->properties=$properties;
20
		return $this;
21
	}
22
23
	public function setProperty($name, $value) {
24
		$this->properties[$name]=$value;
25
		return $this;
26
	}
27
28
	public function getProperty($name) {
29
		if (array_key_exists($name, $this->properties))
30
			return $this->properties[$name];
31
	}
32
33
	public function addToProperty($name, $value, $separator=" ") {
34
		if (\is_array($value)) {
35
			foreach ( $value as $v ) {
36
				$this->addToProperty($name, $v, $separator);
37
			}
38
		} else if ($value !== "" && $this->propertyContains($name, $value) === false) {
39
			$v=@$this->properties[$name];
40
			if (isset($v) && $v !== "")
41
				$v=$v . $separator . $value;
42
				else
43
					$v=$value;
44
45
					return $this->setProperty($name, $v);
46
		}
47
		return $this;
48
	}
49
50
	public function addProperties($properties) {
51
		$this->properties=array_merge($this->properties, $properties);
52
		return $this;
53
	}
54
55
	protected function removePropertyValue($name, $value) {
56
		$this->properties[$name]=\str_replace($value, "", $this->properties[$name]);
57
		return $this;
58
	}
59
60
	protected function removePropertyValues($name, $values) {
61
		$this->removeOldValues($this->properties[$name], $values);
0 ignored issues
show
Bug introduced by
It seems like removeOldValues() 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...
62
		return $this;
63
	}
64
65
	protected function addToPropertyUnique($name, $value, $typeCtrl) {
66
		if (@class_exists($typeCtrl, true))
67
			$typeCtrl=$typeCtrl::getConstants();
68
			if (\is_array($typeCtrl)) {
69
				$this->removeOldValues($this->properties[$name], $typeCtrl);
0 ignored issues
show
Bug introduced by
It seems like removeOldValues() 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...
70
			}
71
			return $this->addToProperty($name, $value);
72
	}
73
74
	public function addToPropertyCtrl($name, $value, $typeCtrl) {
75
		return $this->addToPropertyUnique($name, $value, $typeCtrl);
76
	}
77
78
	public function addToPropertyCtrlCheck($name, $value, $typeCtrl) {
79
		if ($this->ctrl($name, $value, $typeCtrl) === true) {
80
			return $this->addToProperty($name, $value);
81
		}
82
		return $this;
83
	}
84
85
	public function removeProperty($name) {
86
		if (\array_key_exists($name, $this->properties))
87
			unset($this->properties[$name]);
88
			return $this;
89
	}
90
91
	public function propertyContains($propertyName, $value) {
92
		$values=$this->getProperty($propertyName);
93
		if (isset($values)) {
94
			return JString::contains($values, $value);
95
		}
96
		return false;
97
	}
98
99
	protected function setPropertyCtrl($name, $value, $typeCtrl) {
100
		if ($this->ctrl($name, $value, $typeCtrl) === true)
101
			return $this->setProperty($name, $value);
102
			return $this;
103
	}
104
105
	protected function getElementByPropertyValue($propertyName,$value, $elements) {
106
		if (\is_array($elements)) {
107
			$flag=false;
108
			$index=0;
109
			while ( !$flag && $index < sizeof($elements) ) {
110
				if ($elements[$index] instanceof BaseHtml)
0 ignored issues
show
Bug introduced by
The class Ajax\common\html\traits\BaseHtml does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
111
					$flag=($elements[$index]->propertyContains($propertyName, $value) === true);
112
					$index++;
113
			}
114
			if ($flag === true)
115
				return $elements[$index - 1];
116
		} elseif ($elements instanceof BaseHtml) {
0 ignored issues
show
Bug introduced by
The class Ajax\common\html\traits\BaseHtml does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
117
			if ($elements->propertyContains($propertyName, $value) === true)
118
				return $elements;
119
		}
120
		return null;
121
	}
122
}