Passed
Push — master ( f54d17...8b1e15 )
by Sebastian
02:13
created

strval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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\Common;
13
14
use Seboettg\Collection\Comparable\Comparable;
15
16
final class Functions
17
{
18
19 7
    final public static function strval($value): string
20
    {
21 7
        if (is_double($value)) {
22 4
            $str = \strval($value);
23 4
            if (strlen($str) === 1) {
24 1
                return sprintf("%1\$.1f",$value);
25
            }
26 4
            return $str;
27
        }
28 6
        if (is_bool($value)) {
29 3
            return $value ? "true" : "false";
30
        }
31 6
        return "$value";
32
    }
33
34 14
    final public static function isScalarOrStringable($object): bool
35
    {
36 14
        return is_scalar($object)
37 14
            || method_exists($object, "__toString");
38
    }
39
40 8
    final public static function isComparable($object): bool
41
    {
42 8
        return $object instanceof Comparable;
43
    }
44
45 6
    final public static function isStringable($object): bool
46
    {
47 6
        return is_scalar($object)
48 6
            || method_exists($object, "__toString");
49
    }
50
51 11
    final public static function in_array($needle, $array): bool
52
    {
53 11
        if (is_scalar($needle)) {
54 5
            return \in_array($needle, $array);
55
        }
56 6
        if (isComparable($needle)) {
57 4
            foreach ($array as $item) {
58 4
                if (!isComparable($item)) {
59
                    return false;
60
                }
61 4
                if ($needle->compareTo($item) === 0) {
62 4
                    return true;
63
                }
64
            }
65
        }
66 6
        if (isStringable($needle)) {
67 2
            foreach ($array as $item) {
68 2
                if (strcmp((string)$needle, (string) $item) === 0) {
69 2
                    return true;
70
                }
71
            }
72
        }
73 6
        return false;
74
    }
75
}
76
77
78
function strval($value): string
79
{
80 7
    return Functions::strval($value);
81
}
82
83
function isScalarOrStringable($object): bool
84
{
85 14
    return Functions::isScalarOrStringable($object);
86
}
87
88
function isStringable($object): bool
89
{
90 6
    return Functions::isStringable($object);
91
}
92
93
function isComparable($object): bool
94
{
95 8
    return Functions::isComparable($object);
96
}
97
98
function in_array($needle, $array): bool
99
{
100 11
    return Functions::in_array($needle, $array);
101
}
102