ExtensionCollection   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 1

Complexity

Total Complexity 19

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 139
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A collection() 0 4 1
A createCollection() 0 9 2
A convertToArray() 0 8 2
A validateExtension() 0 5 1
A validateClassExists() 0 6 3
A validateClassImplementation() 0 12 3
A getKey() 0 11 4
A getType() 0 8 2
1
<?php
2
3
namespace League\CLImate\TerminalObject\Router;
4
5
use League\CLImate\Util\Helper;
6
7
class ExtensionCollection
8
{
9
    /**
10
     * @var array collection
11
     */
12
    protected $collection = ['basic' => [], 'dynamic' => []];
13
14
    /**
15
     * @var string $basic_interface
16
     */
17
    protected $basic_interface = 'League\CLImate\TerminalObject\Basic\BasicTerminalObjectInterface';
18
19
    /**
20
     * @var string $dynamic_interface
21
     */
22
    protected $dynamic_interface = 'League\CLImate\TerminalObject\Dynamic\DynamicTerminalObjectInterface';
23
24 36
    public function __construct($key, $class)
25
    {
26 36
        $this->createCollection($key, $class);
27 28
    }
28
29 28
    public function collection()
30
    {
31 28
        return $this->collection;
32
    }
33
34
    /**
35
     * Create the collection from the key/class
36
     *
37
     * @param string $original_key
38
     * @param string|object|array $original_class
39
     *
40
     * @return type
41
     */
42 36
    protected function createCollection($original_key, $original_class)
43
    {
44 36
        $collection = $this->convertToArray($original_key, $original_class);
45
46 36
        foreach ($collection as $key => $class) {
47 36
            $this->validateExtension($class);
48 28
            $this->collection[$this->getType($class)][$this->getKey($key, $class)] = $class;
49 28
        }
50 28
    }
51
52
    /**
53
     * Convert the given class and key to an array of classes
54
     *
55
     * @param string|object|array $class
56
     * @param string $key Optional custom key instead of class name
57
     *
58
     * @return array
59
     */
60 36
    protected function convertToArray($key, $class)
61
    {
62 36
        if (is_array($class)) {
63 8
            return $class;
64
        }
65
66 28
        return [$this->getKey($key, $class) => $class];
67
    }
68
69
    /**
70
     * Ensure that the extension is valid
71
     *
72
     * @param string|object|array $class
73
     */
74 36
    protected function validateExtension($class)
75
    {
76 36
        $this->validateClassExists($class);
0 ignored issues
show
Bug introduced by Joe Tannenbaum
It seems like $class defined by parameter $class on line 74 can also be of type array; however, League\CLImate\TerminalO...::validateClassExists() does only seem to accept string|object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
77 32
        $this->validateClassImplementation($class);
0 ignored issues
show
Bug introduced by Joe Tannenbaum
It seems like $class defined by parameter $class on line 74 can also be of type array; however, League\CLImate\TerminalO...teClassImplementation() does only seem to accept string|object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
78 28
    }
79
80
    /**
81
     * @param string|object $class
82
     *
83
     * @throws \Exception if extension class does not exist
84
     */
85 36
    protected function validateClassExists($class)
86
    {
87 36
        if (is_string($class) && !class_exists($class)) {
88 4
            throw new \Exception('Class does not exist: ' . $class);
89
        }
90 32
    }
91
92
    /**
93
     * @param string|object $class
94
     *
95
     * @throws \Exception if extension class does not implement either Dynamic or Basic interface
96
     */
97 32
    protected function validateClassImplementation($class)
98
    {
99 32
        $str_class = is_string($class);
100
101 32
        $valid_implementation = (is_a($class, $this->basic_interface, $str_class)
102 32
                                    || is_a($class, $this->dynamic_interface, $str_class));
103
104 32
        if (!$valid_implementation) {
105 4
            throw new \Exception('Class must implement either '
106 4
                                    . $this->basic_interface . ' or ' . $this->dynamic_interface);
107
        }
108 28
    }
109
110
    /**
111
     * Determine the extension key based on the class
112
     *
113
     * @param string|null $key
114
     * @param string|object $class
115
     *
116
     * @return string
117
     */
118 36
    protected function getKey($key, $class)
119
    {
120 36
        if ($key === null || !is_string($key)) {
121 28
            $class_path = (is_string($class)) ? $class : get_class($class);
122
123 28
            $key = explode('\\', $class_path);
124 28
            $key = end($key);
125 28
        }
126
127 36
        return Helper::snakeCase($key);
128
    }
129
130
    /**
131
     * Get the type of class the extension implements
132
     *
133
     * @param string|object $class
134
     *
135
     * @return string 'basic' or 'dynamic'
136
     */
137 28
    protected function getType($class)
138
    {
139 28
        if (is_a($class, $this->basic_interface, is_string($class))) {
140 24
            return 'basic';
141
        }
142
143 12
        return 'dynamic';
144
    }
145
}
146