Passed
Push — master ( c50550...c21ac6 )
by Nicolas
03:19
created

ArrayRelated::cloneArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Puzzle\Assert;
4
5
trait ArrayRelated
6
{
7 12
    private function assertSameArrayExceptOrder(array $expectedArray, array $array, $message = '')
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
8
    {
9 12
        $this->assertCount(count($expectedArray), $array, $message);
0 ignored issues
show
Bug introduced by
It seems like assertCount() 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...
10
        
11 9
        $expected = $this->cloneArray($expectedArray);
12
        
13 9
        foreach($array as $value)
14
        {
15 9
            $index = array_search($value, $expected, true);
16 9
            $this->assertNotFalse($index, $message);
0 ignored issues
show
Bug introduced by
It seems like assertNotFalse() 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...
17
            
18 5
            unset($expected[$index]);
19 5
        }
20 1
    }
21
    
22 5
    private function assertSameKeysAndValuesExceptOrder(array $expectedArray, array $array, $message = '')
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
23
    {
24 5
        $this->assertCount(count($expectedArray), $array, $message);
0 ignored issues
show
Bug introduced by
It seems like assertCount() 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...
25
        
26 4
        foreach($expectedArray as $key => $value)
27
        {
28 4
            $this->assertArrayHasKey($key, $array, $message);
0 ignored issues
show
Bug introduced by
It seems like assertArrayHasKey() 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 4
            $this->assertSame($value, $array[$key], $message);
0 ignored issues
show
Bug introduced by
The method assertSame() does not exist on Puzzle\Assert\ArrayRelated. Did you maybe mean assertSameArrayExceptOrder()?

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...
30 4
        }
31 2
    }
32
    
33 10
    private function cloneArray(array $array)
34
    {
35 9
        $clone = [];
36
        
37 9
        foreach($array as $key => $value)
38
        {
39 9
            if(is_object($value))
40 9
            {
41 1
                $value = clone $value;
42 1
            }
43 9
            elseif(is_array($value))
44
            {
45 1
                $value = $this->cloneArray($value);
46 1
            }
47
            
48 10
            $clone[$key] = $value;
49 9
        }
50
        
51 9
        return $clone;
52
    }
53
}
54