ContainsNoDuplicateItem::deduplicate()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.9
cc 2
nc 1
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
4
namespace Pitchart\Phlunit\Constraint\Arrays;
5
6
use PHPUnit\Framework\Constraint\Constraint;
7
8
class ContainsNoDuplicateItem extends Constraint
9
{
10 9
    protected function matches($other): bool
11
    {
12 9
        $other = ArrayUtility::toArray($other);
13 9
        return \count($this->deduplicate($other)) === \count($other);
14
    }
15
16
    /**
17
     * @return (mixed|null)[]
18
     *
19
     * @psalm-return non-empty-list<mixed|null>
20
     */
21 9
    private function deduplicate(array $array): array
22
    {
23 9
        $unique = [];
24
        do {
25 9
            $element = \array_shift($array);
26 9
            $unique[] = $element;
27
28 9
            $array = \array_udiff(
29 9
                $array,
30 9
                [$element],
31 9
                function($first, $second) {
32 9
                    return $first <=> $second;
33 9
                }
34
            );
35 9
        } while (\count($array) > 0);
36
37 9
        return $unique;
38
    }
39
40
    /**
41
     * @return string
42
     */
43 1
    public function toString(): string
44
    {
45 1
        return 'contains no duplicate item';
46
    }
47
}
48