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.

YamlFileLoader::load()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 11
nc 4
nop 2
1
<?php
2
3
namespace Junker\Silex;
4
5
use Symfony\Component\Yaml\Yaml;
6
use Symfony\Component\Config\Loader\FileLoader;
7
use Symfony\Component\Config\Resource\FileResource;
8
use Symfony\Component\Yaml\Exception\ParseException;
9
use Symfony\Component\Yaml\Parser as YamlParser;
10
11
class YamlFileLoader extends FileLoader
12
{
13
    private $yamlParser;
14
15
    public function load($file, $type = null)
16
    {
17
        $path = $this->locator->locate($file);
18
19
        $data = $this->loadFile($path);
20
21
        // empty file
22
        if ($data === null) {
23
            $data = array();
24
        }
25
26
        // not an array
27
        if (!is_array($data)) {
28
            throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $file));
29
        }
30
31
        $config = new Config($data);
32
        $config->addResource(new FileResource($path));
0 ignored issues
show
Bug introduced by
It seems like $path defined by $this->locator->locate($file) on line 17 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...
33
34
        // imports
35
        $this->parseImports($config, $path);
36
37
        return $config;
38
    }
39
40
    protected function loadFile($file)
41
    {
42
        if (!class_exists('Symfony\Component\Yaml\Parser')) {
43
            throw new \RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.');
44
        }
45
46
        if (!stream_is_local($file)) {
47
            throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
48
        }
49
50
        if (!file_exists($file)) {
51
            throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
52
        }
53
54
        if ($this->yamlParser === null) {
55
            $this->yamlParser = new YamlParser();
56
        }
57
58
        try {
59
            $data = $this->yamlParser->parse(file_get_contents($file));
60
        } catch (ParseException $e) {
61
            throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
62
        }
63
64
        return $data;
65
    }
66
67
    private function parseImports(Config &$config, $file)
68
    {
69
        if (!isset($config->data['imports'])) {
70
            return;
71
        }
72
        if (!is_array($config->data['imports'])) {
73
            throw new \InvalidArgumentException(sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $file));
74
        }
75
76
        $defaultDirectory = dirname($file);
77
78
        foreach ($config->data['imports'] as $import) {
79
            if (!is_array($import)) {
80
                throw new \InvalidArgumentException(sprintf('The values in the "imports" key should be arrays in %s. Check your YAML syntax.', $file));
81
            }
82
83
            $this->setCurrentDir($defaultDirectory);
84
            $sub_config = $this->import($import['resource'], null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false, $file);
85
86
            $config->addConfig($sub_config);
87
88
            unset($config->data['imports']);
89
        }
90
    }
91
92
    public function supports($resource, $type = null)
93
    {
94
        return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type);
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
95
    }
96
}
97