ContainsSet   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 1
b 0
f 0
dl 0
loc 50
ccs 16
cts 16
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A evaluate() 0 18 4
A failureDescription() 0 3 1
A toString() 0 3 1
1
<?php declare(strict_types=1);
2
3
4
namespace Pitchart\Phlunit\Constraint\Arrays;
5
6
use PHPUnit\Framework\Constraint\Constraint;
7
use function Pitchart\Transformer\transform;
8
9
final class ContainsSet extends Constraint
10
{
11
    /**
12
     * @var iterable
13
     */
14
    private $set;
15
16 17
    public function __construct(iterable $subset)
17
    {
18 17
        $this->set = $subset;
19 17
    }
20
21
    /**
22
     * @param iterable $other
23
     * @param string $description
24
     * @param bool $returnResult
25
     *
26
     * @return bool
27
     */
28 14
    public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
29
    {
30
        //type cast $other & $this->subset as an array to allow
31
        //support in standard array functions.
32 14
        $other = ArrayUtility::toArray($other);
33 14
        $set = ArrayUtility::toArray($this->set);
34
35 14
        if (ArrayUtility::isAssociative($other)) {
36 5
            $result = \array_diff_assoc($set, $other) === [];
37
        } else {
38 9
            $result = transform($set)->diff($other)->toArray() === [];
39
        }
40
41 14
        if (!$returnResult && !$result) {
42 1
            $this->fail($other, $description);
43
        }
44
45 13
        return $result;
46
    }
47
48
    /**
49
     * @return string
50
     */
51 1
    public function toString(): string
52
    {
53 1
        return ' contains the subset ' . $this->exporter()->export($this->set);
54
    }
55
56 1
    protected function failureDescription($other): string
57
    {
58 1
        return $this->exporter()->export($other) . $this->toString();
59
    }
60
}
61