Completed
Pull Request — master (#80)
by Christoffer
02:18
created

toString()   C

Complexity

Conditions 12
Paths 11

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 21
nc 11
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Digia\GraphQL\Util;
4
5
/**
6
 * @param array    $array
7
 * @param callable $fn
8
 * @return mixed
9
 */
10
function arraySome(array $array, callable $fn)
11
{
12
    return array_reduce($array, function ($result, $value) use ($fn) {
13
        return $result || $fn($value);
14
    });
15
}
16
17
/**
18
 * @param array    $array
19
 * @param callable $predicate
20
 * @return mixed|null
21
 */
22
function find(array $array, callable $predicate)
23
{
24
    foreach ($array as $value) {
25
        if ($predicate($value)) {
26
            return $value;
27
        }
28
    }
29
30
    return null;
31
}
32
33
/**
34
 * @param array    $array
35
 * @param callable $keyFn
36
 * @return array
37
 */
38
function keyMap(array $array, callable $keyFn): array
39
{
40
    return array_reduce($array, function ($map, $item) use ($keyFn) {
41
        $map[$keyFn($item)] = $item;
42
        return $map;
43
    }, []);
44
}
45
46
/**
47
 * @param array    $array
48
 * @param callable $keyFn
49
 * @param callable $valFn
50
 * @return array
51
 */
52
function keyValMap(array $array, callable $keyFn, callable $valFn): array
53
{
54
    return array_reduce($array, function ($map, $item) use ($keyFn, $valFn) {
55
        $map[$keyFn($item)] = $valFn($item);
56
        return $map;
57
    }, []);
58
}
59
60
/**
61
 * @param $value
62
 * @return string
63
 */
64
function toString($value): string
65
{
66
    if (\is_object($value) && method_exists($value, '__toString')) {
67
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value returns the type object which is incompatible with the type-hinted return string.
Loading history...
68
    }
69
    if (\is_object($value)) {
70
        return 'Object';
71
    }
72
    if (\is_array($value)) {
73
        return 'Array';
74
    }
75
    if (\is_callable($value)) {
76
        return 'Function';
77
    }
78
    if ($value === '') {
79
        return '(empty string)';
80
    }
81
    if ($value === null) {
82
        return 'null';
83
    }
84
    if ($value === true) {
85
        return 'true';
86
    }
87
    if ($value === false) {
88
        return 'false';
89
    }
90
    if (\is_string($value)) {
91
        return "\"{$value}\"";
92
    }
93
    if (is_scalar($value)) {
94
        return (string)$value;
95
    }
96
    return \gettype($value);
97
}
98
99
/**
100
 * @param $value
101
 * @return string
102
 */
103
function jsonEncode($value): string
104
{
105
    return json_encode($value, JSON_UNESCAPED_UNICODE);
106
}
107
108
/**
109
 * @param string $path
110
 * @return string
111
 */
112
function readFile(string $path): string
113
{
114
    return mb_convert_encoding(file_get_contents($path), 'UTF-8');
115
}
116