Completed
Push — master ( 24cf5e...bc1ad9 )
by Askupa
02:11
created

RequestHandler.php (6 issues)

Upgrade to new PHP Analysis Engine

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\Settings;
4
5
class RequestHandler
6
{
7
    /**
8
     * @var Singleton The reference to *Singleton* instance of this class
9
     */
10
    private static $instance;
11
    
12
    private $request_data = array();
13
    
14
    const NONCE_ACTION = 'amarkal_settings';
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
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
Documentation Bug introduced by
It seems like new static() of type this<Amarkal\Settings\RequestHandler> 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...
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...
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 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\Request...rkal\Settings\Singleton adds the type Amarkal\Settings\RequestHandler to the return on line 27 which is incompatible with the return type documented by Amarkal\Settings\RequestHandler::get_instance of type Amarkal\Settings\Singleton.
Loading history...
28
    }
29
    
30
    public function init()
31
    {
32
        \add_action('wp_ajax_amarkal_settings_save', array( $this, 'save_settings'));
33
        \add_action('wp_ajax_amarkal_settings_reset', array( $this, 'reset_settings'));
34
    }
35
    
36
    public function save_settings()
37
    {
38
        $this->set_request_data();
39
        $child_page  = $this->get_request_child_page();
40
        
41
        \wp_send_json($child_page->update($this->request_data));
42
    }
43
    
44
    public function reset_settings()
45
    {
46
        $this->set_request_data();
47
        $child_page = $this->get_request_child_page();
48
        
49
        \wp_send_json($child_page->reset());
50
    }
51
    
52
    private function get_request_child_page()
53
    {
54
        $manager     = Manager::get_instance();
55
        $slug        = $this->request_data['_amarkal_settings_slug'];
56
        $parent_slug = $this->request_data['_amarkal_settings_parent_slug'];
57
        
58
        return $manager->get_child_page($slug, $parent_slug);
59
    }
60
    
61
    private function set_request_data()
62
    {
63
        $this->request_data = filter_input(INPUT_POST,'data',FILTER_DEFAULT,FILTER_REQUIRE_ARRAY);
0 ignored issues
show
Documentation Bug introduced by
It seems like filter_input(INPUT_POST,..., FILTER_REQUIRE_ARRAY) of type * is incompatible with the declared type array of property $request_data.

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...
64
        $nonce = $this->request_data['_amarkal_settings_nonce'];
65
66
        if( !isset( $nonce ) ||
67
            !\wp_verify_nonce($nonce, self::NONCE_ACTION) ) 
68
        {
69
            \wp_send_json(array(
70
                'values' => array(),
71
                'errors' => array(
72
                    'Your nonce did not verify'
73
                )
74
            ));
75
        }
76
    }
77
}