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 ( f14b80...cf5ad3 )
by Loick
8s
created

ConfigBuilder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.88%

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 16
c 3
b 0
f 3
lcom 1
cbo 2
dl 0
loc 120
ccs 62
cts 64
cp 0.9688
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
C build() 0 65 12
A getWarnings() 0 4 1
A reset() 0 4 1
A createWarningFromInvalidValue() 0 15 2
1
<?php
2
3
/*
4
 * This file is part of the composer-changelogs project.
5
 *
6
 * (c) Loïck Piera <[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 Pyrech\ComposerChangelogs\Config;
13
14
use Pyrech\ComposerChangelogs\Util\FileSystemHelper;
15
16
class ConfigBuilder
17
{
18
    private static $validCommitAutoValues = [
19
        'never',
20
        'ask',
21
        'always',
22
    ];
23
24
    /** @var string[] */
25
    private $warnings = [];
26
27
    /**
28
     * @param array  $extra
29
     * @param string $baseDir
30
     *
31
     * @return Config
32
     */
33 26
    public function build(array $extra, $baseDir)
34
    {
35 26
        $this->reset();
36
37 26
        $commitAuto = 'never';
38 26
        $commitBinFile = null;
39 26
        $commitMessage = 'Update dependencies';
40 26
        $gitlabHosts = [];
41
42 26
        if (array_key_exists('commit-auto', $extra)) {
43 16
            if (in_array($extra['commit-auto'], self::$validCommitAutoValues, true)) {
44 14
                $commitAuto = $extra['commit-auto'];
45 14
            } else {
46 2
                $this->warnings[] = self::createWarningFromInvalidValue(
47 2
                    $extra,
48 2
                    'commit-auto',
49 2
                    $commitAuto,
50 2
                    sprintf('Valid options are "%s".', implode('", "', self::$validCommitAutoValues))
51 2
                );
52
            }
53 16
        }
54
55 26
        if (array_key_exists('commit-bin-file', $extra)) {
56 12
            if ($commitAuto === 'never') {
57 2
                $this->warnings[] = '"commit-bin-file" is specified but "commit-auto" option is set to "never". Ignoring.';
58 2
            } else {
59 10
                $file = realpath(
60 10
                    FileSystemHelper::isAbsolute($extra['commit-bin-file'])
61 10
                    ? $extra['commit-bin-file']
62 10
                    : $baseDir . '/' . $extra['commit-bin-file']
63 10
                );
64
65 10
                if (!file_exists($file)) {
66 2
                    $this->warnings[] = 'The file pointed by the option "commit-bin-file" was not found. Ignoring.';
67 2
                } else {
68 8
                    $commitBinFile = $file;
69
                }
70
            }
71 12
        } else {
72 14
            if ($commitAuto !== 'never') {
73 2
                $this->warnings[] = sprintf(
74 2
                    '"commit-auto" is set to "%s" but "commit-bin-file" was not specified.',
75
                    $commitAuto
76 2
                );
77 2
            }
78
        }
79
80 26
        if (array_key_exists('commit-message', $extra)) {
81 2
            if (strlen(trim($extra['commit-message'])) === 0) {
82
                $this->warnings[] = '"commit-message" is specified but empty. Ignoring and using default commit message.';
83
            } else {
84 2
                $commitMessage = $extra['commit-message'];
85
            }
86 2
        }
87
88 26
        if (array_key_exists('gitlab-hosts', $extra)) {
89 4
            if (!is_array($extra['gitlab-hosts'])) {
90 2
                $this->warnings[] = '"gitlab-hosts" is specified but should be an array. Ignoring.';
91 2
            } else {
92 2
                $gitlabHosts = (array) $extra['gitlab-hosts'];
93
            }
94 4
        }
95
96 26
        return new Config($commitAuto, $commitBinFile, $commitMessage, $gitlabHosts);
97
    }
98
99
    /**
100
     * @return string[]
101
     */
102 26
    public function getWarnings()
103
    {
104 26
        return $this->warnings;
105
    }
106
107 26
    private function reset()
108
    {
109 26
        $this->warnings = [];
110 26
    }
111
112
    /**
113
     * @param array  $extra
114
     * @param string $key
115
     * @param mixed  $default
116
     * @param string $additionalMessage
117
     *
118
     * @return string
119
     */
120 2
    private static function createWarningFromInvalidValue(array $extra, $key, $default, $additionalMessage = '')
121
    {
122 2
        $warning = sprintf(
123 2
            'Invalid value "%s" for option "%s", defaulting to "%s".',
124 2
            $extra[$key],
125 2
            $key,
126
            $default
127 2
        );
128
129 2
        if ($additionalMessage) {
130 2
            $warning .= ' ' . $additionalMessage;
131 2
        }
132
133 2
        return $warning;
134
    }
135
}
136