DirectoryRunner::run()   C
last analyzed

Complexity

Conditions 14
Paths 144

Size

Total Lines 59

Duplication

Lines 7
Ratio 11.86 %

Importance

Changes 0
Metric Value
dl 7
loc 59
rs 5.9
c 0
b 0
f 0
cc 14
nc 144
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}