Passed
Branch master (fb2e38)
by Sebastian
02:23
created

Functions   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 58
ccs 30
cts 31
cp 0.9677
rs 10
wmc 19

5 Methods

Rating   Name   Duplication   Size   Complexity  
A strval() 0 13 5
A isComparable() 0 3 1
A isScalarOrStringable() 0 4 2
A isStringable() 0 4 2
B in_array() 0 23 9
1
<?php
2
3
namespace Seboettg\Collection\Common;
4
5
use Seboettg\Collection\Comparable\Comparable;
6
7
final class Functions
8
{
9
10 7
    final public static function strval($value): string
11
    {
12 7
        if (is_double($value)) {
13 4
            $str = \strval($value);
14 4
            if (strlen($str) == 1) {
15 1
                return sprintf("%1\$.1f",$value);
16
            }
17 4
            return \strval($value);
18
        }
19 6
        if (is_bool($value)) {
20 3
            return $value ? "true" : "false";
21
        }
22 6
        return "$value";
23
    }
24
25 14
    final public static function isScalarOrStringable($object): bool
26
    {
27 14
        return is_scalar($object)
28 14
            || method_exists($object, "__toString");
29
    }
30
31 8
    final public static function isComparable($object): bool
32
    {
33 8
        return $object instanceof Comparable;
34
    }
35
36 6
    final public static function isStringable($object): bool
37
    {
38 6
        return is_scalar($object)
39 6
            || method_exists($object, "__toString");
40
    }
41
42 11
    final public static function in_array($needle, $array): bool
43
    {
44 11
        if (is_scalar($needle)) {
45 5
            return \in_array($needle, $array);
46
        }
47 6
        if (isComparable($needle)) {
48 4
            foreach ($array as $item) {
49 4
                if (!isComparable($item)) {
50
                    return false;
51
                }
52 4
                if ($needle->compareTo($item) === 0) {
53 4
                    return true;
54
                }
55
            }
56
        }
57 6
        if (isStringable($needle)) {
58 2
            foreach ($array as $item) {
59 2
                if (strcmp((string)$needle, (string) $item) === 0) {
60 2
                    return true;
61
                }
62
            }
63
        }
64 6
        return false;
65
    }
66
}
67
68
69
function strval($value): string
70
{
71 7
    return Functions::strval($value);
72
}
73
74
function isScalarOrStringable($object): bool
75
{
76 14
    return Functions::isScalarOrStringable($object);
77
}
78
79
function isStringable($object): bool
80
{
81 6
    return Functions::isStringable($object);
82
}
83
84
function isComparable($object): bool
85
{
86 8
    return Functions::isComparable($object);
87
}
88
89
function in_array($needle, $array): bool
90
{
91 11
    return Functions::in_array($needle, $array);
92
}
93