Completed
Push — master ( ad7fd3...b10ab8 )
by Théo
03:40
created

YamlFileLoader::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the LaravelYaml package.
5
 *
6
 * (c) Théo FIDRY <[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 Fidry\LaravelYaml\FileLoader\Yaml;
13
14
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder;
15
use Fidry\LaravelYaml\Exception\FileLoader\Exception;
16
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException;
17
use Fidry\LaravelYaml\FileLoader\FileLoaderInterface;
18
use Fidry\LaravelYaml\FileLoader\Parser\ParserInterface;
19
use Fidry\LaravelYaml\FileLoader\Parser\Yaml\DefinitionsParser;
20
use Fidry\LaravelYaml\FileLoader\Parser\Yaml\ParametersParser;
21
use Symfony\Component\Config\FileLocatorInterface;
22
use Symfony\Component\Yaml\Exception\ParseException;
23
use Symfony\Component\Yaml\Parser as YamlParser;
24
25
/**
26
 * This loader is able to load YAML files. Parsed values are interpreted and added to the {@see ContainerBuilder} to be
27
 * loaded to the Application later on.
28
 *
29
 * @author Théo FIDRY <[email protected]>
30
 */
31
final class YamlFileLoader implements FileLoaderInterface
32
{
33
    /**
34
     * @var ContainerBuilder
35
     */
36
    private $container;
37
38
    /**
39
     * @var FileLocatorInterface
40
     */
41
    private $fileLocator;
42
43
    /**
44
     * @var DefinitionsParser
45
     */
46
    private $definitionsParser;
47
48
    /**
49
     * @var ParametersParser
50
     */
51
    private $parametersParser;
52
53
    /**
54
     * @var YamlParser
55
     */
56
    private $yamlParser;
57
58
    /**
59
     * @var YamlValidator
60
     */
61
    private $yamlValidator;
62
63
    public function __construct(
64
        ContainerBuilder $container,
65
        FileLocatorInterface $fileLocator,
66
        ParserInterface $definitionsParser = null,
67
        ParserInterface $parametersParser = null,
68
        YamlParser $yamlParser = null,
69
        YamlValidator $yamlValidator = null
70
    ) {
71
        $this->container = $container;
72
        $this->fileLocator = $fileLocator;
73
74
        $this->definitionsParser = (null === $definitionsParser) ? new DefinitionsParser() : $definitionsParser;
0 ignored issues
show
Documentation Bug introduced by
It seems like null === $definitionsPar...() : $definitionsParser can also be of type object<Fidry\LaravelYaml...Parser\ParserInterface>. However, the property $definitionsParser is declared as type object<Fidry\LaravelYaml...Yaml\DefinitionsParser>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75
        $this->parametersParser = (null === $parametersParser) ? new ParametersParser() : $parametersParser;
0 ignored issues
show
Documentation Bug introduced by
null === $parametersPars...r() : $parametersParser is of type object<Fidry\LaravelYaml...Parser\ParserInterface>, but the property $parametersParser was declared to be of type object<Fidry\LaravelYaml...\Yaml\ParametersParser>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
76
        $this->yamlParser = (null === $yamlParser) ? new YamlParser() : $yamlParser;
77
        $this->yamlValidator = (null === $yamlValidator) ? new YamlValidator() : $yamlValidator;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     *
83
     * @param string $resource file name
84
     *
85
     * @example
86
     *  ::load('services.yml')
87
     *
88
     * @throws InvalidArgumentException
89
     * @throws Exception
90
     * @return $this
91
     */
92
    public function load($resource)
93
    {
94
        $path = $this->fileLocator->locate($resource);
95
        $content = $this->loadFile($path);
0 ignored issues
show
Bug introduced by
It seems like $path defined by $this->fileLocator->locate($resource) on line 94 can also be of type array; however, Fidry\LaravelYaml\FileLo...lFileLoader::loadFile() 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...
96
97
        $this->parametersParser->parse($this->container, $content, $resource);
98
        $this->definitionsParser->parse($this->container, $content, $resource);
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param string $filePath
105
     *
106
     * @return array The file content
107
     * @throws InvalidArgumentException
108
     */
109
    private function loadFile($filePath)
110
    {
111
        if (false === stream_is_local($filePath)) {
112
            throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $filePath));
113
        }
114
115
        if (false === file_exists($filePath)) {
116
            throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $filePath));
117
        }
118
119
        try {
120
            $configuration = $this->yamlParser->parse(file_get_contents($filePath));
121
        } catch (ParseException $exception) {
122
            throw new InvalidArgumentException(
123
                sprintf('The file "%s" does not contain valid YAML.', $filePath), 0, $exception
124
            );
125
        }
126
127
        return $this->yamlValidator->validate($configuration, $filePath);
128
    }
129
}
130