Completed
Push — master ( 000800...90ac47 )
by Mathieu
09:18
created

AssertArrays   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 41
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B assertArrayStructureEquals() 0 30 6
1
<?php
2
3
namespace LaravelFr\ApiTests;
4
5
trait AssertArrays
6
{
7
    /**
8
     * Assert that the given array has exactly the given structure.
9
     *
10
     * @param  array $structure
11
     * @param  array $array
12
     *
13
     * @return $this
14
     */
15
    public function assertArrayStructureEquals(array $structure, array $array)
16
    {
17
        $structureFirstLevel = array_map(function ($value, $key) {
18
            return is_array($value) ? $key : $value;
19
        }, $structure, array_keys($structure));
20
21
        $responseFirstLevel = array_keys($array);
22
23
        if ($structureFirstLevel !== ['*']) {
24
            $this->assertEquals($structureFirstLevel, $responseFirstLevel, '', 0.0, 10, true);
0 ignored issues
show
Bug introduced by
It seems like assertEquals() 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
27
        $structureOtherLevels = array_filter($structure, function ($value) {
28
            return is_array($value);
29
        });
30
31
        foreach ($structureOtherLevels as $key => $childStructure) {
32
            if ($key === '*') {
33
                $this->assertInternalType('array', $array);
0 ignored issues
show
Bug introduced by
It seems like assertInternalType() 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...
34
35
                foreach ($array as $responseDataItem) {
36
                    $this->assertArrayStructureEquals($childStructure, $responseDataItem);
37
                }
38
            } else {
39
                $this->assertArrayStructureEquals($childStructure, $array[$key]);
40
            }
41
        }
42
43
        return $this;
44
    }
45
}
46