Test Setup Failed
Push — master ( 0780c2...8b245e )
by Samuel
03:03
created

Rando::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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;
0 ignored issues
show
Unused Code introduced by
return; does not seem to be reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
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;
0 ignored issues
show
Unused Code introduced by
return; does not seem to be reachable.

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 or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

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.

Loading history...
56
    }
57
}
58