Completed
Push — master ( 4aee11...dd7177 )
by Kacper
04:24
created

helper.php ➔ copy()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 1
nop 1
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Nucleus - XMPP Library for PHP
4
 *
5
 * Copyright (C) 2016, Some rights reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp\Utils\helper;
17
18
use Kadet\Xmpp\Utils\Dumper;
19
20
/**
21
 * Returns exception friendly type of value.
22
 *
23
 * @param $value
24
 * @return string
25
 */
26
function typeof($value) : string
27
{
28
    if (is_object($value)) {
29
        return "object of type ".get_class($value);
30
    } elseif (is_resource($value)) {
31
        return get_resource_type($value).' resource';
32
    }
33
34
    return gettype($value);
35
}
36
37
function partial(callable $callable, $argument, int $position = 0) : callable
38
{
39
    return function (...$arguments) use ($callable, $argument, $position) {
40
        $arguments = array_merge(
41
            array_slice($arguments, 0, $position),
42
            [ $argument ],
43
            array_slice($arguments, $position)
44
        );
45
46
        return $callable(...$arguments);
47
    };
48
}
49
50
function dd($value)
51
{
52
    echo Dumper::get()->dump($value).PHP_EOL.PHP_EOL;
53
}
54
55
function format($string, array $arguments = [])
56
{
57
    return str_replace(array_map(function ($e) { return "{{$e}}"; }, array_keys($arguments)), $arguments, $string);
58
}
59
60
function rearrange(array $array, array $keys) : array
61
{
62
    uksort($array, function($a, $b) use($keys) {
63
        return ($keys[$b] ?? 0) <=> ($keys[$a] ?? 0);
64
    });
65
66
    return $array;
67
}
68
69
function copy(array $array)
70
{
71
    return array_map(function($element) {
72
        if(is_array($element)) {
73
            return copy($element);
74
        } elseif(is_object($element)) {
75
            return clone $element;
76
        }
77
78
        return $element;
79
    }, $array);
80
}
81