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.

LoaderProvider::getLoaders()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of the m1\vars library
5
 *
6
 * (c) m1 <[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
 * @package     m1/vars
12
 * @version     1.1.0
13
 * @author      Miles Croxford <[email protected]>
14
 * @copyright   Copyright (c) Miles Croxford <[email protected]>
15
 * @license     http://github.com/m1/vars/blob/master/LICENSE
16
 * @link        http://github.com/m1/vars/blob/master/README.MD Documentation
17
 */
18
19
namespace M1\Vars\Loader;
20
21
/**
22
 * This file provides the loaders and extensions supported by Vars
23
 *
24
 * @since 0.1.1
25
 */
26
class LoaderProvider
27
{
28
    /**
29
     * The available extensions
30
     *
31
     * @var array $extensions
32
     */
33
    private $extensions = array();
34
35
    /**
36
     * The available loaders
37
     *
38
     * @var array $loaders
39
     */
40
    private $loaders = array();
41
42
    /**
43
     * The constructor for LoaderProvider, makes the loaders and extensions
44
     *
45
     * @param array|null $options           The options being used for Vars
46
     * @param array      $default_loaders   The default loaders for Vars
47
     */
48 84
    public function __construct($options, $default_loaders)
49
    {
50 84
        $this->makeLoaders($options, $default_loaders);
51 82
    }
52
53
    /**
54
     * Get loaders and make extensions for the loaders
55
     *
56
     * @param array|null $options           The options being used for Vars
57
     * @param array      $default_loaders   The default loaders for Vars
58
     */
59 84
    private function makeLoaders($options, $default_loaders)
60
    {
61 84
        $loaders = $this->preParseLoaders($options, $default_loaders);
62 84
        $parsed_loaders = array();
63
64 84
        foreach ($loaders as $loader) {
65 84
            if ($loader === 'default') {
66 2
                $parsed_loaders = array_merge($parsed_loaders, $default_loaders);
67
            } else {
68 84
                $parsed_loaders[] = $loader;
69
            }
70
        }
71
72 84
        $parsed_loaders = array_unique($parsed_loaders);
73
74 84
        $this->loaders = $this->makeNameSpaceLoaders($parsed_loaders, $default_loaders);
75 83
        $this->extensions = $this->makeExtensions($this->loaders);
76 82
    }
77
78
    /**
79
     * Pre parse the loaders for use in make loaders
80
     *
81
     * @param array|null $options           The options being used for Vars
82
     * @param array      $default_loaders   The default loaders for Vars
83
     *
84
     * @return array The pre parsed loaders
85
     */
86 84
    private function preParseLoaders($options, $default_loaders)
87
    {
88 84
        $loaders = array();
89
90 84
        if (is_array($options['loaders']) && !empty($options['loaders'])) {
91 79
            $loaders = $options['loaders'];
92 5
        } elseif (is_string($options['loaders'])) {
93 4
            $loaders[] = $options['loaders'];
94
        } else {
95 1
            $loaders = $default_loaders;
96
        }
97
98 84
        return $loaders;
99
    }
100
101
    /**
102
     * Makes namespace loaders from loader strings
103
     *
104
     * @param array $loaders The options being used for Vars
105
     * @param array      $default_loaders   The default loaders for Vars
106
     *
107
     * @throws \InvalidArgumentException If a loader from options isn't found
108
     *
109
     * @return array The namespace loaders
110
     */
111 84
    private function makeNameSpaceLoaders($loaders, $default_loaders)
112
    {
113 84
        $parsed_loaders = array();
114
115 84
        foreach ($loaders as $loader) {
116 84
            if (in_array($loader, $default_loaders)) {
117 81
                $loader = sprintf('%s\%sLoader', __NAMESPACE__, ucfirst(strtolower($loader)));
118
            }
119
120 84
            if (!class_exists($loader)) {
121 1
                throw new \InvalidArgumentException(sprintf("'%s' loader class does not exist", $loader));
122
            }
123
124 83
            $parsed_loaders[] = $loader;
125
        }
126
127 83
        return $parsed_loaders;
128
    }
129
130
    /**
131
     * Get and make extensions for loaders made from makeLoaders()
132
     *
133
     * @see \M1\Vars\Vars::makeLoaders() \M1\Vars\Vars::makeLoaders()
134
     *
135
     * @param  array $loaders File loaders
136
     *
137
     * @throws \RuntimeException If no loader extensions were found
138
     *
139
     * @return array File loader supported extensions
140
     */
141 83
    private function makeExtensions(array $loaders)
142
    {
143 83
        $extensions = array();
144
145 83
        foreach ($loaders as $loader) {
146 83
            $extensions = array_merge($extensions, $loader::$supported);
147
        }
148
149 83
        if (empty($extensions)) {
150 1
            throw new \RuntimeException('No loader extensions were found');
151
        }
152
153 82
        return $extensions;
154
    }
155
156
    /**
157
     * Get the Vars file loaders
158
     *
159
     * @return array The Vars file loaders
160
     */
161 75
    public function getLoaders()
162
    {
163 75
        return $this->loaders;
164
    }
165
166
    /**
167
     * Get the Vars file loaders extensions
168
     *
169
     * @return array The Vars file loader extensions
170
     */
171 7
    public function getExtensions()
172
    {
173 7
        return $this->extensions;
174
    }
175
}
176