Passed
Push — version-4 ( ddfd9f...211252 )
by Sebastian
02:26
created

Functions   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ emptyMap() 0 5 1
emptyMap() 0 5 ?
A hp$0 ➔ pair() 0 3 1
pair() 0 3 ?
A hp$0 ➔ mapOf() 0 11 3
mapOf() 0 11 ?
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 48
    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 48
        $map = emptyMap();
36 48
        foreach ($pairs as $pair) {
37 48
            if (!$pair instanceof Pair) {
38 1
                throw new InvalidArgumentException(sprintf(
39 1
                    "At least one pair is not of type %s", Pair::class));
40
            }
41 48
            $map->put($pair->getKey(), $pair->getValue());
42
        }
43 47
        return $map;
44
    }
45
46
    /**
47
     * @param scalar $key
48
     * @param mixed $value
49
     * @return Pair
50
     */
51 50
    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...
52
    {
53 50
        return new Pair($key, $value);
54
    }
55
}
56
57
/**
58
 * @return MapInterface
59
 */
60
function emptyMap(): MapInterface
61
{
62 53
    return Functions::emptyMap();
63
}
64
65
/**
66
 * @param array<Pair> ...$pairs
67
 * @return MapInterface
68
 */
69
function mapOf(...$pairs): MapInterface
70
{
71 48
    return Functions::mapOf(...$pairs);
72
}
73
74
/**
75
 * @param scalar $key
76
 * @param mixed $value
77
 * @return Pair
78
 */
79
function pair($key, $value): Pair
80
{
81 50
    return Functions::pair($key, $value);
82
}
83