1
|
|
|
<?php |
2
|
|
|
namespace Samshal\Rando; |
3
|
|
|
|
4
|
|
|
class Rando |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
protected static $packagesDirectory = __DIR__ . '/packages'; |
8
|
|
|
protected static $commonNamespace = __NAMESPACE__.'\\Packages'; |
9
|
|
|
protected static $commonInterface = 'Samshal\\Rando\\Packages\\PackageableInterface'; |
10
|
|
|
|
11
|
|
|
public function __call($method, $parameters) |
12
|
|
|
{ |
13
|
|
|
return self::processInstruction($method, $parameters); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public static function __callStatic($method, $parameters) |
17
|
|
|
{ |
18
|
|
|
return self::processInstruction($method, $parameters); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private static function processInstruction($method, $parameters) |
22
|
|
|
{ |
23
|
|
|
$parameters = isset($parameters[0]) ? $parameters[0] : []; |
24
|
|
|
$class = self::instantiatePackage($method); |
25
|
|
|
return self::getRandoString($class, $parameters); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private static function instantiatePackage($packageName) |
29
|
|
|
{ |
30
|
|
|
$namespaceDirectories = glob(self::$packagesDirectory.'/*', GLOB_ONLYDIR); |
31
|
|
|
$packageName = ucfirst(strtolower($packageName)); |
32
|
|
|
foreach ($namespaceDirectories as $namespaceDirectory) { |
33
|
|
|
$baseName = basename($namespaceDirectory); |
34
|
|
|
$namespace = self::$commonNamespace.'\\'.ucfirst(strtolower($baseName)); |
35
|
|
|
$class = $namespace.'\\'.$packageName; |
36
|
|
|
if (class_exists($class)) { |
37
|
|
|
return new $class; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
throw new Exceptions\RequestedClassNotFoundException(); |
41
|
|
|
|
42
|
|
|
return; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private static function getRandoString($class, $parameters) |
46
|
|
|
{ |
47
|
|
|
$implementedInterfaces = class_implements($class); |
48
|
|
|
if (!in_array(self::$commonInterface, $implementedInterfaces)) { |
49
|
|
|
throw new Exceptions\InterfaceNotImplementedException(); |
50
|
|
|
} else { |
51
|
|
|
$class->initializeParameters($parameters); |
52
|
|
|
return $class->stringify(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.