get_common_path()   B
last analyzed

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 Iterator;
18
use PackageVersions\Versions;
19
use function array_pop;
20
use function count;
21
use function Safe\substr;
22
use function str_split;
23
use function strrpos;
24
25
function get_php_scoper_version(): string
26
{
27
    // Since PHP-Scoper relies on COMPOSER_ROOT_VERSION the version parsed by PackageVersions, we rely on Box
28
    // placeholders in order to get the right version for the PHAR.
29
    if (0 === strpos(__FILE__, 'phar:')) {
30
        return '@git_version_placeholder@';
31
    }
32
33
    $rawVersion = Versions::getVersion('humbug/php-scoper');
34
35
    [$prettyVersion, $commitHash] = explode('@', $rawVersion);
36
37
    return $prettyVersion.'@'.substr($commitHash, 0, 7);
0 ignored issues
show
Deprecated Code introduced by
The function Safe\substr() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

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

37
    return $prettyVersion.'@'./** @scrutinizer ignore-deprecated */ substr($commitHash, 0, 7);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
38
}
39
40
/**
41
 * @param string[] $paths Absolute paths
42
 */
43
function get_common_path(array $paths): string
44
{
45
    $nbPaths = count($paths);
46
47
    if (0 === $nbPaths) {
48
        return '';
49
    }
50
51
    $pathRef = (string) array_pop($paths);
52
53
    if (1 === $nbPaths) {
54
        $commonPath = $pathRef;
55
    } else {
56
        $commonPath = '';
57
58
        foreach (str_split($pathRef) as $pos => $char) {
59
            foreach ($paths as $path) {
60
                if (!isset($path[$pos]) || $path[$pos] !== $char) {
61
                    break 2;
62
                }
63
            }
64
65
            $commonPath .= $char;
66
        }
67
    }
68
69
    foreach (['/', '\\'] as $separator) {
70
        $lastSeparatorPos = strrpos($commonPath, $separator);
71
72
        if (false !== $lastSeparatorPos) {
73
            $commonPath = rtrim(substr($commonPath, 0, $lastSeparatorPos), $separator);
0 ignored issues
show
Deprecated Code introduced by
The function Safe\substr() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

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

73
            $commonPath = rtrim(/** @scrutinizer ignore-deprecated */ substr($commonPath, 0, $lastSeparatorPos), $separator);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
74
75
            break;
76
        }
77
    }
78
79
    return $commonPath;
80
}
81
82
function chain(iterable ...$iterables): Iterator
83
{
84
    foreach ($iterables as $iterable) {
85
        yield from $iterable;
86
    }
87
}
88