Passed
Push — master ( d45875...f59561 )
by Théo
02:44
created

functions.php$0 ➔ createScoper()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 PhpParser\Parser;
24
use function array_map;
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 strlen;
33
use function strpos;
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
    if (0 === count($paths)) {
65
        return '';
66
    }
67
68
    $lastOffset = 1;
69
    $common = DIRECTORY_SEPARATOR;
70
71
    while (false !== ($index = strpos($paths[0], DIRECTORY_SEPARATOR, $lastOffset))) {
72
        $dirLen = $index - $lastOffset + 1;
73
        $dir = substr($paths[0], $lastOffset, $dirLen);
74
75
        foreach ($paths as $path) {
76
            if (substr($path, $lastOffset, $dirLen) !== $dir) {
77 View Code Duplication
                if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) {
78
                    $common = substr($common, 0, -1);
79
                }
80
81
                return $common;
82
            }
83
        }
84
85
        $common .= $dir;
86
        $lastOffset = $index + 1;
87
    }
88
89
    $common = substr($common, 0, -1);
90
91 View Code Duplication
    if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) {
92
        $common = substr($common, 0, -1);
93
    }
94
95
    return $common;
96
}
97
98
/**
99
 * In-house clone functions. Does a partial clone that should be enough to provide the immutability required in some
100
 * places for the scoper. It however does not guarantee a deep cloning as would be horribly slow for no good reasons.
101
 * A better alternative would be to find a way to push immutability upstream in PHP-Parser directly.
102
 *
103
 * @param Node $node
104
 *
105
 * @return Node
106
 */
107
function clone_node(Node $node): Node
108
{
109
    $clone = deep_clone($node);
110
111
    foreach ($node->getAttributes() as $key => $attribute) {
112
        $clone->setAttribute($key, $attribute);
113
    }
114
115
    return $clone;
116
}
117
118
/**
119
 * @param mixed $node
120
 *
121
 * @return mixed
122
 *
123
 * @internal
124
 */
125
function deep_clone($node)
126
{
127
    if (is_array($node)) {
128
        return array_map(__FUNCTION__, $node);
129
    }
130
131
    if (null === $node || is_scalar($node)) {
132
        return $node;
133
    }
134
135
    return unserialize(serialize($node));
136
}
137
138
function chain(iterable ...$iterables): Iterator
139
{
140
    foreach ($iterables as $iterable) {
141
        foreach ($iterable as $key => $value) {
142
            yield $key => $value;
143
        }
144
    }
145
}
146
147
function is_stringable($value): bool
148
{
149
    return
150
        null === $value
151
        || is_string($value)
152
        || $value instanceof Name
153
        || $value instanceof Identifier
154
        || (is_object($value) && method_exists($value, '__toString'))
155
    ;
156
}
157