PluginHandler::getPluginInstance()   A
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
c 0
b 0
f 0
nc 10
nop 1
dl 0
loc 16
rs 9.6111
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xhelp;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    {@link https://xoops.org/ XOOPS Project}
17
 * @license      {@link https://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
18
 * @author       Brian Wahoff <[email protected]>
19
 * @author       XOOPS Development Team
20
 */
21
22
/**
23
 * Manages the retrieval, loading, and unloading of plugins
24
 *
25
 * @author  Brian Wahoff <[email protected]>
26
 */
27
class PluginHandler
28
{
29
    /**
30
     * Database connection
31
     *
32
     * @var object
33
     */
34
    public $db;
35
    public $active;
36
    public $plugins;
37
38
    /**
39
     * PluginHandler constructor.
40
     */
41
    public function __construct(\XoopsDatabase $db = null)
42
    {
43
        $this->db     = $db;
44
        $this->active = \unserialize(Utility::getMeta('plugins'));
0 ignored issues
show
Bug introduced by
It seems like XoopsModules\Xhelp\Utility::getMeta('plugins') can also be of type boolean and null; however, parameter $data of unserialize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        $this->active = \unserialize(/** @scrutinizer ignore-type */ Utility::getMeta('plugins'));
Loading history...
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    private function pluginList(): array
51
    {
52
        $plugins = [];
53
        //Open Directory
54
        $d = @\dir(XHELP_PLUGIN_PATH);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Xhelp\XHELP_PLUGIN_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55
56
        if ($d) {
57
            while (false !== ($entry = $d->read())) {
58
                if (!\preg_match('|^\.+$|', $entry) && \preg_match('|\.php$|', $entry)) {
59
                    $plugins[] = \basename(XHELP_PLUGIN_PATH . '/' . $entry, '.php');
60
                }
61
            }
62
        }
63
64
        return $plugins;
65
    }
66
67
    public function getActivePlugins(): void
68
    {
69
        $plugin_files = $this->pluginList();
70
71
        foreach ($plugin_files as $plugin) {
72
            if (\in_array($plugin, $this->active)) {
73
            }
74
        }
75
    }
76
77
    /**
78
     * @param string $script
79
     */
80
    public function activatePlugin(string $script): void
0 ignored issues
show
Unused Code introduced by
The parameter $script is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

80
    public function activatePlugin(/** @scrutinizer ignore-unused */ string $script): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
    {
82
    }
83
84
    /**
85
     * @param string $script
86
     */
87
    public function deactivatePlugin(string $script): void
0 ignored issues
show
Unused Code introduced by
The parameter $script is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

87
    public function deactivatePlugin(/** @scrutinizer ignore-unused */ string $script): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89
    }
90
91
    /**
92
     * @param string $filename
93
     * @return bool
94
     */
95
    public function getPluginInstance(string $filename): bool
96
    {
97
        if (!isset($this->plugins[$filename])) {
98
            if (\is_file($plug_file = XHELP_PLUGIN_PATH . '/' . $filename . '.php')) {
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Xhelp\XHELP_PLUGIN_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
99
                require_once $plug_file;
100
            }
101
            $class = \mb_strtolower(XHELP_DIRNAME) . \ucfirst($filename);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Xhelp\XHELP_DIRNAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
102
            if (\class_exists($class)) {
103
                $this->plugins[$filename] = new $class($GLOBALS['_eventsrv']);
104
            }
105
        }
106
        if (!isset($this->plugins[$filename])) {
107
            \trigger_error('Plugin does not exist<br>Module: ' . XHELP_DIRNAME . '<br>Name: ' . $filename, \E_USER_ERROR);
108
        }
109
110
        return $this->plugins[$filename] ?? false;
111
    }
112
}
113