IsSubset::evaluate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
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 16
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
namespace Pitchart\Phlunit\Constraint\Arrays;
4
5
use PHPUnit\Framework\Constraint\Constraint;
6
use function Pitchart\Transformer\transform;
7
8
final class IsSubset extends Constraint
9
{
10
    /**
11
     * @var iterable
12
     */
13
    private $set;
14
15 17
    public function __construct(iterable $set)
16
    {
17 17
        $this->set = $set;
18 17
    }
19
20
    /**
21
     * @param iterable $other
22
     * @param string $description
23
     * @param bool $returnResult
24
     *
25
     * @return bool
26
     */
27 14
    public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
28
    {
29 14
        $other = ArrayUtility::toArray($other);
30 14
        $set = ArrayUtility::toArray($this->set);
31
32 14
        if (ArrayUtility::isAssociative($other)) {
33 5
            $result = \array_diff_assoc($other, $set) === [];
34
        } else {
35 9
            $result = transform($other)->diff($set)->toArray() === [];
36
        }
37
38 14
        if (!$returnResult && !$result) {
39 1
            $this->fail($other, $description);
40
        }
41
42 13
        return $result;
43
    }
44
45
    /**
46
     * @return string
47
     */
48 1
    public function toString(): string
49
    {
50 1
        return ' is a subset of ' . $this->exporter()->export($this->set);
51
    }
52
53 1
    protected function failureDescription($other): string
54
    {
55 1
        return $this->exporter()->export($other) . $this->toString();
56
    }
57
}
58