ImportsParser::parse()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 5
Ratio 20 %

Code Coverage

Tests 13
CRAP Score 6.2373

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 5
loc 25
ccs 13
cts 16
cp 0.8125
rs 8.439
c 1
b 0
f 1
cc 6
eloc 13
nc 5
nop 3
crap 6.2373
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\Parser\Yaml;
13
14
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder;
15
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException;
16
use Fidry\LaravelYaml\FileLoader\Parser\ParserInterface;
17
18
/**
19
 * @author Théo FIDRY <[email protected]>
20
 */
21
class ImportsParser implements ParserInterface
22
{
23
    /**
24
     * Parses import statements and return resources.
25
     *
26
     * @param ContainerBuilder $containerBuilder
27
     * @param array            $content  YAML file content
28
     * @param string           $fileName file name
29
     *
30
     * @return array
31
     * @throws InvalidArgumentException
32
     */
33 20
    public function parse(ContainerBuilder $containerBuilder, $content, $fileName)
34
    {
35 20
        if (false === isset($content['imports'])) {
36 10
            return [];
37
        }
38
39 10 View Code Duplication
        if (false === is_array($content['imports'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40 4
            throw new InvalidArgumentException(
41 4
                sprintf('The "imports" key should contain an array in %s. Check your YAML syntax.', $fileName)
42 2
            );
43
        }
44
45 6
        $imports = [];
46 6
        foreach ($content['imports'] as $importStatement) {
47 4
            if (false === isset($importStatement['resource']) || false === is_string($importStatement['resource'])) {
48
                throw new InvalidArgumentException(
49
                    sprintf('The "imports" objects should contain a "resource" key in %s. Check your YAML syntax.', $fileName)
50
                );
51
            }
52
53 4
            $imports[$importStatement['resource']] = true;
54 3
        }
55
56 6
        return array_keys($imports);
57
    }
58
}
59