GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 38973a...53549f )
by Matthew
01:59
created

FileLoader::setCacheDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/*
3
 * Copyright (c) 2015 Matthew Loberg
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
namespace Mlo\FileLoader;
8
9
use Symfony\Component\Config\ConfigCache;
10
use Symfony\Component\Config\Definition\ConfigurationInterface;
11
use Symfony\Component\Config\Definition\Processor;
12
use Symfony\Component\Config\FileLocator;
13
use Symfony\Component\Config\Loader\DelegatingLoader;
14
use Symfony\Component\Config\Loader\LoaderInterface;
15
use Symfony\Component\Config\Loader\LoaderResolver;
16
use Symfony\Component\Config\Resource\FileResource;
17
18
/**
19
 * FileLoader
20
 *
21
 * @author Matthew Loberg <[email protected]>
22
 */
23
class FileLoader
24
{
25
    /**
26
     * @var string
27
     */
28
    private $cacheDirectory;
29
30
    /**
31
     * @var array
32
     */
33
    private $directories;
34
35
    /**
36
     * @var LoaderInterface[]|array
37
     */
38
    private $loaders = [];
39
40
    /**
41
     * Constructor
42
     *
43
     * @param string                  $cacheDirectory
44
     * @param array|string            $directories
45
     * @param LoaderInterface[]|array $loaders
46
     */
47
    public function __construct($cacheDirectory, $directories = null, array $loaders = [])
48
    {
49
        $this->cacheDirectory = $cacheDirectory;
50
        $this->directories    = (array) $directories;
51
52
        foreach ($loaders as $loader) {
53
            $this->addLoader($loader);
54
        }
55
    }
56
57
    /**
58
     * Get cache directory
59
     *
60
     * @return string
61
     */
62
    public function getCacheDirectory()
63
    {
64
        return $this->cacheDirectory;
65
    }
66
67
    /**
68
     * Set cache directory
69
     *
70
     * @param string $cacheDirectory
71
     *
72
     * @return FileLoader
73
     */
74
    public function setCacheDirectory($cacheDirectory)
75
    {
76
        $this->cacheDirectory = $cacheDirectory;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Get directories
83
     *
84
     * @return array
85
     */
86
    public function getDirectories()
87
    {
88
        return $this->directories;
89
    }
90
91
    /**
92
     * Set directories
93
     *
94
     * @param array $directories
95
     *
96
     * @return FileLoader
97
     */
98
    public function setDirectories(array $directories)
99
    {
100
        $this->directories = $directories;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Add directory
107
     *
108
     * @param string $directory
109
     *
110
     * @return FileLoader
111
     */
112
    public function addDirectory($directory)
113
    {
114
        if (!in_array($directory, $this->directories)) {
115
            $this->directories[] = $directory;
116
        }
117
118
        return $this;
119
    }
120
121
    /**
122
     * Get Loaders
123
     *
124
     * @return LoaderInterface[]|array
125
     */
126
    public function getLoaders()
127
    {
128
        return $this->loaders;
129
    }
130
131
    /**
132
     * Add loader
133
     *
134
     * @param LoaderInterface $loader
135
     *
136
     * @return FileLoader
137
     */
138
    public function addLoader(LoaderInterface $loader)
139
    {
140
        $this->loaders[] = $loader;
141
142
        return $this;
143
    }
144
145
    /**
146
     * Load from file
147
     *
148
     * @param string $fileName
149
     * @param bool   $refresh
150
     *
151
     * @return array
152
     * @throws \Symfony\Component\Config\Exception\FileLoaderLoadException
153
     */
154
    public function load($fileName, $refresh = false)
155
    {
156
        $cachePath = $this->getCacheDirectory() . DIRECTORY_SEPARATOR . $fileName . '.php';
157
        $cache     = new ConfigCache($cachePath, true);
158
159
        if ($refresh || !$cache->isFresh()) {
160
            $resolver = new LoaderResolver($this->loaders);
161
            $loader   = new DelegatingLoader($resolver);
162
            $locator  = new FileLocator($this->getDirectories());
163
164
            $filePath = $locator->locate($fileName);
165
            $values   = $loader->load($filePath);
166
            $resource = new FileResource($filePath);
0 ignored issues
show
Bug introduced by
It seems like $filePath defined by $locator->locate($fileName) on line 164 can also be of type array; however, Symfony\Component\Config...Resource::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
167
168
            $cacheValue = sprintf("<?php return %s;", var_export($values, true));
169
170
            $cache->write($cacheValue, [$resource]);
171
        } else {
172
            $values = require($cachePath);
173
        }
174
175
        return $values;
176
    }
177
178
    /**
179
     * Load from file and apply configuration
180
     *
181
     * @param string                 $fileName
182
     * @param ConfigurationInterface $configuration
183
     * @param bool                   $refresh
184
     *
185
     * @return array
186
     */
187
    public function loadWithConfiguration($fileName, ConfigurationInterface $configuration, $refresh = false)
188
    {
189
        $tree = $configuration->getConfigTreeBuilder()->buildTree();
190
        $name = $tree->getName();
191
192
        $cachePath = $this->getCacheDirectory() . DIRECTORY_SEPARATOR . $fileName . '.config.' . $name . '.php';
193
194
        $cache = new ConfigCache($cachePath, true);
195
196
        if ($refresh || !$cache->isFresh()) {
197
            $config = $this->load($fileName, $refresh);
198
199
            $refClass = new \ReflectionClass(get_class($configuration));
200
            $resource = new FileResource($refClass->getFileName());
201
202
            $processor = new Processor();
203
            $values = $processor->process($tree, $config);
204
205
            $retval = sprintf("<?php\nreturn %s;", var_export($values, true));
206
207
            $cache->write($retval, [$resource]);
208
        } else {
209
            $values = require($cachePath);
210
        }
211
212
        return $values;
213
    }
214
}
215