Completed
Push — master ( 06b292...adafac )
by Nicolai
02:22
created

ArrayPluck::assertArrayWasCorrectlyPlucked()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 4
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace SmartWeb\ModuleTesting\Assertions;
5
6
use Closure;
7
use SmartWeb\ModuleTesting\DomainAssert\DomainRule;
8
use function call_user_func;
9
use function call_user_func_array;
10
use function is_callable;
11
12
13
/**
14
 * Trait ArrayPluck
15
 *
16
 * @package SmartWeb\Testing\Assertions
17
 */
18
trait ArrayPluck
19
{
20
    
21
    /**
22
     * @param string|callable  $plucker
23
     * @param array            $input
24
     * @param int|string|array $keys
25
     * @param array            $expected
26
     */
27
    protected function assertArrayWasCorrectlyPlucked($plucker, array $input, $keys, array $expected)
28
    {
29
        $plucker = is_callable($plucker) || ($plucker instanceof Closure)
30
            ? $plucker
31
            : [$this, $plucker];
32
        
33
        $actual = call_user_func_array($plucker, [$input, $keys]);
34
        
35
        $this->assertThat(
0 ignored issues
show
Bug introduced by
It seems like assertThat() 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...
36
            compact('actual', 'expected'),
37
            new DomainRule("actual == expected")
38
        );
39
    }
40
    
41
    /**
42
     * @param callable|string  $plucker
43
     * @param array            $input
44
     * @param int|string|array $keys
45
     * @param array            $expected
46
     */
47
    protected function assertArrayWasCorrectlyPlucked2($plucker, array $input, $keys, array $expected)
48
    {
49
        $plucker = is_callable($plucker) || ($plucker instanceof Closure)
50
            ? $plucker
51
            : [$this, $plucker];
52
        
53
        $plucker = function ($input, $keys) use ($plucker)
54
        {
55
            return call_user_func($plucker, $input, $keys);
56
        };
57
        
58
        $params = [$input, $keys];
59
        
60
        $this->assertThat(
0 ignored issues
show
Bug introduced by
It seems like assertThat() 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...
61
            compact('input', 'keys', 'expected', 'plucker', 'params'),
62
            new DomainRule("call(plucker, params) == expected")
63
        );
64
    }
65
}