Failed Conditions
Pull Request — master (#6)
by Chad
01:45
created

ArrayAssertsTrait::assertSameArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.4109
c 0
b 0
f 0
cc 3
eloc 31
nc 3
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Chadicus;
4
5
/**
6
 * Trait for adding asserts for arrays
7
 */
8
trait ArrayAssertsTrait
9
{
10
    /**
11
     * Asserts the given $actual array is the same as the $expected array disregarding index order
12
     *
13
     * @param array       $expected The expected array.
14
     * @param mixed       $actual   The actual array.
15
     * @param string|null $prefix   Prefix to use with error messages. Useful for nested arrays.
16
     *
17
     * @return void
18
     */
19
    public function assertSameArray(array $expected, $actual, $prefix = null)
20
    {
21
        //assert that the actual value is an array
22
        $this->assertInternalType('array', $actual, '$actual was not an array');
23
24
        $expectedKeys = array_keys($expected);
25
        $actualKeys = array_keys($actual);
26
27
        //find any keys in the expected array that are not present in the actual array
28
        $missingExpectedKeys = array_diff($expectedKeys, $actualKeys);
29
        $this->assertCount(
30
            0,
31
            $missingExpectedKeys,
32
            sprintf(
33
                '$actual array is missing %d keys: %s',
34
                count($missingExpectedKeys),
35
                implode(', ', $missingExpectedKeys)
36
            )
37
        );
38
39
        //find any keys in the actual array that are not expected in the expected array
40
        $unexpectedKeys = array_diff($actualKeys, $expectedKeys);
41
        $this->assertCount(
42
            0,
43
            $unexpectedKeys,
44
            sprintf(
45
                '$actual array contains %d unexpected keys: %s',
46
                count($unexpectedKeys),
47
                implode(', ', $unexpectedKeys)
48
            )
49
        );
50
51
        //Assert all values are the same value and type.
52
        //Recursively call assertSameArray on array values
53
        foreach ($expected as $key => $value) {
54
            if (is_array($value)) {
55
                $this->assertSameArray($value, $actual[$key], "{$prefix}{$key}.");
56
                continue;
57
            }
58
59
            $this->assertSame(
0 ignored issues
show
Bug introduced by
The method assertSame() does not exist on Chadicus\ArrayAssertsTrait. Did you maybe mean assertSameArray()?

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...
60
                $value,
61
                $actual[$key],
62
                sprintf(
63
                    "{$prefix}{$key} value is not correct expected %s\nfound %s",
64
                    var_export($value, 1),
65
                    var_export($actual[$key], 1)
66
                )
67
            );
68
        }
69
    }
70
71
    /**
72
     * Asserts the number of elements of an array, Countable or Traversable.
73
     *
74
     * Ensures this method must be provided by classes using this trait.
75
     *
76
     * @param integer $expectedCount The expected number of items in $haystack.
77
     * @param mixed   $haystack      The array, countable or traversable object containing items.
78
     * @param string  $message       Optional error message to give upon failure.
79
     *
80
     * @return void
81
     */
82
    abstract public function assertCount($expectedCount, $haystack, $message = '');
83
84
    /**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$actual" missing
Loading history...
85
     * Asserts that a variable is of a given type.
86
     *
87
     * Ensures this method must be provided by classes using this trait.
88
     *
89
     * @param string $expected The expected internal type.
90
     * @param mixed  $haystack The variable to verify.
0 ignored issues
show
Bug introduced by
There is no parameter named $haystack. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
Coding Style introduced by
Doc comment for parameter $haystack does not match actual variable name $actual
Loading history...
91
     * @param string $message  Optional error message to give upon failure.
92
     *
93
     * @return void
94
     */
95
    abstract public function assertInternalType($expected, $actual, $message = '');
96
}
97