Passed
Branch version-4 (8b03a3)
by Sebastian
02:18
created

anonymous//src/Map/Functions.php$0

Complexity

Total Complexity 0

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
wmc 0
1
<?php
2
declare(strict_types=1);
3
/*
4
 * Copyright (C) 2022 Sebastian Böttger <[email protected]>
5
 * You may use, distribute and modify this code under the
6
 * terms of the MIT license.
7
 *
8
 * You should have received a copy of the MIT license with
9
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
10
 */
11
12
namespace Seboettg\Collection\Map;
13
14
use InvalidArgumentException;
15
16
final class Functions
17
{
18
    /**
19
     * @return MapInterface
20
     */
21
    public static final function emptyMap(): MapInterface
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
22
    {
23
        return new class() implements MapInterface {
24
            private $array = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "array" must contain a leading underscore
Loading history...
introduced by
The private property $array is not used, and could be removed.
Loading history...
25
            use MapTrait;
26
        };
27
    }
28
29
    /**
30
     * @param array<Pair> ...$pairs
31
     * @return MapInterface
32
     */
33 22
    public final static function mapOf(...$pairs): MapInterface
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
34
    {
35 22
        $map = emptyMap();
36 22
        foreach ($pairs as $pair) {
37 22
            if (!$pair instanceof Pair) {
38
                throw new InvalidArgumentException(sprintf(
39
                    "At least one pair is not of type %s", Pair::class));
40
            }
41 22
            $map->put($pair->getKey(), $pair->getValue());
42
        }
43 22
        return $map;
44
    }
45
46 22
    public final static function pair($key, $value): Pair
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
47
    {
48 22
        return new Pair($key, $value);
49
    }
50
}
51
52
/**
53
 * @return MapInterface
54
 */
55
function emptyMap(): MapInterface
56
{
57 23
    return Functions::emptyMap();
58
}
59
60
/**
61
 * @param array<Pair> ...$pairs
62
 * @return MapInterface
63
 */
64
function mapOf(...$pairs): MapInterface
65
{
66 22
    return Functions::mapOf(...$pairs);
67
}
68
69
70
function pair($key, $value): Pair
71
{
72 22
    return Functions::pair($key, $value);
73
}
74