SerializableObject   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 99
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 3 1
A properties() 0 11 3
A add() 0 13 2
A getProperty() 0 7 2
A setProperty() 0 4 1
A hasProperty() 0 3 1
B search() 0 20 8
B searchArray() 0 21 6
1
<?php
2
namespace Ext;
3
4
class SerializableObject implements Serializable
5
{
6
	/** @var array  */
7
	private $properties = array();
8
9
	/** @var array  */
10
	protected $ignore = array();
11
12
	public function serialize(Serializer $serializer) {
13
		return $serializer->serialize($this);
14
	}
15
16
	public function properties() {
17
		$properties = array();
18
19
		foreach($this->properties as $key => $value) {
20
			if (!in_array($key, $this->ignore)){
21
				$properties[$key] = $value;
22
			}
23
		}
24
25
		return $properties;
26
	}
27
28
	protected function add($key, $item) {
29
		$values = array();
30
31
		if (array_key_exists($key, $this->properties)) {
32
			$values = $this->properties[$key];
33
		}
34
35
		$values[] = $item;
36
37
		$this->properties[$key] = $values;
38
39
		return $this;
40
	}
41
42
	protected function getProperty($key) {
43
		if ($this->hasProperty($key)) {
44
			return $this->properties[$key];
45
		}
46
47
		return null;
48
	}
49
50
	protected function setProperty($key, $value) {
51
		$this->properties[$key] = $value;
52
		return $this;
53
	}
54
55
	protected function hasProperty($key) {
56
		return array_key_exists($key, $this->properties);
57
	}
58
59
	public static function search($component,$class='\Ext\Base',$property=null,$propertyValue=null,$findFirst = false){
60
		$return = [];
61
		if(is_a($component, $class) and ($property==null or ($property!=null and $component->getProperty($property)==$propertyValue))){
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
62
			if($findFirst){
63
				return $component;
64
			}else{
65
				$return[] = $component;
66
			}
67
		}
68
69
		if($foundComponents = static::searchArray($component->properties(),$class,$property,$propertyValue,$findFirst)){
70
			if($findFirst){
71
				return $foundComponents;
72
			}else{
73
				$return = array_merge($return,$foundComponents);
74
			}
75
76
		}
77
		return $return;
78
	}
79
80
	protected static function searchArray($array,$class='\Ext\Base',$property=null,$propertyValue='',$findFirst = false){
81
		$return = [];
82
		foreach($array as $element){
83
			$foundComponent = null;
84
			if($element instanceof SerializableObject){
85
				$foundComponent = static::search($element,$class,$property,$propertyValue,$findFirst);
86
			}elseif(is_array($element)){
87
				$foundComponent = static::searchArray($element,$class,$property,$propertyValue,$findFirst);
88
			}
89
90
			if($foundComponent) {
91
				if ($findFirst) {
92
					$return = $foundComponent;
93
					break;
94
				} else {
95
					$return = array_merge($return, $foundComponent);
96
				}
97
			}
98
		}
99
		return $return;
100
	}
101
102
}