Completed
Pull Request — master (#40)
by
unknown
05:34
created

Nested::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Arrayzy\Extend;
4
use Arrayzy\ArrayImitator;
5
6
class Nested
7
{
8
    /**
9
     * @var ArrayImitator
10
     */
11
    protected $arrayContainer;
12
13
    /**
14
     * Nested constructor.
15
     *
16
     * @param ArrayImitator $arrayObject
17
     */
18 4
    public function __construct($arrayObject)
19
    {
20 4
        $this->arrayContainer = $arrayObject;
21 4
    }
22
23
    /**
24
     * Check value for this keys exist.
25
     *
26
     * @param $keyString
27
     * @param string $separator
28
     * @return bool
29
     */
30 4
    public function has($keyString, $separator = '.')
31
    {
32 4
        $array = $this->arrayContainer->toArray();
33 4
        $keys = explode($separator, $keyString);
34 4 View Code Duplication
        foreach ($keys as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35 4
            if(!is_array($array) or !array_key_exists($key, $array)) {
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...
36 4
                return false;
37
            }
38 4
            $array = $array[$key];
39 4
        }
40 4
        return true;
41
    }
42
43
    /**
44
     * Return value from nested array or default value.
45
     *
46
     * @param $keyString
47
     * @param string $separator
48
     * @return array|mixed
49
     */
50 4
    public function get($keyString, $separator = '.')
51
    {
52 4
        $array = $this->arrayContainer->toArray();
53 4
        $keys = explode($separator, $keyString);
54 4 View Code Duplication
        foreach ($keys as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
55 4
            if(!is_array($array) or !array_key_exists($key, $array)) {
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...
56 4
                return $this->arrayContainer->getDefaultValue();
57
            }
58 4
            $array = $array[$key];
59 4
        }
60 4
        return $array;
61
    }
62
63
}