Test Failed
Push — master ( a58c94...1bcec0 )
by Brent
03:26
created

YamlParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 15.79 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 6
loc 38
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 5

1 Method

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

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
namespace Brendt\Stitcher\Parser;
4
5
use Brendt\Stitcher\Exception\ParserException;
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\Yaml\Exception\ParseException;
8
use Symfony\Component\Yaml\Yaml;
9
use Brendt\Stitcher\Config;
10
11
/**
12
 * The YamlParser take a path to one or more YAML files, and parses the content into an array.
13
 *
14
 * @see \Brendt\Stitcher\Parser\AbstractArrayParser::parseArrayData()
15
 */
16
class YamlParser extends AbstractArrayParser {
17
18
    /**
19
     * @param string $path
20
     *
21
     * @return mixed
22
     * @throws ParserException
23
     */
24
    public function parse($path = '*.yml') {
25
        if (!strpos($path, '.yml')) {
26
            $path .= '.yml';
27
        }
28
29
        $root = Config::get('directories.src');
30
        $files = Finder::create()->files()->in($root)->path($path);
31
        $yamlData = [];
32
33
        foreach ($files as $file) {
34
            try {
35
                $parsed = Yaml::parse($file->getContents());
36
37 View Code Duplication
                if (isset($parsed['entries'])) {
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...
38
                    $yamlData += $parsed['entries'];
39
                } else {
40
                    $id = str_replace(".{$file->getExtension()}", '', $file->getFilename());
41
                    $yamlData[$id] = $parsed;
42
                }
43
            } catch (ParseException $e) {
44
                throw new ParserException("{$file->getRelativePathname()}: {$e->getMessage()}");
45
            }
46
        }
47
48
        $parsedEntries = $this->parseArrayData($yamlData);
49
50
        return $parsedEntries;
51
    }
52
53
}
54