Passed
Push — master ( fecc04...e5efab )
by Théo
03:04
created

SymfonyPatcher::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 2
rs 9.7666
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\Patcher;
16
17
use function preg_replace;
18
use function strpos;
19
20
final class SymfonyPatcher
21
{
22
    private const PATHS = [
23
        'src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php',
24
        'symfony/dependency-injection/Dumper/PhpDumper.php',
25
    ];
26
27 21
    public function __invoke(string $filePath, string $prefix, string $contents): string
28
    {
29 21
        if (false === $this->isValidPath($filePath)) {
30 9
            return $contents;
31
        }
32
33 12
        return preg_replace(
34 12
            '/use (Symfony(\\\\(?:\\\\)?)Component\\\\.+?;)/',
35 12
            sprintf(
36 12
                'use %s$2$1',
37 12
                $prefix
38
            ),
39 12
            $contents
40
        );
41
    }
42
43 21
    private function isValidPath(string $filePath): bool
44
    {
45 21
        foreach (self::PATHS as $path) {
46 21
            if (false !== strpos($filePath, $path)) {
47 21
                return true;
48
            }
49
        }
50
51 9
        return false;
52
    }
53
}
54