Completed
Push — master ( 976ce5...4a7070 )
by Christopher
06:24
created

Generator::inspectRoutes()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 4
nc 2
nop 0
crap 3
1
<?php
2
3
namespace Phwoolcon\TestStarter\TestCase;
4
5
use File_Iterator_Facade;
6
use PHPUnit\Util\Fileloader;
7
use Phwoolcon\Controller;
8
use Phwoolcon\TestStarter\Exception\TestCaseGenerator\TestsExistingException;
9
use Phwoolcon\TestStarter\TestCase\Generator\RoutesInspector;
10
use Phwoolcon\Text;
11
use ReflectionClass;
12
13
class Generator
14
{
15
    protected $srcDir;
16
    protected $outputDir;
17
    protected $routesFile;
18
    protected $candidates;
19
20 3
    public function __construct($srcDir, $outputDir, $routesFile = '')
21
    {
22 3
        $this->srcDir = realpath($srcDir) . DIRECTORY_SEPARATOR;
23 3
        $this->outputDir = $outputDir;
24 3
        $this->routesFile = $routesFile;
25 3
        $outputFinder = new File_Iterator_Facade();
26 3
        if ($outputFinder->getFilesAsArray([
27 3
            $this->outputDir . '/Unit',
28 3
            $this->outputDir . '/Integration',
29 3
        ], '.php')) {
30 1
            throw new TestsExistingException();
31
        }
32 2
    }
33
34 1
    public function generate()
35
    {
36 1
        $candidates = $this->detectCandidates();
0 ignored issues
show
Unused Code introduced by
$candidates is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
37 1
        return $this;
38
    }
39
40 2
    public function detectCandidates()
41
    {
42 2
        if ($this->candidates !== null) {
43 1
            return $this->candidates;
44
        }
45 2
        $this->candidates = [];
46
47 2
        $finder = new File_Iterator_Facade();
48 2
        $files = $finder->getFilesAsArray($this->srcDir, '.php');
49 2
        foreach ($files as $filename) {
50 2
            $this->inspectClassFile($filename);
51
        }
52
53 2
        $this->inspectRoutes();
54
55 2
        return $this->candidates;
56
    }
57
58 2
    protected function inspectClassFile($filename)
59
    {
60 2
        $classes = get_declared_classes();
61 2
        Fileloader::checkAndLoad($filename);
62 2
        $newClasses = array_diff(get_declared_classes(), $classes);
63 2
        $magicMethods = array_flip([
64 2
            '__construct',
65
            '__destruct',
66
            '__call',
67
            '__callStatic',
68
            '__get',
69
            '__set',
70
            '__isset',
71
            '__unset',
72
            '__sleep',
73
            '__wakeup',
74
            '__toString',
75
            '__invoke',
76
            '__set_state',
77
            '__clone',
78
            '__debugInfo',
79
        ]);
80 2
        foreach ($newClasses as $newClass) {
81 1
            $reflection = new ReflectionClass($newClass);
82 1
            if (!Text::startsWith($reflection->getFileName(), $this->srcDir, false)) {
83 1
                continue;
84
            }
85
            // Skip controller classes, because they will be tested via route
86 1
            if ($reflection->isSubclassOf(Controller::class)) {
87 1
                continue;
88
            }
89 1
            $methods = [];
90 1
            foreach ($reflection->getMethods() as $method) {
91 1
                if (isset($magicMethods[$method->getName()])) {
92 1
                    continue;
93
                }
94 1
                if (!$method->isPublic()) {
95 1
                    continue;
96
                }
97 1
                if ($method->getDeclaringClass()->getName() !== $newClass) {
98 1
                    continue;
99
                }
100 1
                $methods[] = $method->getName();
101
            }
102 1
            $this->candidates['unit'][$newClass] = $methods;
103
        }
104 2
    }
105
106 2
    protected function inspectRoutes()
107
    {
108 2
        if ($this->routesFile && is_file($this->routesFile)) {
109 2
            $inspector = new RoutesInspector($this->routesFile);
110 2
            $this->candidates['routes'] = $inspector->inspect();
111
        }
112 2
    }
113
}
114