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\Admin; |
||
| 4 | |||
| 5 | class Notifications |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var Singleton The reference to *Singleton* instance of this class |
||
| 9 | */ |
||
| 10 | private static $instance; |
||
| 11 | |||
| 12 | private $notifications = array(); |
||
| 13 | |||
| 14 | private $dismissed_notices; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Returns the *Singleton* instance of this class. |
||
| 18 | * |
||
| 19 | * @return Singleton The *Singleton* instance. |
||
| 20 | */ |
||
| 21 | public static function get_instance() |
||
| 22 | { |
||
| 23 | if( null === static::$instance ) |
||
|
0 ignored issues
–
show
|
|||
| 24 | { |
||
| 25 | 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.
}
}
Loading history...
It seems like
new static() of type this<Amarkal\Admin\Notifications> is incompatible with the declared type object<Amarkal\Admin\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.. Loading history...
|
|||
| 26 | } |
||
| 27 | 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.
}
}
Loading history...
The expression
static::$instance; of type Amarkal\Admin\Notificati...Amarkal\Admin\Singleton adds the type Amarkal\Admin\Notifications to the return on line 27 which is incompatible with the return type documented by Amarkal\Admin\Notifications::get_instance of type Amarkal\Admin\Singleton.
Loading history...
|
|||
| 28 | } |
||
| 29 | |||
| 30 | public function init() |
||
| 31 | { |
||
| 32 | add_action( 'admin_notices', array( $this, 'render_notifications' ) ); |
||
| 33 | add_action( 'network_admin_notices', array( $this, 'render_network_notifications' ) ); |
||
| 34 | add_action( 'wp_ajax_dismiss_admin_notification', array( $this, 'dismiss_notification' ) ); |
||
| 35 | add_action( 'admin_footer', array( $this, 'render_script' ) ); // Must be hooked to a late action hook |
||
| 36 | $this->dismissed_notices = get_site_option( 'amarkal_dismissed_notices', array() ); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function register_notification( $handle, $options ) |
||
| 40 | { |
||
| 41 | if( !key_exists( $handle, $this->notifications ) ) |
||
| 42 | { |
||
| 43 | $this->notifications[$handle] = $options; |
||
| 44 | } |
||
| 45 | else trigger_error( "The handle <strong>$handle</strong> has already been registered. Please choose a different handle for your notification." ); |
||
| 46 | } |
||
| 47 | |||
| 48 | public function render_notifications() |
||
| 49 | { |
||
| 50 | foreach($this->notifications as $handle => $notification) |
||
| 51 | { |
||
| 52 | $this->render_notification( $handle, $notification ); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | public function render_network_notifications() |
||
| 57 | { |
||
| 58 | foreach( $this->notifications as $handle => $notification ) |
||
| 59 | { |
||
| 60 | if( $notification['network'] ) $this->render_notification( $handle, $notification ); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | public function dismiss_notification() |
||
| 65 | { |
||
| 66 | $id = filter_input( INPUT_POST, 'id' ); |
||
| 67 | if( !in_array( $id, $this->dismissed_notices ) ) |
||
| 68 | { |
||
| 69 | $this->dismissed_notices[] = $id; |
||
| 70 | update_site_option( 'amarkal_dismissed_notices', $this->dismissed_notices); |
||
| 71 | } |
||
| 72 | wp_die(); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function render_script() |
||
| 76 | { |
||
| 77 | if( 0 === count( $this->notifications ) ) return; |
||
| 78 | ?> |
||
| 79 | <script> |
||
| 80 | jQuery(document).ready(function($){ |
||
| 81 | $('.notice').on('click','.notice-dismiss',function(e){ |
||
| 82 | $.post(ajaxurl,{ |
||
| 83 | action: 'dismiss_admin_notification', |
||
| 84 | id: $(this).parent().attr('id') |
||
| 85 | }); |
||
| 86 | }); |
||
| 87 | }); |
||
| 88 | </script> |
||
| 89 | <?php |
||
| 90 | } |
||
| 91 | |||
| 92 | private function render_notification( $id, $n ) |
||
| 93 | { |
||
| 94 | if( in_array( $id, $this->dismissed_notices ) ) return; |
||
| 95 | |||
| 96 | printf( |
||
| 97 | '<div id="%s" class="notice notice-%s %s%s"><p>%s</p></div>', |
||
| 98 | $id, |
||
| 99 | $n['type'], |
||
| 100 | $n['dismissible']?'is-dismissible ':'', |
||
| 101 | $n['class'], |
||
| 102 | $n['html'] |
||
| 103 | ); |
||
| 104 | } |
||
| 105 | } |
||
| 106 |
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
SomeClassto useselfinstead: