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
Branch feature/normalise_config_files... (93a67d)
by Marc
03:11
created

YamChapiConfigLoader::validate()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 29
ccs 0
cts 13
cp 0
rs 6.7272
c 1
b 0
f 0
cc 7
eloc 17
nc 6
nop 2
crap 56
1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2017-03-20
7
 *
8
 */
9
10
namespace Chapi\Component\DependencyInjection\Loader;
11
12
13
use Doctrine\Instantiator\Exception\InvalidArgumentException;
14
use Symfony\Component\Config\Resource\FileResource;
15
use Symfony\Component\Yaml\Exception\ParseException;
16
use Symfony\Component\Yaml\Exception\RuntimeException;
17
use Symfony\Component\Yaml\Parser as YamlParser;
18
use Symfony\Component\Yaml\Yaml;
19
20
class YamChapiConfigLoader extends \Symfony\Component\DependencyInjection\Loader\YamlFileLoader
21
{
22
    /**
23
     * @var YamlParser
24
     */
25
    private $yamlParser;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function loadProfileParameters($sResource, $sProfile)
31
    {
32
        $path = $this->locator->locate($sResource);
33
34
        $content = $this->loadFile($path);
35
36
        $this->container->addResource(new FileResource($path));
0 ignored issues
show
Bug introduced by
It seems like $path defined by $this->locator->locate($sResource) on line 32 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...
37
38
        // empty file
39
        if (null === $content) {
40
            return;
41
        }
42
43
        // parameters
44
        if (isset($content['profiles']) && isset($content['profiles'][$sProfile]) && isset($content['profiles'][$sProfile]['parameters'])) {
45
            if (!is_array($content['profiles'][$sProfile]['parameters'])) {
46
                throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $sResource));
47
            }
48
49
            foreach ($content['profiles'][$sProfile]['parameters'] as $key => $value) {
50
                $this->container->setParameter($key, $value);
51
            }
52
        }
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    protected function loadFile($file)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
59
    {
60
        if (!class_exists('Symfony\Component\Yaml\Parser')) {
61
            throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.');
62
        }
63
64
        if (!stream_is_local($file)) {
65
            throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
66
        }
67
68
        if (!file_exists($file)) {
69
            throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
70
        }
71
72
        if (null === $this->yamlParser) {
73
            $this->yamlParser = new YamlParser();
74
        }
75
76
        try {
77
            $configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT);
78
        } catch (ParseException $e) {
79
            throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
80
        }
81
82
        return $this->validate($configuration, $file);
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    private function validate($content, $file)
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
89
    {
90
        if (null === $content) {
91
            return $content;
92
        }
93
94
        if (!is_array($content)) {
95
            throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file));
96
        }
97
98
        foreach ($content as $namespace => $data) {
99
            if (in_array($namespace, array('profiles'))) {
100
                continue;
101
            }
102
103
            if (!$this->container->hasExtension($namespace)) {
104
                $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
105
                throw new InvalidArgumentException(sprintf(
106
                    'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
107
                    $namespace,
108
                    $file,
109
                    $namespace,
110
                    $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'
111
                ));
112
            }
113
        }
114
115
        return $content;
116
    }
117
}