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

SymfonyPatcher::isSupportedFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 3
rs 10
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