Passed
Push — master ( 68f720...5e727b )
by Paul
10:53
created

Plugin::action()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use BadMethodCallException;
6
use GeminiLabs\SiteReviews\Helpers\Arr;
7
use GeminiLabs\SiteReviews\Helpers\Cast;
8
use GeminiLabs\SiteReviews\Helpers\Str;
9
use ReflectionClass;
10
11
/**
12
 * @property string $id
13
 * @property string $name
14
 * @method array filterArray($hook, ...$args)
15
 * @method bool filterBool($hook, ...$args)
16
 * @method float filterFloat($hook, ...$args)
17
 * @method int filterInt($hook, ...$args)
18
 * @method object filterObject($hook, ...$args)
19
 * @method string filterString($hook, ...$args)
20
 */
21
trait Plugin
22
{
23
    /**
24
     * @var static
25
     */
26
    protected static $instance;
27
28
    protected $file;
29
    protected $languages;
30
    protected $testedTo;
31
    protected $version;
32
33 27
    public function __call($method, $args)
34
    {
35 27
        $isFilter = Str::startsWith('filter', $method);
36 27
        $to = Helper::buildMethodName(Str::removePrefix($method, 'filter'), 'to');
37 27
        if ($isFilter && method_exists(Cast::class, $to)) {
38 27
            $filtered = call_user_func_array([$this, 'filter'], $args);
39 27
            return Cast::$to($filtered);
40
        }
41
        throw new BadMethodCallException("Method [$method] does not exist.");
42
    }
43
44
    public function __construct()
45
    {
46
        $file = wp_normalize_path((new ReflectionClass($this))->getFileName());
0 ignored issues
show
Bug introduced by
The function wp_normalize_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

46
        $file = /** @scrutinizer ignore-call */ wp_normalize_path((new ReflectionClass($this))->getFileName());
Loading history...
47
        $this->file = str_replace('plugin/Application', $this->id, $file);
48
        $plugin = get_file_data($this->file, [
0 ignored issues
show
Bug introduced by
The function get_file_data was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

48
        $plugin = /** @scrutinizer ignore-call */ get_file_data($this->file, [
Loading history...
49
            'languages' => 'Domain Path',
50
            'name' => 'Plugin Name',
51
            'testedTo' => 'Tested up to',
52
            'version' => 'Version',
53
        ], 'plugin');
54
        array_walk($plugin, function ($value, $key) {
55
            if (property_exists($this, $key)) {
56
                $this->$key = $value;
57
            }
58
        });
59
    }
60
61 27
    public function __get($property)
62
    {
63 27
        if (property_exists($this, $property)) {
64 15
            return $this->$property;
65
        }
66 27
        $constant = 'static::'.strtoupper($property);
67 27
        if (defined($constant)) {
68 27
            return constant($constant);
69
        }
70
    }
71
72
    /**
73
     * @param string $hook
74
     * @param mixed ...$args
75
     * @return void
76
     */
77 26
    public function action($hook, ...$args)
78
    {
79 26
        do_action_ref_array($this->id.'/'.$hook, $args);
0 ignored issues
show
Bug introduced by
The function do_action_ref_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

79
        /** @scrutinizer ignore-call */ 
80
        do_action_ref_array($this->id.'/'.$hook, $args);
Loading history...
80 26
    }
81
82
    /**
83
     * @param mixed $args
84
     * @return Arguments
85
     */
86 15
    public function args($args = [])
87
    {
88 15
        return new Arguments($args);
89
    }
90
91
    /**
92
     * @param string $name
93
     * @return array
94
     */
95 9
    public function config($name)
96
    {
97 9
        $path = $this->filterString('config', 'config/'.$name.'.php');
98 9
        $configFile = $this->path($path);
99 9
        $config = file_exists($configFile)
100 9
            ? include $configFile
101 9
            : [];
102 9
        return $this->filterArray('config/'.$name, $config);
103
    }
104
105
    /**
106
     * @param string $property
107
     * @return string
108
     */
109 21
    public function constant($property, $className = 'static')
110
    {
111 21
        $property = strtoupper($property);
112 21
        $constant = $className.'::'.$property;
113 21
        return defined($constant)
114 21
            ? $this->filterString('const/'.$property, constant($constant))
115 21
            : '';
116
    }
117
118
    /**
119
     * @param string $hook
120
     * @param mixed ...$args
121
     * @return mixed
122
     */
123 27
    public function filter($hook, ...$args)
124
    {
125 27
        return apply_filters_ref_array($this->id.'/'.$hook, $args);
0 ignored issues
show
Bug introduced by
The function apply_filters_ref_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

125
        return /** @scrutinizer ignore-call */ apply_filters_ref_array($this->id.'/'.$hook, $args);
Loading history...
126
    }
127
128
    /**
129
     * @param string $hook
130
     * @param mixed ...$args
131
     * @return array
132
     */
133 7
    public function filterArrayUnique($hook, ...$args)
134
    {
135 7
        $filtered = apply_filters_ref_array($this->id.'/'.$hook, $args);
0 ignored issues
show
Bug introduced by
The function apply_filters_ref_array was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

135
        $filtered = /** @scrutinizer ignore-call */ apply_filters_ref_array($this->id.'/'.$hook, $args);
Loading history...
136 7
        return array_unique(array_filter(Cast::toArray($filtered)));
137
    }
138
139
    /**
140
     * @return static
141
     */
142 27
    public static function load()
143
    {
144 27
        if (empty(static::$instance)) {
145
            static::$instance = new static();
146
        }
147 27
        return static::$instance;
148
    }
149
150
    /**
151
     * @param string $file
152
     * @return string
153
     */
154 15
    public function path($file = '', $realpath = true)
155
    {
156 15
        $path = plugin_dir_path($this->file);
0 ignored issues
show
Bug introduced by
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

156
        $path = /** @scrutinizer ignore-call */ plugin_dir_path($this->file);
Loading history...
157 15
        if (!$realpath) {
158 9
            $path = trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file));
0 ignored issues
show
Bug introduced by
The constant GeminiLabs\SiteReviews\WP_PLUGIN_DIR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The function trailingslashit was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

158
            $path = /** @scrutinizer ignore-call */ trailingslashit(WP_PLUGIN_DIR).basename(dirname($this->file));
Loading history...
159
        }
160 15
        $path = trailingslashit($path).ltrim(trim($file), '/');
161 15
        return $this->filterString('path', $path, $file);
162
    }
163
164
    /**
165
     * @param string $className
166
     * @return mixed|false
167
     */
168
    public function runIf($className, ...$args)
169
    {
170
        return class_exists($className)
171
            ? call_user_func_array([glsr($className), 'handle'], $args)
172
            : false;
173
    }
174
175
    /**
176
     * @param string $path
177
     * @return string
178
     */
179
    public function url($path = '')
180
    {
181
        $url = esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/'));
0 ignored issues
show
Bug introduced by
The function plugin_dir_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

181
        $url = esc_url(/** @scrutinizer ignore-call */ plugin_dir_url($this->file).ltrim(trim($path), '/'));
Loading history...
Bug introduced by
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

181
        $url = /** @scrutinizer ignore-call */ esc_url(plugin_dir_url($this->file).ltrim(trim($path), '/'));
Loading history...
182
        return $this->filterString('url', $url, $path);
183
    }
184
185
    /**
186
     * @param string $versionLevel
187
     * @return string
188
     */
189 7
    public function version($versionLevel = '')
190
    {
191 7
        $pattern = '/^v?(\d{1,5})(\.\d++)?(\.\d++)?(.+)?$/i';
192 7
        preg_match($pattern, $this->version, $matches);
193 7
        switch ($versionLevel) {
194 7
            case 'major':
195
                $version = Arr::get($matches, 1);
196
                break;
197 7
            case 'minor':
198 7
                $version = Arr::get($matches, 1).Arr::get($matches, 2);
199 7
                break;
200
            case 'patch':
201
                $version = Arr::get($matches, 1).Arr::get($matches, 2).Arr::get($matches, 3);
202
                break;
203
        }
204 7
        return empty($version)
205
            ? $this->version
206 7
            : $version;
207
    }
208
}
209