FileLoader::doImport()   F
last analyzed

Complexity

Conditions 12
Paths 402

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 51
rs 3.6305
c 0
b 0
f 0
cc 12
nc 402
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symfony\Component\Config\Loader;
13
14
use Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException;
15
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
16
use Symfony\Component\Config\Exception\LoaderLoadException;
17
use Symfony\Component\Config\FileLocatorInterface;
18
use Symfony\Component\Config\Resource\FileExistenceResource;
19
use Symfony\Component\Config\Resource\GlobResource;
20
21
/**
22
 * FileLoader is the abstract class used by all built-in loaders that are file based.
23
 *
24
 * @author Fabien Potencier <[email protected]>
25
 */
26
abstract class FileLoader extends Loader
27
{
28
    protected static array $loading = [];
29
30
    private ?string $currentDir = null;
31
32
    public function __construct(
33
        protected FileLocatorInterface $locator,
34
        ?string $env = null,
35
    ) {
36
        parent::__construct($env);
37
    }
38
39
    /**
40
     * Sets the current directory.
41
     */
42
    public function setCurrentDir(string $dir): void
43
    {
44
        $this->currentDir = $dir;
45
    }
46
47
    /**
48
     * Returns the file locator used by this loader.
49
     */
50
    public function getLocator(): FileLocatorInterface
51
    {
52
        return $this->locator;
53
    }
54
55
    /**
56
     * Imports a resource.
57
     *
58
     * @param mixed                $resource       A Resource
59
     * @param string|null          $type           The resource type or null if unknown
60
     * @param bool                 $ignoreErrors   Whether to ignore import errors or not
61
     * @param string|null          $sourceResource The original resource importing the new resource
62
     * @param string|string[]|null $exclude        Glob patterns to exclude from the import
63
     *
64
     * @throws LoaderLoadException
65
     * @throws FileLoaderImportCircularReferenceException
66
     * @throws FileLocatorFileNotFoundException
67
     */
68
    public function import(mixed $resource, ?string $type = null, bool $ignoreErrors = false, ?string $sourceResource = null, string|array|null $exclude = null): mixed
69
    {
70
        if (\is_string($resource) && \strlen($resource) !== ($i = strcspn($resource, '*?{[')) && !str_contains($resource, "\n")) {
71
            $excluded = [];
72
            foreach ((array) $exclude as $pattern) {
73
                foreach ($this->glob($pattern, true, $_, false, true) as $path => $info) {
74
                    // normalize Windows slashes and remove trailing slashes
75
                    $excluded[rtrim(str_replace('\\', '/', $path), '/')] = true;
76
                }
77
            }
78
79
            $ret = [];
80
            $isSubpath = 0 !== $i && str_contains(substr($resource, 0, $i), '/');
81
            foreach ($this->glob($resource, false, $_, $ignoreErrors || !$isSubpath, false, $excluded) as $path => $info) {
82
                if (null !== $res = $this->doImport($path, 'glob' === $type ? null : $type, $ignoreErrors, $sourceResource)) {
83
                    $ret[] = $res;
84
                }
85
                $isSubpath = true;
86
            }
87
88
            if ($isSubpath) {
89
                return isset($ret[1]) ? $ret : ($ret[0] ?? null);
90
            }
91
        }
92
93
        return $this->doImport($resource, $type, $ignoreErrors, $sourceResource);
94
    }
95
96
    /**
97
     * @internal
98
     */
99
    protected function glob(string $pattern, bool $recursive, array|GlobResource|null &$resource = null, bool $ignoreErrors = false, bool $forExclusion = false, array $excluded = []): iterable
100
    {
101
        if (\strlen($pattern) === $i = strcspn($pattern, '*?{[')) {
102
            $prefix = $pattern;
103
            $pattern = '';
104
        } elseif (0 === $i || !str_contains(substr($pattern, 0, $i), '/')) {
105
            $prefix = '.';
106
            $pattern = '/'.$pattern;
107
        } else {
108
            $prefix = \dirname(substr($pattern, 0, 1 + $i));
109
            $pattern = substr($pattern, \strlen($prefix));
110
        }
111
112
        try {
113
            $prefix = $this->locator->locate($prefix, $this->currentDir, true);
114
        } catch (FileLocatorFileNotFoundException $e) {
115
            if (!$ignoreErrors) {
116
                throw $e;
117
            }
118
119
            $resource = [];
120
            foreach ($e->getPaths() as $path) {
121
                $resource[] = new FileExistenceResource($path);
122
            }
123
124
            return;
125
        }
126
        $resource = new GlobResource($prefix, $pattern, $recursive, $forExclusion, $excluded);
0 ignored issues
show
Bug introduced by
It seems like $prefix can also be of type array; however, parameter $prefix of Symfony\Component\Config...Resource::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

126
        $resource = new GlobResource(/** @scrutinizer ignore-type */ $prefix, $pattern, $recursive, $forExclusion, $excluded);
Loading history...
127
128
        yield from $resource;
129
    }
130
131
    private function doImport(mixed $resource, ?string $type = null, bool $ignoreErrors = false, ?string $sourceResource = null): mixed
132
    {
133
        try {
134
            $loader = $this->resolve($resource, $type);
135
136
            if ($loader instanceof DirectoryAwareLoaderInterface) {
137
                $loader = $loader->forDirectory($this->currentDir);
0 ignored issues
show
Bug introduced by
It seems like $this->currentDir can also be of type null; however, parameter $currentDirectory of Symfony\Component\Config...terface::forDirectory() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

137
                $loader = $loader->forDirectory(/** @scrutinizer ignore-type */ $this->currentDir);
Loading history...
138
            }
139
140
            if (!$loader instanceof self) {
141
                return $loader->load($resource, $type);
142
            }
143
144
            if (null !== $this->currentDir) {
145
                $resource = $loader->getLocator()->locate($resource, $this->currentDir, false);
146
            }
147
148
            $resources = \is_array($resource) ? $resource : [$resource];
149
            for ($i = 0; $i < $resourcesCount = \count($resources); ++$i) {
150
                if (isset(self::$loading[$resources[$i]])) {
151
                    if ($i == $resourcesCount - 1) {
152
                        throw new FileLoaderImportCircularReferenceException(array_keys(self::$loading));
153
                    }
154
                } else {
155
                    $resource = $resources[$i];
156
                    break;
157
                }
158
            }
159
            self::$loading[$resource] = true;
160
161
            try {
162
                $ret = $loader->load($resource, $type);
163
            } finally {
164
                unset(self::$loading[$resource]);
165
            }
166
167
            return $ret;
168
        } catch (FileLoaderImportCircularReferenceException $e) {
169
            throw $e;
170
        } catch (\Exception $e) {
171
            if (!$ignoreErrors) {
172
                // prevent embedded imports from nesting multiple exceptions
173
                if ($e instanceof LoaderLoadException) {
174
                    throw $e;
175
                }
176
177
                throw new LoaderLoadException($resource, $sourceResource, 0, $e, $type);
178
            }
179
        }
180
181
        return null;
182
    }
183
}
184