Test Failed
Push — develop ( 76157d...9702e2 )
by Paul
08:08
created

AbstractHooks::levelPluginsLoaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 1
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Hooks;
4
5
use GeminiLabs\SiteReviews\Contracts\HooksContract;
6
7
/**
8
 * Trigger order of primary WP hooks:
9
 * 
10
 * 1. plugins_loaded     Fires once activated plugins have loaded.
11
 * 2. load_textdomain    Fires before the MO translation file is loaded.
12
 * 3. after_setup_theme  Fires after the theme is loaded.
13
 * 4. init               Fires after WordPress has finished loading but before any headers are sent.
14
 * 5. wp_loaded          Fires after WordPress, all plugins, and the theme are fully loaded and instantiated.
15
 * 6. admin_init         Fires as an admin screen or script is being initialized.
16
 * 7. current_screen     Fires after the current screen has been set.
17
 * 8. load-{$page_hook}  Fires before a particular screen is loaded.
18
 */
19
abstract class AbstractHooks implements HooksContract
20
{
21
    protected $basename;
22
    protected $id;
23
    protected $prefix;
24
    protected $taxonomy;
25
    protected $type;
26
27
    public function __construct()
28
    {
29
        $this->basename = glsr()->basename;
30
        $this->id = glsr()->id;
31
        $this->prefix = glsr()->prefix;
32
        $this->taxonomy = glsr()->taxonomy;
33
        $this->type = glsr()->post_type;
34
    }
35
36
    public function hook(string $classname, array $hooks): void
37
    {
38
        glsr()->singleton($classname); // make singleton
39
        $controller = glsr($classname);
40
        foreach ($hooks as $hook) {
41
            if (2 > count($hook)) {
42
                continue;
43
            }
44
            $func = str_starts_with($hook[0], 'filter') ? 'add_filter' : 'add_action';
45
            $hook = array_pad($hook, 3, 10); // priority
46
            $hook = array_pad($hook, 4, 1); // accepted args
47
            $args = [ // order is intentional!
48
                'hook' => $hook[1],
49
                'callback' => [$controller, $hook[0]],
50
                'priority' => (int) $hook[2],
51
                'args' => (int) $hook[3],
52
            ];
53
            if (!str_starts_with($args['hook'], glsr()->id) && method_exists($controller, 'proxy')) {
54
                $args['callback'] = $controller->proxy($hook[0]);
55
            }
56
            call_user_func_array($func, array_values($args));
57
        }
58
    }
59
60
    public function levelInit(): ?int
61
    {
62
        return null;
63
    }
64
65
    public function levelPluginsLoaded(): ?int
66
    {
67
        return null;
68
    }
69
70
    /**
71
     * @action init
72
     */
73
    public function onInit(): void
74
    {
75
    }
76
77
    /**
78
     * @action plugins_loaded
79
     */
80
    public function onPluginsLoaded(): void
81
    {
82
    }
83
84
    /**
85
     * @param mixed $fallback
86
     *
87
     * @return mixed
88
     */
89
    public function option(string $path, $fallback = '', string $cast = '')
90
    {
91
        return glsr_get_option($path, $fallback, $cast);
92
    }
93
94
    public function run(): void
95
    {
96
    }
97
98
    public function runDeferred(): void
99
    {
100
        if (null !== ($level = $this->levelInit())) {
0 ignored issues
show
introduced by
The condition null !== $level = $this->levelInit() is always false.
Loading history...
Bug introduced by
Are you sure the assignment to $level is correct as $this->levelInit() targeting GeminiLabs\SiteReviews\H...tractHooks::levelInit() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
101
            add_action('init', [$this, 'onInit'], $level);
102
        }
103
        if (null !== ($level = $this->levelPluginsLoaded())) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $level is correct as $this->levelPluginsLoaded() targeting GeminiLabs\SiteReviews\H...s::levelPluginsLoaded() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
introduced by
The condition null !== $level = $this->levelPluginsLoaded() is always false.
Loading history...
104
            add_action('plugins_loaded', [$this, 'onPluginsLoaded'], $level);
105
        }
106
    }
107
}
108