|
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(); |
|
|
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.