Completed
Push — master ( 1b9d6b...06771b )
by Askupa
01:19
created

Manager::default_args()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Amarkal\Shortcode;
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 $shortcodes = 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
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...
22
        {
23
            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\Shortcode\Manager> is incompatible with the declared type object<Amarkal\Shortcode\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...
24
        }
25
        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\Shortcode\Manage...kal\Shortcode\Singleton adds the type Amarkal\Shortcode\Manager to the return on line 25 which is incompatible with the return type documented by Amarkal\Shortcode\Manager::get_instance of type Amarkal\Shortcode\Singleton.
Loading history...
26
    }
27
    
28
    public function register_shortcode( $args )
29
    {
30
        $this->validate_args($args);
31
        if($this->shortcode_exists($args['id']))
32
        {
33
            throw new \RuntimeException("A shortcode with id '{$args['id']}' has already been registered");
34
        }
35
        $this->shortcodes[$args['id']] = array_merge($this->default_args(), $args);
36
    }
37
    
38
    public function print_json_object($plugin_array)
39
    {
40
        
41
        return $plugin_array;
42
    }
43
    
44
    public function enqueue_script($plugins_array)
45
    {
46
        // Printing the JSON object ensures that it will be available whenever 
47
        // the visual editor is present.
48
        echo "<script id='amarkal-shortcode-json' type='application/json'>{$this->prepare_json_object()}</script>";
49
        
50
        // This script must be included after the JSON object, since it refers
51
        // to it, and so the JSON must be readily available.
52
        $plugins_array['amarkal_shortcode'] = \Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/js/dist/amarkal-shortcode.min.js');
53
        return $plugins_array;
54
    }
55
56
    public function enqueue_style()
57
    {
58
        \add_editor_style(\Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/css/dist/amarkal-shortcode.min.css'));
59
    }
60
    
61
    /**
62
     * Create a JSON object that will be printed in the admin section
63
     * to be used by the TinyMCE plugin code.
64
     */
65
    private function prepare_json_object()
66
    {
67
        $json = array();
68
        foreach($this->shortcodes as $id => $shortcode)
69
        {
70
            $popup = new Popup($shortcode['fields']);
71
            $json[$id] = $shortcode;
72
            $json[$id]['html'] = $popup->render();
73
        }
74
        return json_encode($json);
75
    }
76
    
77
    /**
78
     * Default shortcode arguments
79
     */
80
    private function default_args()
81
    {
82
        return array(
83
            'id'                => null,
84
            'title'             => '',
85
            'template'          => null,
86
            'cmd'               => '',
87
            'width'             => 550,
88
            'height'            => 450,
89
            'fields'            => array(),
90
            'show_placeholder'  => true,
91
            'placeholder_class' => null
92
        );
93
    }
94
    
95
    /**
96
     * Check if a shortcode with the given ID has already been registered
97
     */
98
    private function shortcode_exists( $id )
99
    {
100
        return array_key_exists($id, $this->shortcodes);
101
    }
102
    
103
    /**
104
     * Validate that the provided arguments have the required arguments as
105
     * specified in self::required_args()
106
     */
107
    private function validate_args( $args )
108
    {
109
        foreach($this->required_args() as $arg)
110
        {
111
            if(!array_key_exists($arg, $args))
112
            {
113
                throw new \RuntimeException("Missing required argument '$arg'");
114
            }
115
        }
116
    }
117
    
118
    /**
119
     * A list of required arguments
120
     */
121
    private function required_args()
122
    {
123
        return array('id','template','fields');
124
    }
125
126
    /**
127
     * Private constructor to prevent instantiation
128
     */
129
    private function __construct() 
130
    {
131
        \add_filter('mce_external_plugins',array($this,'enqueue_script'));
132
        \add_action('admin_init', array($this,'enqueue_style'));
133
    }
134
}