GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Module   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 5
dl 0
loc 191
c 0
b 0
f 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 14 3
A ensureRequires() 0 10 3
A registerProvides() 0 12 4
A registerProvide() 0 6 1
A has() 0 4 1
A provider() 0 14 2
A makeProvider() 0 6 2
A doesProvide() 0 4 1
A masterContext() 0 8 2
A getName() 0 10 2
A getNamespace() 0 10 2
1
<?php
2
3
namespace Saltwater\Salt;
4
5
use Saltwater\Server as S;
6
use Saltwater\Utils as U;
7
use Saltwater\Salt\Provider;
8
9
/**
10
 * Modules encapsulate salts
11
 *
12
 * @package Saltwater\Salt
13
 */
14
class Module
15
{
16
    /** @var string Can be used to override the automatically generated value */
17
    public static $name;
18
19
    /** @var string Can be used to override the automatically generated value */
20
    public static $namespace;
21
22
    /**
23
     * @var array Requirements that need to be in place for this module
24
     */
25
    protected $require = array();
26
27
    /**
28
     * @var array Salts that this module provides
29
     */
30
    protected $provide = array();
31
32
    /**
33
     * @var int bitmask of Salts passed to the registry
34
     */
35
    public $registry = 0;
36
37
    /**
38
     * @param string $name
39
     *
40
     * @return void
41
     */
42
    public function register($name)
43
    {
44
        $name = 'module.' . $name;
45
46
        if (S::$n->registry->exists($name) || $this->registry) {
47
            return;
48
        }
49
50
        $this->ensureRequires();
51
52
        $this->registry |= S::$n->registry->append($name);
53
54
        $this->registerProvides();
55
    }
56
57
    /**
58
     * Make sure all required modules are loaded
59
     *
60
     * @return void
61
     */
62
    private function ensureRequires()
63
    {
64
        if (empty($this->require['module'])) {
65
            return;
66
        }
67
68
        foreach ($this->require['module'] as $module) {
69
            S::$n->modules->append($module, true);
70
        }
71
    }
72
73
    /**
74
     * Register all salts the module provides
75
     *
76
     * @return void
77
     */
78
    private function registerProvides()
79
    {
80
        if (empty($this->provide)) {
81
            return;
82
        }
83
84
        foreach ($this->provide as $type => $content) {
85
            foreach ($content as $salt) {
86
                $this->registerProvide($type, $salt);
87
            }
88
        }
89
    }
90
91
    /**
92
     * Register a salt that this module provides
93
     *
94
     * @param $type
95
     * @param $salt
96
     *
97
     * @return void
98
     */
99
    private function registerProvide($type, $salt)
100
    {
101
        $this->registry |= S::$n->registry->append(
102
            $type . '.' . U::camelTodashed($salt)
103
        );
104
    }
105
106
    /**
107
     * Check whether the module contains a salt
108
     *
109
     * @param $bit
110
     *
111
     * @return bool
112
     */
113
    public function has($bit)
114
    {
115
        return ($this->registry & $bit) == $bit;
116
    }
117
118
    /**
119
     * Create a new provider instance
120
     *
121
     * @param string $type
122
     * @param string $caller
123
     *
124
     * @return Provider
125
     */
126
    public function provider($caller, $type)
127
    {
128
        $class = $this->makeProvider($type);
129
130
        if ($class === false) {
131
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Saltwater\Salt\Module::provider of type Saltwater\Salt\Provider.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
132
        }
133
134
        $class::setModule($this::getName());
135
136
        $class::setCaller($caller);
137
138
        return new $class;
139
    }
140
141
    /**
142
     * @param string $type
143
     *
144
     * @return false|Provider
145
     */
146
    private function makeProvider($type)
147
    {
148
        $class = U::className($this::getNamespace(), 'provider', $type);
149
150
        return class_exists($class) ? $class : false;
151
    }
152
153
    /**
154
     * Check whether this module provides a salt
155
     *
156
     * @return bool
157
     */
158
    public function doesProvide($salt)
159
    {
160
        return !empty($this->provide[$salt]);
161
    }
162
163
    /**
164
     * Return the master context for this module
165
     *
166
     * @return null|string
167
     */
168
    public function masterContext()
169
    {
170
        if (!$this->doesProvide('context')) {
171
            return null;
172
        }
173
174
        return U::camelTodashed($this->provide['context'][0]);
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public static function getName()
181
    {
182
        $class = get_called_class();
183
184
        if (!empty($class::$name)) {
185
            return $class::$name;
186
        }
187
188
        return U::namespacedClassToDashed($class);
189
    }
190
191
    /**
192
     * @return string
193
     */
194
    public static function getNamespace()
195
    {
196
        $class = get_called_class();
197
198
        if (!empty($class::$namespace)) {
199
            return $class::$namespace;
200
        }
201
202
        return U::namespaceFromClass($class);
203
    }
204
}
205