ContainsSet::evaluate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 18
ccs 9
cts 9
cp 1
rs 9.9666
cc 4
nc 4
nop 3
crap 4
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