SubMenu::plugin_page()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CodexShaper\WP\Admin\Menus;
4
5
/**
6
 * Menu Generator.
7
 */
8
class SubMenu
9
{
10
    public $parent_slug;
11
12
    public $page_title;
13
14
    public $menu_title;
15
16
    public $capability;
17
18
    public $slug;
19
20
    public $callback;
21
22
    public $position;
23
24
    public $plugin_name;
25
26
    public function save()
27
    {
28
        add_action('admin_menu', [$this, 'create_submenu']);
29
    }
30
31 View Code Duplication
    public static function make($options = [])
0 ignored issues
show
Duplication introduced by
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.

Loading history...
32
    {
33
        foreach ($options as $property => $value) {
34
            if (property_exists($this, $property)) {
35
                $this->{$property} = $value;
0 ignored issues
show
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
36
            }
37
        }
38
        add_action('admin_menu', [$this, 'create_submenu']);
39
    }
40
41
    /**
42
     * Register our menu page.
43
     *
44
     * @return void
45
     */
46
    public function create_submenu()
47
    {
48
        if (current_user_can($this->capability)) {
49
            $hook = add_submenu_page(
50
                $this->parent_slug,
51
                $this->page_title,
52
                $this->menu_title,
53
                $this->capability,
54
                $this->slug,
55
                $this->callback,
56
                $this->icon
0 ignored issues
show
Bug introduced by
The property icon does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
57
            );
58
        }
59
60
        // if ( current_user_can( $this->capability ) ) {
61
        //     $submenu[ $this->slug ][] = array( __( 'Clients', 'textdomain' ), $this->capability, 'admin.php?page=' . $this->slug . '#/clients' );
62
        //     $submenu[ $this->slug ][] = array( __( 'Settings', 'textdomain' ), $this->capability, 'admin.php?page=' . $this->slug . '#/settings' );
63
        // }
64
65
        add_action('load-'.$hook, [$this, 'init_hooks']);
0 ignored issues
show
Bug introduced by
The variable $hook does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
66
    }
67
68
    /**
69
     * Initialize our hooks for the admin page.
70
     *
71
     * @return void
72
     */
73
    public function init_hooks()
74
    {
75
        add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
76
    }
77
78
    /**
79
     * Load scripts and styles for the app.
80
     *
81
     * @return void
82
     */
83
    public function enqueue_scripts()
84
    {
85
        wp_enqueue_style($this->plugin_name . '-vendors');
86
        wp_enqueue_style($this->plugin_name . '-admin');
87
        wp_enqueue_script($this->plugin_name . '-admin');
88
    }
89
90
    /**
91
     * Render our admin page.
92
     *
93
     * @return void
94
     */
95
    public function plugin_page()
96
    {
97
        echo '<div class="wrap"><div id="'. $this->plugin_name .'-admin" csrf-token="'.csrf_token().'"></div></div>';
98
    }
99
}
100