ImportsParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 13.16 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 81.25%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
lcom 0
cbo 1
dl 5
loc 38
ccs 13
cts 16
cp 0.8125
rs 10
c 1
b 0
f 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 5 25 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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