Completed
Push — master ( 954942...2b19b6 )
by Théo
02:40
created

functions.php ➔ get_common_path()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 35
Code Lines 19

Duplication

Lines 6
Ratio 17.14 %

Code Coverage

Tests 17
CRAP Score 9.0139

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 9
nop 1
dl 6
loc 35
ccs 17
cts 18
cp 0.9444
crap 9.0139
rs 4.909
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\Command\AddPrefixCommand;
19
use Humbug\PhpScoper\Handler\HandleAddPrefix;
20
use PackageVersions\Versions;
21
use PhpParser\Parser;
22
use PhpParser\ParserFactory;
23
use Symfony\Component\Console\Application as SymfonyApplication;
24
use Symfony\Component\Filesystem\Filesystem;
25
26
/**
27
 * @private
28
 */
29
function create_application(): SymfonyApplication
30
{
31
    $app = new Application('PHP Scoper', get_version());
32
33
    $app->addCommands([
34
        new AddPrefixCommand(
35
            new Filesystem(),
36
            new HandleAddPrefix(
37
                new Scoper(
38
                    create_parser()
39
                )
40
            )
41
        ),
42
    ]);
43
44
    return $app;
45
}
46
47
/**
48
 * @private
49
 */
50
function get_version(): string
51
{
52
    $rawVersion = Versions::getVersion('humbug/php-scoper');
53
54
    list($prettyVersion, $commitHash) = explode('@', $rawVersion);
55
56
    return (1 === preg_match('/9{7}/', $prettyVersion)) ? $commitHash : $prettyVersion;
57
}
58
59
/**
60
 * @private
61
 */
62
function create_parser(): Parser
63
{
64
    return (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
65
}
66
67
/**
68
 * @param string[] $paths Absolute paths
69
 *
70
 * @return string
71
 */
72
function get_common_path(array $paths): string
73
{
74 6
    if (0 === count($paths)) {
75 1
        return '';
76
    }
77
78 5
    $lastOffset = 1;
79 5
    $common = DIRECTORY_SEPARATOR;
80
81 5
    while (false !== ($index = strpos($paths[0], DIRECTORY_SEPARATOR, $lastOffset))) {
82 4
        $dirLen = $index - $lastOffset + 1;
83 4
        $dir = substr($paths[0], $lastOffset, $dirLen);
84
85 4
        foreach ($paths as $path) {
86 4
            if (substr($path, $lastOffset, $dirLen) !== $dir) {
87 2 View Code Duplication
                if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) {
88 2
                    $common = substr($common, 0, strlen($common) - 1);
89
                }
90
91 4
                return $common;
92
            }
93
        }
94
95 3
        $common .= $dir;
96 3
        $lastOffset = $index + 1;
97
    }
98
99 3
    $common = substr($common, 0, -1);
100
101 3 View Code Duplication
    if (0 < strlen($common) && DIRECTORY_SEPARATOR === $common[strlen($common) - 1]) {
102
        $common = substr($common, 0, strlen($common) - 1);
103
    }
104
105 3
    return $common;
106
}
107