Passed
Pull Request — develop (#67)
by Glynn
03:04
created

arrayFlatten()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Procedural wrappers for various functions.
5
 *
6
 * This file is part of PinkCrab Function Constructors.
7
 *
8
 * PinkCrab Function Constructors is free software: you can redistribute it and/or modify it under the terms of the
9
 * GNU General Public License as published by the Free Software Foundation, either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * PinkCrab Function Constructors is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
13
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
 * See the GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with PinkCrab Function Constructors.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 *
19
 * @author Glynn Quelch <[email protected]>
20
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
21
 * @package PinkCrab\FunctionConstructors
22
 */
23
24
declare(strict_types=1);
25
26
use PinkCrab\FunctionConstructors\Arrays as Arr;
0 ignored issues
show
Bug introduced by
The type PinkCrab\FunctionConstructors\Arrays was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
if (! function_exists('stringContains')) {
29
    /**
30
     * Checks if a string contains a sub string
31
     *
32
     * @param string $haystack The string to search within.
33
     * @param string $needle The sub string to look for.
34
     * @return bool
35
     */
36
    function stringContains(string $haystack, string $needle): bool
37
    {
38
        return strpos($haystack, $needle) !== false;
39
    }
40
}
41
42
if (! function_exists('array_flatten')) {
43
    /**
44
     * Flattens an array to desired depth.
45
     * doesn't preserve keys
46
     *
47
     * @param mixed[] $array The array to flatten
48
     * @param int|null $n The depth to flatten the array, if null will flatten all arrays.
49
     * @return mixed[]
50
     */
51
    function arrayFlatten(array $array, ?int $n = null): array
52
    {
53
        return Arr\flattenByN($n)($array);
0 ignored issues
show
Bug introduced by
The function flattenByN was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        return /** @scrutinizer ignore-call */ Arr\flattenByN($n)($array);
Loading history...
54
    }
55
}
56
57
if (! function_exists('toObject')) {
58
    /**
59
     * Simple mapper for turning arrays into stdClass objects.
60
     *
61
     * @param array<string|int, mixed> $array
62
     * @return object
63
     */
64
    function toObject(array $array)
65
    {
66
        return Arr\toObject(new stdClass())($array);
0 ignored issues
show
Bug introduced by
new stdClass() of type stdClass is incompatible with the type array expected by parameter $array of toObject(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        return Arr\toObject(/** @scrutinizer ignore-type */ new stdClass())($array);
Loading history...
67
    }
68
}
69
70
if (! function_exists('invokeCallable')) {
71
    /**
72
     * Used to invoke a callable.
73
     *
74
     * @param callable(mixed ...$args):mixed $fn
75
     * @param mixed ...$args
76
     * @return mixed
77
     */
78
    function invokeCallable(callable $fn, ...$args)
79
    {
80
        return $fn(...$args);
81
    }
82
}
83
84
if (! function_exists('isArrayAccess')) {
85
    /**
86
     * Checks if an array or an object which has array like access.
87
     *
88
     * @source https://stackoverflow.com/questions/12346479/how-to-check-for-arrayness-in-php
89
     * @param mixed $var
90
     * @return bool
91
     */
92
    function isArrayAccess($var)
93
    {
94
        return is_array($var) || $var instanceof \ArrayAccess;
95
    }
96
}
97