ArrayAccessTrait::offsetSet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4286
cc 2
eloc 5
nc 2
nop 2
1
<?php namespace Knot\Dict;
2
3
trait ArrayAccessTrait {
4
5
	abstract public function __unset($index);
6
7
8
	abstract public function __isset($index);
9
10
11
	abstract public function lastKey();
12
13
14
	/**
15
	 * @param mixed $offset
16
	 *
17
	 * @return boolean
18
	 */
19
	public function offsetExists($offset)
20
	{
21
		return $this->__isset($offset);
22
	}
23
24
25
	/**
26
	 * @param mixed $offset
27
	 *
28
	 * @return mixed
29
	 */
30
	public function &offsetGet($offset = null)
31
	{
32
		if ( count(func_get_args()) == 0 or is_null($offset) )
0 ignored issues
show
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...
33
		{
34
			$this->data[] = [ ];
0 ignored issues
show
Bug introduced by
The property data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
36
			return $this->data[$this->lastKey()];
37
		}
38
39
		return $this->data[$offset];
40
	}
41
42
43
	/**
44
	 * @param mixed $offset
45
	 * @param mixed $value
46
	 *
47
	 * @return void
48
	 */
49
	public function offsetSet($offset, $value)
50
	{
51
		if ( is_null($offset) )
52
		{
53
			$this->data[] = $value;
54
		}
55
		else
56
		{
57
			$this->data[$offset] = $value;
58
		}
59
	}
60
61
62
	/**
63
	 * @param mixed $offset
64
	 *
65
	 * @return void
66
	 */
67
	public function offsetUnset($offset)
68
	{
69
		$this->__unset($offset);
70
	}
71
}