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 ( 8854b7...a3174d )
by François
02:32
created

Reader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2016 François Kooman <[email protected]>.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace fkooman\RemoteStorage\Config;
20
21
use InvalidArgumentException;
22
use RuntimeException;
23
24
class Reader
25
{
26
    /** @var array */
27
    private $config;
28
29
    public function __construct(ReaderInterface $reader)
30
    {
31
        $this->config = $reader->readConfig();
32
    }
33
34
    public static function isRequired(array $argv)
35
    {
36
        foreach ($argv as $arg) {
37
            if (is_string($arg)) {
38
                continue;
39
            } elseif (is_bool($arg)) {
40
                return $arg;
41
            } else {
42
                throw new InvalidArgumentException('invalid argument type');
43
            }
44
        }
45
46
        return true;
47
    }
48
49
    public static function defaultValue(array $argv)
50
    {
51
        $argc = count($argv);
52
        for ($i = 1; $i < $argc; ++$i) {
53
            if (false === $argv[$i]) {
54
                // return next as default value
55
                if (array_key_exists($i + 1, $argv)) {
56
                    return $argv[$i + 1];
57
                }
58
59
                return;
60
            }
61
        }
62
    }
63
64
    public static function configValues(array $argv)
65
    {
66
        $configValues = [];
67
        foreach ($argv as $arg) {
68
            if (!is_string($arg)) {
69
                break;
70
            }
71
            $configValues[] = $arg;
72
        }
73
74
        return $configValues;
75
    }
76
77
    /**
78
     * Get the configuration as a nested array.
79
     *
80
     * @return array the configuration as a nested array
81
     */
82
    public function getConfig()
83
    {
84
        return $this->config;
85
    }
86
87
    public function v()
88
    {
89
        $argv = func_get_args();
90
        $argc = count($argv);
91
92
        // need at least one parameter
93
        if (0 === $argc) {
94
            throw new InvalidArgumentException('no configuration field requested');
95
        }
96
97
        // first parameter must be string
98
        if (!is_string($argv[0])) {
99
            throw new InvalidArgumentException('first argument must be string');
100
        }
101
102
        // if config key exists, return its value
103
        $configValues = self::configValues($argv);
104
        if (self::configExists($this->config, $configValues)) {
105
            return self::getValue($this->config, $configValues);
106
        }
107
108
        // if it is required and not available throw error
109
        if (self::isRequired($argv)) {
110
            throw new RuntimeException(
111
                sprintf('configuration value "%s" not found', implode(':', $configValues))
112
            );
113
        }
114
115
        // return the default value
116
        return self::defaultValue($argv);
117
    }
118
119
    private static function configExists(array $configPointer, array $argv)
120
    {
121
        foreach ($argv as $arg) {
122
            if (!is_array($configPointer)) {
123
                return false;
124
            }
125
            if (!array_key_exists($arg, $configPointer)) {
126
                return false;
127
            }
128
            $configPointer = $configPointer[$arg];
129
        }
130
131
        return true;
132
    }
133
134
    private static function getValue(array $configPointer, array $argv)
135
    {
136
        foreach ($argv as $arg) {
137
            $configPointer = $configPointer[$arg];
138
        }
139
140
        return $configPointer;
141
    }
142
}
143