Transformer::transformSelect()   B
last analyzed

Complexity

Conditions 10
Paths 160

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 34
rs 7.1666
cc 10
nc 160
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\FusionBuilder;
4
5
use GeminiLabs\SiteReviews\Helper;
6
use GeminiLabs\SiteReviews\Integrations\FusionBuilder\Defaults\ControlDefaults;
7
8
class Transformer
9
{
10
    public array $args;
11
    public string $name;
12
    public string $shortcode;
13
14
    public function __construct(string $name, array $args, string $shortcode = '')
15
    {
16
        $args = glsr(ControlDefaults::class)->merge(
17
            wp_parse_args(compact('name'), $args)
18
        );
19
        $this->args = $args;
20
        $this->name = $name;
21
        $this->shortcode = $shortcode;
22
        $method = Helper::buildMethodName('transform', $this->args['type']);
23
        if (method_exists($this, $method)) {
24
            call_user_func([$this, $method]);
25
        }
26
    }
27
28
    public function control(): array
29
    {
30
        $groups = [ // order is intentional
31
            'design' => esc_html_x('Design', 'admin-text', 'site-reviews'),
32
            'general' => esc_html_x('General', 'admin-text', 'site-reviews'),
33
            'advanced' => esc_html_x('Advanced', 'admin-text', 'site-reviews'),
34
        ];
35
        $control = $this->args;
36
        $control['group'] = $groups[$this->args['group']] ?? ucfirst($this->args['group']);
37
        return $control;
38
    }
39
40
    public function transformCheckbox(): void
41
    {
42
        if ('hide' === $this->name) {
43
            $this->args['heading'] = esc_html_x('Hide', 'admin-text', 'site-reviews');
44
        }
45
        if (!empty($this->args['options'])) {
46
            $placeholder = $this->args['placeholder'] ?? esc_html_x('Select...', 'admin-text', 'site-reviews');
47
            $this->args['placeholder_text'] = $placeholder;
48
            $this->args['type'] = 'multiple_select';
49
            $this->args['value'] = $this->args['options'];
50
        } else {
51
            $this->args['default'] = 0;
52
            $this->args['type'] = 'radio_button_set';
53
            $this->args['value'] = [
54
                0 => esc_html_x('No', 'admin-text', 'site-reviews'),
55
                'yes' => esc_html_x('Yes', 'admin-text', 'site-reviews'), // because the dependency option doesn't work well with numerical values
56
            ];
57
        }
58
    }
59
60
    public function transformNumber(): void
61
    {
62
        $this->args['type'] = 'range';
63
    }
64
65
    public function transformSelect(): void
66
    {
67
        if ('assigned_posts' === $this->name) {
68
            $this->args['placeholder'] = esc_html_x('Search Pages...', 'admin-text', 'site-reviews');
69
        }
70
        if ('assigned_terms' === $this->name) {
71
            $this->args['placeholder'] = esc_html_x('Search Categories...', 'admin-text', 'site-reviews');
72
        }
73
        if ('assigned_users' === $this->name) {
74
            $this->args['placeholder'] = esc_html_x('Search Users...', 'admin-text', 'site-reviews');
75
        }
76
        if ('author' === $this->name) {
77
            $this->args['placeholder'] = esc_html_x('Search User...', 'admin-text', 'site-reviews');
78
        }
79
        if ('post_id' === $this->name) {
80
            $this->args['placeholder'] = esc_html_x('Search Review...', 'admin-text', 'site-reviews');
81
        }
82
        if (!empty($this->args['options'])) {
83
            $options = $this->args['options'];
84
            if (!array_key_exists('', $options)) {
85
                $placeholder = $this->args['placeholder'] ?? esc_html_x('Select...', 'admin-text', 'site-reviews');
86
                $options = ['' => $placeholder] + $options;
87
            }
88
            $this->args['value'] = $options;
89
        } elseif (!isset($this->args['options'])) {
90
            $this->args['ajax'] = glsr()->prefix.'fusion_search_query';
91
            $this->args['ajax_params'] = [
92
                'option' => $this->name,
93
                'shortcode' => $this->shortcode,
94
            ];
95
            $this->args['placeholder'] ??= esc_html_x('Search...', 'admin-text', 'site-reviews');
96
            $this->args['type'] = 'ajax_select';
97
            if (!($this->args['multiple'] ?? false)) {
98
                $this->args['max_input'] = 1;
99
            }
100
        }
101
    }
102
103
    public function transformText(): void
104
    {
105
        $this->args['type'] = 'textfield';
106
        if ('id' === $this->name) {
107
            $this->args['description'] = esc_html_x('Add an ID to the wrapping HTML element.', 'admin-text', 'site-reviews');
108
            $this->args['heading'] = esc_html_x('Custom CSS ID', 'admin-text', 'site-reviews');
109
        }
110
        if ('class' === $this->name) {
111
            $this->args['description'] = esc_html_x('Add a class to the wrapping HTML element.', 'admin-text', 'site-reviews');
112
            $this->args['heading'] = esc_html_x('Custom CSS Class', 'admin-text', 'site-reviews');
113
        }
114
    }
115
}
116