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.

getConfigurationByConfigFile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 8
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the Gerrie package.
4
 *
5
 * (c) Andreas Grunwald <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Gerrie\Component\Configuration;
12
13
use Gerrie\Component\Console\InputExtendedInterface;
14
use Symfony\Component\Yaml\Yaml;
15
16
class ConfigurationFactory
17
{
18
    /**
19
     * Creates a configuration based on a YAML configuration file.
20
     *
21
     * @param string $configFile
22
     * @return Configuration
23
     */
24 3
    public static function getConfigurationByConfigFile($configFile)
25
    {
26 3
        if (file_exists($configFile) === false) {
27 1
            $message = 'Configuration file "%s" not found or accessible.';
28 1
            $message = sprintf($message, $configFile);
29 1
            throw new \RuntimeException($message, 1415381521);
30
        }
31
32 2
        $config = Yaml::parse($configFile);
33 2
        $configuration = new Configuration($config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by \Symfony\Component\Yaml\Yaml::parse($configFile) on line 32 can also be of type string; however, Gerrie\Component\Configu...guration::__construct() does only seem to accept array, 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...
34
35 2
        return $configuration;
36
    }
37
38
    /**
39
     * Creates a configuration based on a YAML configuration file, command line options and command line arguments.
40
     * The command line options will be merged into the configuration.
41
     * The command line arguments will be merged into the configuration as well.
42
     *
43
     * The command line options got a higher priority as the configuration file.
44
     * The command line arguments will be added to the configuration file and not overwritten.
45
     *
46
     * @param string $configFile
47
     * @param InputExtendedInterface $input
48
     * @return Configuration
49
     */
50 4
    public static function getConfigurationByConfigFileAndCommandOptionsAndArguments($configFile, InputExtendedInterface $input)
51
    {
52 4
        if ($input->isOptionSet('config-file') === true) {
53 1
            $configuration = self::getConfigurationByConfigFile($configFile);
54 1
        } else {
55 3
            $configuration = new Configuration();
56
        }
57
58 4
        $configuration = self::mergeCommandOptionsIntoConfiguration($configuration, $input);
59 4
        $configuration = self::mergeCommandArgumentsIntoConfiguration($configuration, $input);
60
61 4
        return $configuration;
62
    }
63
64
    /**
65
     * Merges the command line options into a existing configuration.
66
     *
67
     * E.g.
68
     *  * Database credentials
69
     *  * SSH settings
70
     *
71
     * @param Configuration $config
72
     * @param InputExtendedInterface $input
73
     * @return Configuration
74
     */
75 4
    protected static function mergeCommandOptionsIntoConfiguration(Configuration $config, InputExtendedInterface $input)
76
    {
77
        $configurationMapping = [
78
            // Database credentials
79 4
            'database-host' => 'Database.Host',
80 4
            'database-user' => 'Database.Username',
81 4
            'database-pass' => 'Database.Password',
82 4
            'database-port' => 'Database.Port',
83 4
            'database-name' => 'Database.Name',
84
            // SSH settings
85 4
            'ssh-key'       => 'SSH.KeyFile',
86 4
        ];
87
88 4
        foreach ($configurationMapping as $optionName => $configName) {
89 4
            if ($input->isOptionSet($optionName) === true) {
90 4
                $config->setConfigurationValue($configName, $input->getOption($optionName));
91
92
            // If the value is not set / available, set this to nothing
93 4
            } elseif (!$config->getConfigurationValue($configName)) {
94 4
                $config->setConfigurationValue($configName, null);
95 4
            }
96 4
        }
97
98 4
        return $config;
99
    }
100
101
    /**
102
     * Merges the command line arguments into a existing configuration.
103
     *
104
     * E.g.
105
     *  * Instances
106
     *
107
     * @param Configuration $config
108
     * @param InputExtendedInterface $input
109
     * @return Configuration
110
     */
111 4
    protected static function mergeCommandArgumentsIntoConfiguration(Configuration $config, InputExtendedInterface $input)
112
    {
113 4
        if ($input->hasArgument('instances') === false) {
114 2
            return $config;
115
        }
116
117 2
        $argumentInstances = $input->getArgument('instances');
118
119 2
        if (count($argumentInstances) === 0) {
120 1
            return $config;
121
        }
122
123
        // Gerrie is a reserved keyword for project names
124 1
        $config->setConfigurationValue('Gerrit.Gerrie', $argumentInstances);
125
126 1
        return $config;
127
    }
128
}