Passed
Push — version-4 ( d2a5d9...7cacf2 )
by Sebastian
07:43
created

Functions.php$0 ➔ isStringable()   A

Complexity

Conditions 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Seboettg\Collection\Lists;
4
5
use Seboettg\Collection\Comparable\Comparable;
6
use Stringable;
7
8
final class Functions
9
{
10
11 4
    final public static function strval($value): string
12
    {
13 4
        if (is_double($value)) {
14 2
            $str = \strval($value);
15 2
            if (strlen($str) == 1) {
16 1
                return sprintf("%1\$.1f",$value);
17
            }
18 2
            return \strval($value);
19
        }
20 3
        if (is_bool($value)) {
21 1
            return $value ? "true" : "false";
22
        }
23 3
        return "$value";
24
    }
25
26
    final public static function emptyList(): ListInterface
27
    {
28
        return new class() implements ListInterface {
29
            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...
30
            use ArrayListTrait;
31
        };
32
    }
33
34 38
    final public static function listOf(...$elements): ListInterface
35
    {
36 38
        return listFromArray($elements);
37
    }
38
39 38
    final public static function listFromArray($elements): ListInterface
40
    {
41 38
        $list = emptyList();
42 38
        $list->setArray(array_values($elements));
43 38
        return $list;
44
    }
45
46 7
    final public static function isScalarOrStringable($object): bool
47
    {
48 7
        return is_scalar($object)
49 7
            || method_exists($object, "__toString");
50
    }
51
52 3
    final public static function isComparable($object): bool
53
    {
54 3
        return $object instanceof Comparable;
55
    }
56
57 1
    final public static function isStringable($object): bool
58
    {
59 1
        return is_scalar($object)
60 1
            || method_exists($object, "__toString");
61
    }
62
63 6
    final public static function in_array($needle, $array): bool
64
    {
65 6
        if (is_scalar($needle)) {
66 5
            return \in_array($needle, $array);
67
        }
68 1
        if (isComparable($needle)) {
69
            foreach ($array as $item) {
70
                if (!isComparable($item)) {
71
                    return false;
72
                }
73
                if ($needle->compareTo($item) === 0) {
74
                    return true;
75
                }
76
            }
77
        }
78 1
        if (isStringable($needle)) {
79 1
            foreach ($array as $item) {
80 1
                if (strcmp((string)$needle, (string) $item) === 0) {
81 1
                    return true;
82
                }
83
            }
84
        }
85 1
        return false;
86
    }
87
}
88
89
function strval($value): string
90
{
91 4
    return Functions::strval($value);
92
}
93
94
function emptyList(): ListInterface
95
{
96 44
    return Functions::emptyList();
97
}
98
99
function listOf(...$elements): ListInterface
100
{
101 38
    return Functions::listOf(...$elements);
102
}
103
104
function listFromArray(array $elements): ListInterface
105
{
106 38
    return Functions::listFromArray($elements);
107
}
108
109
function isScalarOrStringable($object): bool
110
{
111 7
    return Functions::isScalarOrStringable($object);
112
}
113
114
function isStringable($object): bool
115
{
116 1
    return Functions::isStringable($object);
117
}
118
119
function isComparable($object): bool
120
{
121 3
    return Functions::isComparable($object);
122
}
123
124
function in_array($needle, $array): bool
125
{
126 6
   return Functions::in_array($needle, $array);
127
}
128