Functions::isScalarOrStringable()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
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
 * Get string value of a variable. It differs from the original function in that it is also – next to scalar values – able to handle objects.
79
 * @param mixed $value
80
 * @return string
81
 */
82
function strval($value): string
83
{
84 7
    return Functions::strval($value);
85
}
86
87
/**
88
 * Returns true when $object is a scalar value or when `isStringable` returns true for $object
89
 * @param mixed $object
90
 * @return bool
91
 */
92
function isScalarOrStringable($object): bool
93
{
94 14
    return Functions::isScalarOrStringable($object);
95
}
96
97
/**
98
 * Returns `true` when $object implements `__toString` or `$object` is a scalar value
99
 * @param mixed $object
100
 * @return bool
101
 */
102
function isStringable($object): bool
103
{
104 6
    return Functions::isStringable($object);
105
}
106
107
/**
108
 * Returns true when `$object` implements the `Comparable` interface.
109
 * @param mixed $object
110
 * @return bool
111
 */
112
function isComparable($object): bool
113
{
114 8
    return Functions::isComparable($object);
115
}
116
117
/**
118
 * Returns `true` when the given `$array` contains `$needle`. It differs from the original function in that 
119
 * it is also – next to scalar values – able to handle objects by using either `compareTo` method, when `$object`
120
 * is an instance of Comparable or `strcmp`, when `$object` is a string or `$object` implements the Stringable interface.
121
 * @param string $needle
122
 * @param array $array
123
 * @return bool
124
 */
125
function in_array($needle, $array): bool
126
{
127 11
    return Functions::in_array($needle, $array);
128
}
129