Passed
Push — master ( 1a8268...9cfe94 )
by Sebastian
08:09
created

Functions.php$0 ➔ isMap()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 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 50
    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 50
        $map = emptyMap();
36 50
        $map->setArray(array_combine(
37 50
            array_map(fn (Pair $pair) => $pair->getKey(), $pairs), //keys
38 50
            array_map(fn (Pair $pair) => $pair->getValue(), $pairs) //values
39
        ));
40 50
        return $map;
41
    }
42
43
    /**
44
     * @param scalar $key
45
     * @param mixed $value
46
     * @return Pair
47
     */
48 55
    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...
49
    {
50 55
        return Pair::factory($key, $value);
51
    }
52
53 2
    public static function isMap($value): bool
54
    {
55 2
        return $value instanceof MapInterface;
56
    }
57
}
58
59
/**
60
 * @return MapInterface
61
 */
62
function emptyMap(): MapInterface
63
{
64 59
    return Functions::emptyMap();
65
}
66
67
/**
68
 * @param array<Pair> ...$pairs
69
 * @return MapInterface
70
 */
71
function mapOf(...$pairs): MapInterface
72
{
73 50
    return Functions::mapOf(...$pairs);
74
}
75
76
/**
77
 * @param scalar $key
78
 * @param mixed $value
79
 * @return Pair
80
 */
81
function pair($key, $value): Pair
82
{
83 55
    return Functions::pair($key, $value);
84
}
85
86
function isMap($value): bool
87
{
88 2
    return Functions::isMap($value);
89
}
90