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... (5aeeaf)
by Marc
03:47
created

YamChapiConfigLoader::loadFile()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 26
ccs 0
cts 13
cp 0
rs 8.439
c 1
b 0
f 0
cc 6
eloc 14
nc 7
nop 1
crap 42
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
        $content = $this->loadProfileFileContent($sResource, $sProfile);
33
34
        // empty file
35
        if (empty($content)) {
36
            return;
37
        }
38
39
        // parameters
40
        if (isset($content['parameters'])) {
41
            if (!is_array($content['parameters'])) {
42
                throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $sResource));
43
            }
44
45
            foreach ($content['parameters'] as $key => $value) {
46
                $this->container->setParameter($key, $value);
47
            }
48
        }
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    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...
55
    {
56
        if (!class_exists('Symfony\Component\Yaml\Parser')) {
57
            throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.');
58
        }
59
60
        if (!stream_is_local($file)) {
61
            throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
62
        }
63
64
        if (!file_exists($file)) {
65
            throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
66
        }
67
68
        if (null === $this->yamlParser) {
69
            $this->yamlParser = new YamlParser();
70
        }
71
72
        try {
73
            $configuration = $this->yamlParser->parse(file_get_contents($file), Yaml::PARSE_CONSTANT);
74
        } catch (ParseException $e) {
75
            throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
76
        }
77
78
        return $this->validate($configuration, $file);
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    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...
85
    {
86
        if (null === $content) {
87
            return $content;
88
        }
89
90
        if (!is_array($content)) {
91
            throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file));
92
        }
93
94
        foreach ($content as $namespace => $data) {
95
            if (in_array($namespace, array('profiles'))) {
96
                continue;
97
            }
98
99
            if (!$this->container->hasExtension($namespace)) {
100
                $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...
101
                throw new InvalidArgumentException(sprintf(
102
                    'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
103
                    $namespace,
104
                    $file,
105
                    $namespace,
106
                    $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'
107
                ));
108
            }
109
        }
110
111
        return $content;
112
    }
113
114
    /**
115
     * @param string $sResource
116
     * @param string $sProfile
117
     * @return array
118
     */
119
    private function loadProfileFileContent($sResource, $sProfile)
120
    {
121
        $path = $this->locator->locate($sResource);
122
        $content = $this->loadFile($path);
123
124
        $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 121 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...
125
126
        // empty file
127
        if (null === $content) {
128
            return [];
129
        }
130
131
        // parameters
132
        if (isset($content['profiles']) && isset($content['profiles'][$sProfile])) {
133
            return $content['profiles'][$sProfile];
134
        }
135
136
        return [];
137
    }
138
}