ContainsNoDuplicateItem   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 15
c 1
b 0
f 0
dl 0
loc 38
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A matches() 0 4 1
A toString() 0 3 1
A deduplicate() 0 17 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