YamlScoper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
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\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 YAML configuration files.
32
 */
33
final class YamlScoper implements Scoper
34
{
35
    private const YAML_EXTENSION_REGEX = '/\.ya?ml$/i';
36
    private const CLASS_PATTERN = '/(?:(?<singleClass>(?:[\p{L}_\d]+(?<singleSeparator>\\\\(?:\\\\)?))):)|(?<class>(?:[\p{L}_\d]+(?<separator>\\\\(?:\\\\)?)+)+[\p{L}_\d]+)/u';
37 20
38
    private Scoper $decoratedScoper;
39 20
    private string $prefix;
40
    private EnrichedReflector $enrichedReflector;
41
    private SymbolsRegistry $symbolsRegistry;
42
43
    public function __construct(
44
        Scoper $decoratedScoper,
45 19
        string $prefix,
46
        EnrichedReflector $enrichedReflector,
47 19
        SymbolsRegistry $symbolsRegistry
48 3
    ) {
49
        $this->decoratedScoper = $decoratedScoper;
50
        $this->prefix = $prefix;
51 16
        $this->enrichedReflector = $enrichedReflector;
52 5
        $this->symbolsRegistry = $symbolsRegistry;
53
    }
54
55 11
    public function scope(string $filePath, string $contents): string
56 11
    {
57 11
        if (1 !== native_preg_match(self::YAML_EXTENSION_REGEX, $filePath)) {
58 11
            return $this->decoratedScoper->scope(...func_get_args());
59 11
        }
60 11
61
        if (1 > native_preg_match_all(self::CLASS_PATTERN, $contents, $matches)) {
62
            return $contents;
63 11
        }
64 11
65 11
        $contents = self::replaceClasses(
66 11
            array_filter($matches['singleClass']),
67 11
            array_filter($matches['singleSeparator']),
68 11
            $this->prefix,
69
            $contents,
70
            $this->enrichedReflector,
71 11
            $this->symbolsRegistry,
72
        );
73
74
        return self::replaceClasses(
75
            array_filter($matches['class']),
76
            array_filter($matches['separator']),
77
            $this->prefix,
78 11
            $contents,
79
            $this->enrichedReflector,
80
            $this->symbolsRegistry,
81
        );
82
    }
83
84
    /**
85 11
     * @param string[] $classes
86 10
     * @param string[] $separators
87
     */
88
    private static function replaceClasses(
89 11
        array $classes,
90
        array $separators,
91 11
        string $prefix,
92 11
        string $contents,
93
        EnrichedReflector $enrichedReflector,
94 11
        SymbolsRegistry $symbolsRegistry
95 11
    ): string {
96
        if ([] === $classes) {
97 11
            return $contents;
98
        }
99 11
100 1
        $scopedContents = '';
101 11
102
        foreach ($classes as $index => $class) {
103
            $separator = $separators[$index];
104 11
105 1
            $psr4Service = $class.$separator.':';
106 1
107 1
            if (false !== strpos($contents, $psr4Service)) {
108
                $offset = strpos($contents, $psr4Service) + strlen($psr4Service);
109
110
                $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

110
                $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...
111
                $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

111
                $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...
112 11
113
                $prefixedClass = $prefix.$separator.$class;
114 11
115
                $scopedContents .= $enrichedReflector->belongsToExcludedNamespace($class.$separator.'__UnknownService__')
116
                    ? $stringToScope
117
                    : str_replace($class, $prefixedClass, $stringToScope);
118
119
                continue;
120
            }
121
122
            $offset = strpos($contents, $class) + strlen($class);
123
124
            $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

124
            $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...
125
            $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

125
            $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...
126
127
            $prefixedClass = $prefix.$separator.$class;
128
129
            $scopedContents .= $enrichedReflector->belongsToExcludedNamespace($class)
130
                ? $stringToScope
131
                : str_replace($class, $prefixedClass, $stringToScope);
132
133
            if ($enrichedReflector->isExposedClass($class)) {
134
                $symbolsRegistry->recordClass(
135
                    new FullyQualified($class),
136
                    new FullyQualified($prefixedClass),
137
                );
138
            }
139
        }
140
141
        $scopedContents .= $contents;
142
143
        return $scopedContents;
144
    }
145
}
146