Passed
Pull Request — master (#558)
by Théo
02:10
created

SymfonyPatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 32
ccs 14
cts 14
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 13 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 SymfonyPatcher
22
{
23
    private const PATHS = [
24
        'src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php',
25
        'symfony/dependency-injection/Dumper/PhpDumper.php',
26
    ];
27 21
28
    public function __invoke(string $filePath, string $prefix, string $contents): string
29 21
    {
30 9
        if (!self::isSupportedFile($filePath)) {
31
            return $contents;
32
        }
33 12
34 12
        return (string) preg_replace(
35 12
            '/use (Symfony(\\\\(?:\\\\)?)Component\\\\.+?;)/',
36 12
            sprintf(
37 12
                'use %s$2$1',
38
                $prefix
39 12
            ),
40
            $contents
41
        );
42
    }
43 21
44
    private static function isSupportedFile(string $filePath): bool
45 21
    {
46 21
        foreach (self::PATHS as $path) {
47 12
            if (false !== strpos($filePath, $path)) {
48
                return true;
49
            }
50
        }
51 9
52
        return false;
53
    }
54
}
55