DirectoryRunner   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 89
Duplicated Lines 7.87 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 4
dl 7
loc 89
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C run() 7 59 14
A canHandle() 0 4 1
A acceptRule() 0 4 1

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\Directory\DirectoryRuleInterface;
15
use Gabrieljmj\Should\Runner\Rule\RuleInterface;
16
use Gabrieljmj\Should\Ambient\AmbientInterface;
17
18
class DirectoryRunner extends AbstractRunner
19
{
20
    use \Gabrieljmj\Should\Tool\DirectoryValidatorTrait;
21
    use \Gabrieljmj\Should\Tool\ClassFileInfoTrait;
22
23
    /**
24
     * Runs the tests
25
     *
26
     * @param mixed $param
27
     */
28
    public function run($param)
29
    {
30
        $dir = $this->validateDir($param);
31
        $verifiedFiles = [];
32
        $files = [];
33
34
        if (count($this->rules) > 0) {
35
            foreach ($this->rules as $rule) {
36
                $verifiedFiles[] = $rule->execute($dir);
37
            }
38
39
            if (count($verifiedFiles) > 1) {
40
                $files = call_user_func_array('array_intersect', $verifiedFiles);
41
            } else {
42
                foreach ($verifiedFiles as $validFiles) {
43
                    $files = array_merge($validFiles, $files);
44
                }
45
            }
46
        } else {
47
            $path = realpath($dir);
48
            $entries = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST);
49
50 View Code Duplication
            foreach ($entries as $key => $name) {
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...
51
                $files[$key] = $name;
52
                $e = explode('\\', $name);
53
                if (end($e) === '.' || end($e) === '..' || is_dir($name)) {
54
                    unset($files[$key]);
55
                }
56
            }
57
        }
58
59
        $ambients = ['files' => [], 'classes' => []];
60
        $classesReports = [];
61
62
        foreach ($files as $file) {
63
            $require = require_once $file;
64
65
            if (!$require instanceof AmbientInterface) {
66
                $ambients['classes'][] = $this->getNamespace($file) . '\\' . $this->getClass($file);
67
            } else {
68
                $ambients['files'][] = $require;
69
            }
70
        }
71
72
        foreach ($ambients['files'] as $ambient) {
73
            $this->runTest($ambient);
74
        }
75
76
        $classRunner = new AmbientClassRunner();
77
78
        foreach ($ambients['classes'] as $ambient) {
79
            $classRunner->run($ambient);
80
            $classesReports[] = $classRunner->getReport();
81
        }
82
83
        foreach ($classesReports as $report) {
84
            $this->combineReport($report);
85
        }
86
    }
87
88
    /**
89
     * Verifies if runner can handle something
90
     *
91
     * @param mixed $param
92
     * @return mixed
93
     */
94
    public function canHandle($param)
95
    {
96
        return is_dir($param);
97
    }
98
99
    /**
100
     * @param
101
     */
102
    protected function acceptRule(RuleInterface $rule)
103
    {
104
        return $rule instanceof DirectoryRuleInterface;
105
    }
106
}