This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.