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 Amarkal\UI; |
||
4 | |||
5 | class Renderer |
||
6 | { |
||
7 | /** |
||
8 | * @var Singleton The reference to *Singleton* instance of this class |
||
9 | */ |
||
10 | private static $instance; |
||
11 | |||
12 | private $registered_components = array(); |
||
13 | |||
14 | /** |
||
15 | * Returns the *Singleton* instance of this class. |
||
16 | * |
||
17 | * @return Singleton The *Singleton* instance. |
||
18 | */ |
||
19 | public static function get_instance() |
||
20 | { |
||
21 | if( null === static::$instance ) |
||
0 ignored issues
–
show
|
|||
22 | { |
||
23 | static::$instance = new static(); |
||
0 ignored issues
–
show
Since
$instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self , or increasing the visibility of $instance to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
![]() It seems like
new static() of type this<Amarkal\UI\Renderer> is incompatible with the declared type object<Amarkal\UI\Singleton> of property $instance .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. ![]() |
|||
24 | } |
||
25 | return static::$instance; |
||
0 ignored issues
–
show
Since
$instance is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self , or increasing the visibility of $instance to at least protected.
Let’s assume you have a class which uses late-static binding: class YourClass
{
private static $someVariable;
public static function getSomeVariable()
{
return static::$someVariable;
}
}
The code above will run fine in your PHP runtime. However, if you now create a
sub-class and call the class YourSubClass extends YourClass { }
YourSubClass::getSomeVariable(); // Will cause an access error.
In the case above, it makes sense to update class SomeClass
{
private static $someVariable;
public static function getSomeVariable()
{
return self::$someVariable; // self works fine with private.
}
}
![]() |
|||
26 | } |
||
27 | |||
28 | public function render_component( $type, array $props = array() ) |
||
29 | { |
||
30 | $component = $this->create_component( $type, $props ); |
||
0 ignored issues
–
show
$props is of type array , but the function expects a object<Amarkal\UI\type> .
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
![]() |
|||
31 | return $component->render(); |
||
32 | } |
||
33 | |||
34 | |||
35 | /** |
||
36 | * Register a custom component class. If the given component name is similar |
||
37 | * to the name of one of the core components, it will override it. If a |
||
38 | * custom component with a similar name has already been registered, an |
||
39 | * exception will be thrown. |
||
40 | * |
||
41 | * @param string $type |
||
42 | * @param string $class_name |
||
43 | * @throws \RuntimeException |
||
44 | */ |
||
45 | View Code Duplication | public function register_component( $type, $class_name ) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
46 | { |
||
47 | if( !in_array($type, $this->registered_components) ) |
||
48 | { |
||
49 | $this->registered_components[$type] = $class_name; |
||
50 | } |
||
51 | else throw new \RuntimeException("A component of type '$type' has already been registered."); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * @param type $type |
||
57 | * @param type $props |
||
58 | * @return type |
||
59 | */ |
||
60 | private function create_component( $type, $props ) |
||
61 | { |
||
62 | try { |
||
63 | $component = $this->create_registered_component ($type, $props ); |
||
64 | } catch (\RuntimeException $ex) { |
||
65 | $component = $this->create_core_component ($type, $props ); |
||
66 | } |
||
67 | |||
68 | return $component; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | * @param type $type |
||
74 | * @param type $props |
||
75 | * @throws \RuntimeException |
||
76 | */ |
||
77 | private function create_core_component( $type, $props ) |
||
78 | { |
||
79 | $file_name = __DIR__."/components/$type/controller.php"; |
||
80 | $class_name = 'Amarkal\\UI\\Component_'.$type; |
||
81 | |||
82 | // Load one of the core components |
||
83 | if(!class_exists($class_name)) |
||
84 | { |
||
85 | if( file_exists( $file_name ) ) |
||
86 | { |
||
87 | require_once $file_name; |
||
88 | } |
||
89 | else |
||
90 | { |
||
91 | throw new \RuntimeException("A component of type '$type' does not exist."); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | return new $class_name($props); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * |
||
100 | * @param type $type |
||
101 | * @param type $props |
||
102 | * @throws \RuntimeException |
||
103 | */ |
||
104 | View Code Duplication | private function create_registered_component( $type, $props ) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
105 | { |
||
106 | if(in_array($type, $this->registered_components)) |
||
107 | { |
||
108 | $class_name = $this->registered_components[$type]; |
||
109 | return new $class_name($props); |
||
110 | } |
||
111 | |||
112 | throw new \RuntimeException("A component of type '$type' has not been registered."); |
||
113 | } |
||
114 | } |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: