Passed
Push — master ( a0e7de...dbda56 )
by Théo
02:21
created

get_common_path()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 37
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 10
nop 1
dl 0
loc 37
rs 8.0555
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 PackageVersions\Versions;
21
use function array_pop;
22
use function count;
23
use function str_split;
24
use function strrpos;
25
use function substr;
26
27
function create_application(): Application
28
{
29
    return (new ApplicationFactory())->create();
30
}
31
32
function get_php_scoper_version(): string
33
{
34
    // Since PHP-Scoper relies on COMPOSER_ROOT_VERSION the version parsed by PackageVersions, we rely on Box
35
    // placeholders in order to get the right version for the PHAR.
36
    if (0 === strpos(__FILE__, 'phar:')) {
37
        return '@git_version_placeholder@';
38
    }
39
40
    $rawVersion = Versions::getVersion('humbug/php-scoper');
41
42
    [$prettyVersion, $commitHash] = explode('@', $rawVersion);
43
44
    return $prettyVersion.'@'.substr($commitHash, 0, 7);
45
}
46
47
/**
48
 * @param string[] $paths Absolute paths
49
 *
50
 * @return string
51
 */
52
function get_common_path(array $paths): string
53
{
54
    $nbPaths = count($paths);
55
56
    if (0 === $nbPaths) {
57
        return '';
58
    }
59
60
    $pathRef = (string) array_pop($paths);
61
62
    if (1 === $nbPaths) {
63
        $commonPath = $pathRef;
64
    } else {
65
        $commonPath = '';
66
67
        foreach (str_split($pathRef) as $pos => $char) {
68
            foreach ($paths as $path) {
69
                if (!isset($path[$pos]) || $path[$pos] !== $char) {
70
                    break 2;
71
                }
72
            }
73
74
            $commonPath .= $char;
75
        }
76
    }
77
78
    foreach (['/', '\\'] as $separator) {
79
        $lastSeparatorPos = strrpos($commonPath, $separator);
80
81
        if (false !== $lastSeparatorPos) {
82
            $commonPath = rtrim(substr($commonPath, 0, $lastSeparatorPos), $separator);
83
84
            break;
85
        }
86
    }
87
88
    return $commonPath;
89
}
90
91
function chain(iterable ...$iterables): Iterator
92
{
93
    foreach ($iterables as $iterable) {
94
        yield from $iterable;
95
    }
96
}
97