Passed
Push — master ( 829152...255726 )
by Théo
02:02
created

YamlScoper::scope()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 17
c 1
b 0
f 0
nc 3
nop 5
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 3
rs 9.7
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\Scoper\Symfony;
16
17
use function array_filter;
18
use function func_get_args;
19
use Humbug\PhpScoper\Scoper;
20
use Humbug\PhpScoper\Whitelist;
21
use PhpParser\Node\Name\FullyQualified;
22
use function preg_match_all;
23
use function str_replace;
24
use function strlen;
25
use function strpos;
26
use function substr;
27
28
/**
29
 * Scopes the Symfony YAML configuration files.
30
 */
31
final class YamlScoper implements Scoper
32
{
33
    private const FILE_PATH_PATTERN = '/\.ya?ml$/i';
34
35
    private $decoratedScoper;
36
37 20
    public function __construct(Scoper $decoratedScoper)
38
    {
39 20
        $this->decoratedScoper = $decoratedScoper;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 19
    public function scope(string $filePath, string $contents, string $prefix, array $patchers, Whitelist $whitelist): string
46
    {
47 19
        if (1 !== preg_match(self::FILE_PATH_PATTERN, $filePath)) {
48 3
            return $this->decoratedScoper->scope(...func_get_args());
49
        }
50
51 16
        if (1 > preg_match_all('/(?:(?<singleClass>(?:[\p{L}_\d]+(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_\d]+(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u', $contents, $matches)) {
52 5
            return $contents;
53
        }
54
55 11
        $contents = $this->replaceClasses(
56 11
            array_filter($matches['singleClass']),
57 11
            array_filter($matches['singleSeparator']),
58 11
            $prefix,
59 11
            $contents,
60 11
            $whitelist
61
        );
62
63 11
        $contents = $this->replaceClasses(
64 11
            array_filter($matches['class']),
65 11
            array_filter($matches['separator']),
66 11
            $prefix,
67 11
            $contents,
68 11
            $whitelist
69
        );
70
71 11
        return $contents;
72
    }
73
74
    /**
75
     * @param string[] $classes
76
     * @param string[] $separators
77
     */
78 11
    private function replaceClasses(
79
        array $classes,
80
        array $separators,
81
        string $prefix,
82
        string $contents,
83
        Whitelist $whitelist
84
    ): string {
85 11
        if ([] === $classes) {
86 10
            return $contents;
87
        }
88
89 11
        $scopedContents = '';
90
91 11
        foreach ($classes as $index => $class) {
92 11
            $separator = $separators[$index];
93
94 11
            $psr4Service = $class.$separator.':';
95 11
96
            if (false !== strpos($contents, $psr4Service)) {
97 11
                $offset = strpos($contents, $psr4Service) + strlen($psr4Service);
98
99 11
                $stringToScope = substr($contents, 0, $offset);
100 1
                $contents = substr($contents, $offset);
101 11
102
                $prefixedClass = $prefix.$separator.$class;
103
104 11
                $scopedContents .= $whitelist->belongsToWhitelistedNamespace($class.$separator.'__UnknownService__')
105 1
                    ? $stringToScope
106 1
                    : str_replace($class, $prefixedClass, $stringToScope)
107 1
                ;
108
109
                continue;
110
            }
111
112 11
            $offset = strpos($contents, $class) + strlen($class);
113
114 11
            $stringToScope = substr($contents, 0, $offset);
115
            $contents = substr($contents, $offset);
116
117
            $prefixedClass = $prefix.$separator.$class;
118
119
            $scopedContents .= $whitelist->belongsToWhitelistedNamespace($class)
120
                ? $stringToScope
121
                : str_replace($class, $prefixedClass, $stringToScope)
122
            ;
123
124
            if ($whitelist->isSymbolWhitelisted($class) || $whitelist->isGlobalWhitelistedClass($class)) {
125
                $whitelist->recordWhitelistedClass(
126
                    new FullyQualified($class),
127
                    new FullyQualified($prefixedClass)
128
                );
129
            }
130
        }
131
132
        $scopedContents .= $contents;
133
134
        return $scopedContents;
135
    }
136
}
137