Passed
Pull Request — master (#556)
by Théo
02:01
created

ComposerPatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 23
c 1
b 1
f 0
dl 0
loc 45
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 26 2
A isSupportedFile() 0 9 3
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\Patcher;
16
17
use function Safe\preg_replace;
18
use function Safe\sprintf;
19
use function strpos;
20
21
final class ComposerPatcher
22
{
23
    private const PATHS = [
24
        'src/Composer/Package/Loader/ArrayLoader.php',
25
        'src/Composer/Package/Loader/RootPackageLoader.php',
26
    ];
27
28
    public function __invoke(string $filePath, string $prefix, string $contents): string
29
    {
30
        if (!self::isSupportedFile($filePath)) {
31
            return $contents;
32
        }
33
34
        return str_replace(
35
            [
36
                '\'Composer\\Package\\RootPackage\'',
37
                '\'Composer\\\\Package\\\\RootPackage\'',
38
                ' Composer\\Package\\RootPackage ',
39
40
                '\'Composer\\Package\\CompletePackage\'',
41
                '\'Composer\\\\Package\\\\CompletePackage\'',
42
                ' Composer\\Package\\CompletePackage ',
43
            ],
44
            [
45
                '\''.$prefix.'\\Composer\\Package\\RootPackage\'',
46
                '\''.$prefix.'\\\\Composer\\\\Package\\\\RootPackage\'',
47
                ' '.$prefix.'\\Composer\\Package\\RootPackage ',
48
49
                '\''.$prefix.'\\Composer\\Package\\CompletePackage\'',
50
                '\''.$prefix.'\\\\Composer\\\\Package\\\\CompletePackage\'',
51
                ' '.$prefix.'\\Composer\\Package\\CompletePackage ',
52
            ],
53
            $contents,
54
        );
55
    }
56
57
    private static function isSupportedFile(string $filePath): bool
58
    {
59
        foreach (self::PATHS as $path) {
60
            if (false !== strpos($filePath, $path)) {
61
                return true;
62
            }
63
        }
64
65
        return false;
66
    }
67
}
68