Passed
Branchmaster (fdc5d2)
by Thomas
03:17
created

Instantiate   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 90%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 15
c 3
b 1
f 1
lcom 0
cbo 0
dl 0
loc 82
ccs 27
cts 30
cp 0.9
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C make() 0 50 12
A instantiate() 0 12 3
1
<?php
2
/**
3
 * Part of twLib
4
 * http://www.thomaswhiston.com
5
 * [email protected]
6
 * Created by PhpStorm.
7
 * User: Thomas Whiston
8
 * Date: 27/01/2016
9
 * Time: 14:14
10
 */
11
namespace twhiston\twLib\Object;
12
13
    /**
14
     * Class Instantiate
15
     * Create a class
16
     * @package twhiston\twLib\Object
17
     */
18
/**
19
 * Class Instantiate
20
 * @package twhiston\twLib\Object
21
 */
22
class Instantiate
23
{
24
25
26
    /**
27
     * @param $class
28
     * @param $args
29
     * @param $namespace string psr-4 style 'whatever\\thing\\another\\'
30
     * @param null $interface test that class implements this interface
31
     * @return object
32
     * @throws \Exception
33
     */
34 9
    public static function make($class, $args, $namespace = null, $interface = null)
35
    {
36
37
//        $count = substr_count($class, '\\');
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
38
//        if ($count = 0 || !(0 === strpos($class, 'Drupal'))) {
39
//            //if there is no '/' we assume this is not a fully qualified namespace and make it our root Constraint namespace
40
//            //If there is no drupal at the start then we assume its our class further down the tree
41
//            $class = $namespace.$class;
42
//        }
43
44 9
        if ($namespace !== null) {
45 7
            $class = $namespace . $class;
46 7
        }
47
48
        try {
49
50 9
            if (!class_exists($class, true)) {
51 5
                throw new \Exception('Class does not exist');
52
            }
53
54 4
            if ($interface !== null) {
55
56 4
                $imp = class_implements($class, true);
57 4
                if (!is_array($imp)) {
58
                    throw new \Exception('Does not implement an interface');
59
                }
60 4
                if (!in_array($interface, $imp)) {
61 2
                    throw new \Exception('Does not implement requested interface');
62
                }
63
64 2
            }
65
            //Make sure args are an array
66 2
            if ($args !== null && $args !== false) {
67 2
                $args = (!is_array($args)) ? array($args) : $args;
68 2
                if (is_array($args)) {
69 2
                    $c = count(array_filter(array_keys($args), 'is_string'));
0 ignored issues
show
Unused Code introduced by
$c 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...
70 2
                    if (count(array_filter(array_keys($args), 'is_string')) > 0) {
71
                        $args = [$args];
72
                    }
73 2
                }
74 2
            }
75
76 2
            $instance = Instantiate::instantiate($class, $args);
77
78 9
        } catch (\Exception $e) {
79 7
            throw $e;
80
        }
81
82 2
        return $instance;
83
    }
84
85
    /**
86
     * @param $class
87
     * @param $args
88
     * @return object
89
     */
90 2
    private static function instantiate($class, $args)
91
    {
92
//BOOOOO, this produces a parse error on less than 5.6.0
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
//        if (version_compare(phpversion(), '5.6.0') !== -1) {
94
//            $instance = new $class(...$args);
95
//        } else {
96 2
        $reflect = new \ReflectionClass($class);
97 2
        $instance = ($args === null || $args === false) ? $reflect->newInstanceArgs() : $reflect->newInstanceArgs($args);
98
//        }
99
100 2
        return $instance;
101
    }
102
103
}