Passed
Push — master ( c82b6f...43d2f1 )
by Brent
02:59
created

JsonProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 18.75 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 6
loc 32
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

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\provider;
4
5
use brendt\stitcher\exception\ProviderException;
6
use Symfony\Component\Finder\Finder;
7
8
class JsonProvider extends AbstractArrayProvider {
9
10
    public function parse($path = '*.json') {
11
        $finder = new Finder();
12
        $data = [];
13
        if (!strpos($path, '.json')) {
14
            $path .= '.json';
15
        }
16
17
        $files = $finder->files()->in("{$this->root}")->path($path);
18
19
        foreach ($files as $file) {
20
            $parsed = json_decode($file->getContents(), true);
21
22
            if (json_last_error() > 0 && $error = json_last_error_msg()) {
23
                throw new ProviderException("{$file->getRelativePathname()}: {$error}");
24
            }
25
26 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...
27
                $data += $parsed['entries'];
28
            } else {
29
                $id = str_replace(".{$file->getExtension()}", '', $file->getFilename());
30
                $data[$id] = $parsed;
31
            }
32
        }
33
34
        $data = $this->parseArrayData($data);
35
36
        return $data;
37
    }
38
39
}
40