Completed
Push — master ( 727b9d...4893ea )
by Askupa
02:03
created

Manager::add_subpage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Amarkal\Settings;
4
5
class Manager
6
{
7
    /**
8
     * @var Singleton The reference to *Singleton* instance of this class
9
     */
10
    private static $instance;
11
    
12
    private $pages = array();
13
    
14
    private $subpages = array();
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
Bug introduced by
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 getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
24
        {
25
            static::$instance = new static();
0 ignored issues
show
Bug introduced by
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 getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Documentation Bug introduced by
It seems like new static() of type this<Amarkal\Settings\Manager> is incompatible with the declared type object<Amarkal\Settings\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
Bug introduced by
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 getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Bug Compatibility introduced by
The expression static::$instance; of type Amarkal\Settings\Manager...rkal\Settings\Singleton adds the type Amarkal\Settings\Manager to the return on line 27 which is incompatible with the return type documented by Amarkal\Settings\Manager::get_instance of type Amarkal\Settings\Singleton.
Loading history...
28
    }
29
    
30
    public function add_page($args)
31
    {
32
        $slug = $args['slug'];
33
        if(array_key_exists($slug,$this->pages))
34
        {
35
            throw new \RuntimeException("A page with slug '$slug' has already been registered");
36
        }
37
        $this->pages[$slug] = new Page($args);
38
    }
39
    
40
    public function add_subpage($args)
41
    {
42
        $this->subpages[] = new SubPage($args);
43
    }
44
    
45
    public function register_settings()
46
    {
47
        register_setting( 'myoption-group', 'new_option_name' );
48
        register_setting( 'myoption-group', 'some_other_option' );
49
        register_setting( 'myoption-group', 'option_etc' );
50
    }
51
    
52
    /**
53
     * Register styles & scripts to be enqueued by settings subpages
54
     */
55
    public function register_scripts()
56
    {
57
        \wp_register_style('amarkal-settings',$this->get_url(__DIR__.'/assets/css/style.min.css'));
58
        \wp_register_script('amarkal-settings',$this->get_url(__DIR__.'/assets/js/script.min.js'));
59
    }
60
    
61
    public function save_settings()
62
    {
63
        echo 'save';
64
        \wp_die();
65
    }
66
    
67
    public function reset_settings()
68
    {
69
        echo 'reset';
70
        \wp_die();
71
    }
72
    
73
    /**
74
     * Private constructor to prevent instantiation
75
     */
76
    private function __construct() 
77
    {
78
        $this->init();
79
    }
80
    
81
    private function init()
82
    {
83
        \add_action('admin_init',array($this,'register_scripts'));
84
        \add_action('wp_ajax_amarkal_settings_save', array( $this, 'save_settings'));
85
        \add_action('wp_ajax_amarkal_settings_reset', array( $this, 'reset_settings'));
86
    }
87
    
88
    private function get_url( $path )
89
    {
90
        $url  = str_replace( ABSPATH, '', $path );
91
        return esc_url_raw( site_url( $url ) );
92
    }
93
}