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

JsonParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 15.79 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 6 28 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
namespace Brendt\Stitcher\Parser;
4
5
use Brendt\Stitcher\Config;
6
use Brendt\Stitcher\Exception\ParserException;
7
use Symfony\Component\Finder\Finder;
8
9
/**
10
 * The JsonParser take a path to one or more JSON files, and parses the content into an array.
11
 */
12
class JsonParser extends AbstractArrayParser {
13
14
    /**
15
     * @param string $path
16
     *
17
     * @return array
18
     * @throws ParserException
19
     */
20
    public function parse($path = '*.json') {
21
        if (!strpos($path, '.json')) {
22
            $path .= '.json';
23
        }
24
25
        $data = [];
26
        $root = Config::get('directories.src');
27
        $files = Finder::create()->files()->in("{$root}")->path($path);
28
29
        foreach ($files as $file) {
30
            $parsed = json_decode($file->getContents(), true);
31
32
            if (json_last_error() > 0 && $error = json_last_error_msg()) {
33
                throw new ParserException("{$file->getRelativePathname()}: {$error}");
34
            }
35
36 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...
37
                $data += $parsed['entries'];
38
            } else {
39
                $id = str_replace(".{$file->getExtension()}", '', $file->getFilename());
40
                $data[$id] = $parsed;
41
            }
42
        }
43
44
        $data = $this->parseArrayData($data);
45
46
        return $data;
47
    }
48
49
}
50