ThemeFeaturesModule::addThemeFeatures()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 11
rs 8.8333
cc 7
nc 6
nop 0
1
<?php
2
3
namespace Leonidas\Framework\Theme\Module\Abstracts;
4
5
use Leonidas\Framework\Module\Abstracts\Module;
6
use Leonidas\Hooks\TargetsAfterSetupThemeHook;
7
8
abstract class ThemeFeaturesModule extends Module
9
{
10
    use TargetsAfterSetupThemeHook;
11
12
    public const IN_CORE = [
13
        'post-thumbnails',
14
        'post-formats',
15
        'html5',
16
        'custom-logo',
17
        'custom-header',
18
        'custom-background',
19
        'title-tag',
20
    ];
21
22
    public const CORE_DISABLE = [
23
        'disable-custom-colors',
24
        'disable-custom-gradients',
25
        'disable-custom-font-sizes',
26
    ];
27
28
    public const CORE_BOOL = [
29
        'automatic-feed-links',
30
        'custom-background',
31
        'custom-header',
32
        'custom-logo',
33
        'post-thumbnails',
34
        'title-tag',
35
    ];
36
37
    public const CORE_BASE = [
38
        'custom-background',
39
        'custom-header',
40
        'custom-logo',
41
        'html5',
42
        'post-formats',
43
    ];
44
45
    public const CORE_LIST = [];
46
47
    public const EXTRA_BOOL = [];
48
49
    public const EXTRA_BASE = [];
50
51
    public const EXTRA_LIST = [];
52
53
    public function hook(): void
54
    {
55
        $this->targetAfterSetupThemeHook();
56
    }
57
58
    protected function defineAfterSetUpThemePriority(): ?int
59
    {
60
        return PHP_INT_MAX;
61
    }
62
63
    protected function doAfterSetupThemeAction(): void
64
    {
65
        $this->addThemeFeatures();
66
    }
67
68
    protected function addThemeFeatures(): void
69
    {
70
        foreach ($this->features() as $feature => $args) {
71
            if ($this->isSupportedBoolFeature($feature, $args)) {
72
                add_theme_support($feature);
0 ignored issues
show
Bug introduced by
The function add_theme_support 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

72
                /** @scrutinizer ignore-call */ 
73
                add_theme_support($feature);
Loading history...
73
            } elseif ($this->isSupportedBaseFeature($feature, $args)) {
74
                add_theme_support($feature, $args);
75
            } elseif ($this->isSupportedListFeature($feature, $args)) {
76
                add_theme_support($feature, ...is_array($args) ? $args : [$args]);
77
            } elseif ($this->isDisabledFeature($feature, $args)) {
78
                remove_theme_support($feature);
0 ignored issues
show
Bug introduced by
The function remove_theme_support 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

78
                /** @scrutinizer ignore-call */ 
79
                remove_theme_support($feature);
Loading history...
79
            }
80
        }
81
    }
82
83
    protected function isSupportedBoolFeature(string $feature, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $feature 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

83
    protected function isSupportedBoolFeature(/** @scrutinizer ignore-unused */ string $feature, $args)

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...
84
    {
85
        return true === $args;
86
    }
87
88
    protected function isSupportedBaseFeature(string $feature, $args)
89
    {
90
        return !in_array($feature, $this->getVariadicFeatures())
91
            && !is_bool($args);
92
    }
93
94
    protected function isSupportedListFeature(string $feature, $args)
95
    {
96
        return in_array($feature, $this->getVariadicFeatures())
97
            && !is_bool($args);
98
    }
99
100
    protected function isDisabledFeature(string $feature, $args)
101
    {
102
        return current_theme_supports($feature) && false === $args;
0 ignored issues
show
Bug introduced by
The function current_theme_supports 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

102
        return /** @scrutinizer ignore-call */ current_theme_supports($feature) && false === $args;
Loading history...
103
    }
104
105
    public function getVariadicFeatures(): array
106
    {
107
        return array_merge(static::CORE_LIST, static::EXTRA_LIST);
108
    }
109
110
    abstract protected function features(): array;
111
}
112