Passed
Branch master (c976c6)
by Théo
03:51
created

anonymous//src/functions.php$0   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 6
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
wmc 1
lcom 0
cbo 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper;
16
17
use Humbug\PhpScoper\Console\Application;
18
use Humbug\PhpScoper\Console\ApplicationFactory;
19
use Iterator;
20
use PhpParser\Node;
21
use PhpParser\Node\Identifier;
22
use PhpParser\Node\Name;
23
use function array_map;
24
use function array_pop;
25
use function count;
26
use function is_array;
27
use function is_object;
28
use function is_scalar;
29
use function is_string;
30
use function method_exists;
31
use function serialize;
32
use function str_split;
33
use function strrpos;
34
use function substr;
35
use function unserialize;
36
37
function create_application(): Application
38
{
39
    return (new ApplicationFactory())->create();
40
}
41
42
/**
43
 * @private
44
 *
45
 * @deprecated Will be removed in future releases.
46
 */
47
function create_scoper(): Scoper
48
{
49
    return (new class() extends ApplicationFactory {
50
        public static function createScoper(): Scoper
51
        {
52
            return parent::createScoper();
53
        }
54
    })::createScoper();
55
}
56
57
/**
58
 * @param string[] $paths Absolute paths
59
 *
60
 * @return string
61
 */
62
function get_common_path(array $paths): string
63
{
64
    $nbPaths = count($paths);
65
    if (0 === $nbPaths) {
66
        return '';
67
    }
68
    $pathRef = array_pop($paths);
69
    if (1 === $nbPaths) {
70
        $commonPath = $pathRef;
71
    } else {
72
        $commonPath = '';
73
        foreach (str_split($pathRef) as $pos => $char) {
74
            foreach ($paths as $path) {
75
                if (!isset($path[$pos]) || $path[$pos] !== $char) {
76
                    break 2;
77
                }
78
            }
79
            $commonPath .= $char;
80
        }
81
    }
82
    foreach (['/', '\\'] as $separator) {
83
        $lastSeparatorPos = strrpos($commonPath, $separator);
84
        if (false !== $lastSeparatorPos) {
85
            $commonPath = rtrim(substr($commonPath, 0, $lastSeparatorPos), $separator);
86
87
            break;
88
        }
89
    }
90
91
    return $commonPath;
92
}
93
94
/**
95
 * In-house clone functions. Does a partial clone that should be enough to provide the immutability required in some
96
 * places for the scoper. It however does not guarantee a deep cloning as would be horribly slow for no good reasons.
97
 * A better alternative would be to find a way to push immutability upstream in PHP-Parser directly.
98
 *
99
 * @param Node $node
100
 *
101
 * @return Node
102
 */
103
function clone_node(Node $node): Node
104
{
105
    $clone = deep_clone($node);
106
107
    foreach ($node->getAttributes() as $key => $attribute) {
108
        $clone->setAttribute($key, $attribute);
109
    }
110
111
    return $clone;
112
}
113
114
/**
115
 * @param mixed $node
116
 *
117
 * @return mixed
118
 *
119
 * @internal
120
 */
121
function deep_clone($node)
122
{
123
    if (is_array($node)) {
124
        return array_map(__FUNCTION__, $node);
125
    }
126
127
    if (null === $node || is_scalar($node)) {
128
        return $node;
129
    }
130
131
    return unserialize(serialize($node));
132
}
133
134
function chain(iterable ...$iterables): Iterator
135
{
136
    foreach ($iterables as $iterable) {
137
        foreach ($iterable as $key => $value) {
138
            yield $key => $value;
139
        }
140
    }
141
}
142
143
function is_stringable($value): bool
144
{
145
    return
146
        null === $value
147
        || is_string($value)
148
        || $value instanceof Name
149
        || $value instanceof Identifier
150
        || (is_object($value) && method_exists($value, '__toString'))
151
    ;
152
}
153