JsonRunner   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 92
Duplicated Lines 8.7 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 8
loc 92
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
B run() 0 31 10
A canHandle() 0 4 2
A acceptRule() 0 4 1
A createReportFromAmbients() 8 16 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
 * ShouldPHP
4
 *
5
 * @author  Gabriel Jacinto <[email protected]>
6
 * @status  dev
7
 * @link    https://github.com/GabrielJMJ/ShouldPHP
8
 * @license MIT
9
 */
10
 
11
namespace Gabrieljmj\Should\Runner;
12
13
use Gabrieljmj\Should\Runner\AbstractRunner;
14
use Gabrieljmj\Should\Runner\Rule\RuleInterface;
15
use Gabrieljmj\Should\Report\Report;
16
use Gabrieljmj\Should\Runner\Rule\Json\JsonRuleInterface;
17
18
class JsonRunner extends AbstractRunner
19
{
20
    private $runners;
21
22
    public function __construct(array $runners)
23
    {
24
        parent::__construct();
25
26
        foreach ($runners as $runner) {
27
            if (!$runner instanceof RunnerInterface) {
28
                throw new \InvalidArgumentException('A runner passed is not instance of RunnerInterface');
29
            }
30
        }
31
32
        $this->runners = $runners;
33
    }
34
35
    /**
36
     * Runs the tests
37
     *
38
     * @param mixed $param
39
     */
40
    public function run($param)
41
    {
42
        $content = file_get_contents($param);
43
        $json = json_decode($content);
44
        $ruleNamespace = '\Gabrieljmj\Should\Runner\Rule';
45
46
        if (isset($json->ambients)) {
47
            $ambients = $json->ambients;
48
            if (isset($json->rules)) {
49
                $rules = $json->rules;
50
51
                foreach ($rules as $category => $categoryRules) {
52
                    foreach ((array) $categoryRules as $key => $value) {
53
                        $class = is_string($key) && is_array($value) 
54
                            ? $ruleNamespace . '\\' . ucfirst($category) . '\\' . ucfirst($key) 
55
                            : $ruleNamespace . '\\' . ucfirst($category) . '\\' . ucfirst($value);
56
57
                        $ref = new \ReflectionClass($class);
58
                        $ruleI = is_string($key) && is_array($value) ? $ref->newInstanceArgs($value) : $ref->newInstance();
59
60
                        foreach ($this->runners as $key => $runner) {
61
                            $runner->pushRule($ruleI);
62
                            $this->runners[$key] = $runner;
63
                        }
64
                    }
65
                }
66
            }
67
68
            $this->createReportFromAmbients($ambients);
69
        }
70
    }
71
72
    /**
73
     * Verifies if runner can handle something
74
     *
75
     * @param mixed $param
76
     * @return mixed
77
     */
78
    public function canHandle($param)
79
    {
80
        return file_exists($param) && substr($param, -5) === '.json';
81
    }
82
83
    protected function acceptRule(RuleInterface $rule)
84
    {
85
        return $rule instanceof JsonRuleInterface;
86
    }
87
88
    /**
89
     * Creates the report from all executed ambients
90
     *
91
     * @param array $ambients
92
     */
93
    private function createReportFromAmbients(array $ambients){
94
        $reports = [];
95
96 View Code Duplication
        foreach ($ambients as $ambient) {
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...
97
            foreach ($this->runners as $runner) {
98
                if ($runner->canHandle($ambient)) {
99
                    $runner->run($ambient);
100
                    $reports[] = $runner->getReport();
101
                }
102
            }
103
        }
104
105
        foreach ($reports as $report) {
106
            $this->combineReport($report);
107
        }
108
    }
109
}