XmlScoper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 76
dl 0
loc 153
rs 10
c 1
b 0
f 0
ccs 51
cts 51
cp 1
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A scope() 0 18 2
B replaceClasses() 0 56 7
A scopeClasses() 0 26 2
A scopeNamespaces() 0 17 2
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 Humbug\PhpScoper\Scoper\Scoper;
18
use Humbug\PhpScoper\Symbol\EnrichedReflector;
19
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
20
use PhpParser\Node\Name\FullyQualified;
21
use function array_filter;
22
use function func_get_args;
23
use function preg_match as native_preg_match;
24
use function preg_match_all as native_preg_match_all;
25
use function Safe\substr;
26
use function str_replace;
27
use function strlen;
28
use function strpos;
29
30
/**
31
 * Scopes the Symfony XML configuration files.
32
 */
33
final class XmlScoper implements Scoper
34
{
35
    private const XML_EXTENSION_REGEX = '/\.xml$/i';
36 15
    private const NAMESPACE_PATTERN = '/<prototype.*\snamespace="(?:(?<namespace>(?:[^\\\\]+(?<separator>\\\\(?:\\\\)?))))"/';
37
    private const SINGLE_CLASS_PATTERN = '/(?:(?<singleClass>(?:[\p{L}_\d]+(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_\d]+(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u';
38 15
39
    private Scoper $decoratedScoper;
40
    private string $prefix;
41
    private EnrichedReflector $enrichedReflector;
42
    private SymbolsRegistry $symbolsRegistry;
43
44 14
    public function __construct(
45
        Scoper $decoratedScoper,
46 14
        string $prefix,
47 3
        EnrichedReflector $enrichedReflector,
48
        SymbolsRegistry $symbolsRegistry
49
    ) {
50 11
        $this->decoratedScoper = $decoratedScoper;
51 11
        $this->prefix = $prefix;
52
        $this->enrichedReflector = $enrichedReflector;
53 11
        $this->symbolsRegistry = $symbolsRegistry;
54
    }
55
56 11
    public function scope(string $filePath, string $contents): string
57
    {
58 11
        if (1 !== native_preg_match(self::XML_EXTENSION_REGEX, $filePath)) {
59 3
            return $this->decoratedScoper->scope(...func_get_args());
60
        }
61
62 8
        $contents = self::scopeClasses(
63 8
            $contents,
64 8
            $this->prefix,
65 8
            $this->enrichedReflector,
66 8
            $this->symbolsRegistry,
67 8
        );
68
69
        return self::scopeNamespaces(
70 8
            $contents,
71 8
            $this->prefix,
72 8
            $this->enrichedReflector,
73 8
            $this->symbolsRegistry,
74 8
        );
75 8
    }
76
77
    private static function scopeClasses(
78 8
        string $contents,
79
        string $prefix,
80
        EnrichedReflector $enrichedReflector,
81 11
        SymbolsRegistry $symbolsRegistry
82
    ): string {
83 11
        if (1 > native_preg_match_all(self::SINGLE_CLASS_PATTERN, $contents, $matches)) {
84 10
            return $contents;
85
        }
86
87 1
        $contents = self::replaceClasses(
88 1
            array_filter($matches['singleClass']),
89 1
            array_filter($matches['singleSeparator']),
90 1
            $prefix,
91 1
            $contents,
92 1
            $enrichedReflector,
93
            $symbolsRegistry,
94
        );
95
96
        return self::replaceClasses(
97
            array_filter($matches['class']),
98
            array_filter($matches['separator']),
99
            $prefix,
100 8
            $contents,
101
            $enrichedReflector,
102
            $symbolsRegistry,
103
        );
104
    }
105
106
    private static function scopeNamespaces(
107 8
        string $contents,
108 8
        string $prefix,
109
        EnrichedReflector $enrichedReflector,
110
        SymbolsRegistry $symbolsRegistry
111 8
    ): string {
112
        if (1 > native_preg_match_all(self::NAMESPACE_PATTERN, $contents, $matches)) {
113 8
            return $contents;
114 8
        }
115
116 8
        return self::replaceClasses(
117 8
            array_filter($matches['namespace']),
118
            array_filter($matches['separator']),
119 8
            $prefix,
120
            $contents,
121 8
            $enrichedReflector,
122 1
            $symbolsRegistry,
123 8
        );
124
    }
125
126 8
    /**
127 1
     * @param string[] $classes
128 1
     * @param string[] $separators
129 1
     */
130
    private static function replaceClasses(
131
        array $classes,
132
        array $separators,
133
        string $prefix,
134 8
        string $contents,
135
        EnrichedReflector $enrichedReflector,
136 8
        SymbolsRegistry $symbolsRegistry
137
    ): string {
138
        if ([] === $classes) {
139
            return $contents;
140
        }
141
142
        $scopedContents = '';
143
144
        foreach ($classes as $index => $class) {
145
            $separator = $separators[$index];
146
147
            $psr4Service = '"'.$class.$separator.'"';
148
149
            if (false !== strpos($contents, $psr4Service)) {
150
                $offset = strpos($contents, $psr4Service) + strlen($psr4Service);
151
152
                $stringToScope = substr($contents, 0, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function Safe\substr() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

152
                $stringToScope = /** @scrutinizer ignore-deprecated */ substr($contents, 0, $offset);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
153
                $contents = substr($contents, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function Safe\substr() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

153
                $contents = /** @scrutinizer ignore-deprecated */ substr($contents, $offset);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
154
155
                $prefixedClass = $prefix.$separator.$class;
156
157
                $scopedContents .= $enrichedReflector->belongsToExcludedNamespace($class.$separator.'__UnknownService__')
158
                    ? $stringToScope
159
                    : str_replace($class, $prefixedClass, $stringToScope);
160
161
                continue;
162
            }
163
164
            $offset = strpos($contents, $class) + strlen($class);
165
166
            $stringToScope = substr($contents, 0, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function Safe\substr() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

166
            $stringToScope = /** @scrutinizer ignore-deprecated */ substr($contents, 0, $offset);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
167
            $contents = substr($contents, $offset);
0 ignored issues
show
Deprecated Code introduced by
The function Safe\substr() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

167
            $contents = /** @scrutinizer ignore-deprecated */ substr($contents, $offset);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
168
169
            $prefixedClass = $prefix.$separator.$class;
170
171
            $scopedContents .= $enrichedReflector->belongsToExcludedNamespace($class)
172
                ? $stringToScope
173
                : str_replace($class, $prefixedClass, $stringToScope);
174
175
            if ($enrichedReflector->isExposedClass($class)) {
176
                $symbolsRegistry->recordClass(
177
                    new FullyQualified($class),
178
                    new FullyQualified($prefixedClass),
179
                );
180
            }
181
        }
182
183
        $scopedContents .= $contents;
184
185
        return $scopedContents;
186
    }
187
}
188