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

SymfonyPatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 15 2
A isValidPath() 0 10 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 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