Completed
Pull Request — master (#2)
by Beñat
02:38
created

Admin::menu()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 46
rs 8.9411
cc 2
eloc 21
nc 2
nop 0
1
<?php
2
3
namespace LIN3S\WPSymfonyForm\Admin;
4
5
use LIN3S\WPSymfonyForm\Admin\Storage\InMemoryStorage;
6
use LIN3S\WPSymfonyForm\Admin\Storage\YamlStorage;
7
use LIN3S\WPSymfonyForm\Admin\Views\FormsTable;
8
use LIN3S\WPSymfonyForm\Admin\Views\LogsTable;
9
use LIN3S\WPSymfonyForm\Registry\FormWrapperRegistry;
10
11
/**
12
 * Main admin class.
13
 *
14
 * @author Beñat Espiña <[email protected]>
15
 */
16
class Admin
17
{
18
    /**
19
     * The forms table.
20
     *
21
     * @var FormsTable
22
     */
23
    private $formsTable;
24
25
    /**
26
     * The logs table.
27
     *
28
     * @var LogsTable
29
     */
30
    public $logsTable;
31
32
    /**
33
     * The form wrapper registry.
34
     *
35
     * @var FormWrapperRegistry
36
     */
37
    private $formWrapperRegistry;
38
39
    /**
40
     * Array which contains the forms.
41
     *
42
     * @var array
43
     */
44
    private $forms;
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param FormWrapperRegistry $formWrapperRegistry The form wrapper registry
50
     */
51
    public function __construct(FormWrapperRegistry $formWrapperRegistry)
52
    {
53
        $this->formWrapperRegistry = $formWrapperRegistry;
54
        $this->forms = [];
55
        add_filter('set-screen-option', function ($status, $option, $value) {
0 ignored issues
show
Unused Code introduced by
The call to the function add_filter() seems unnecessary as the function has no side-effects.
Loading history...
56
            return $value;
57
        }, 10, 3);
0 ignored issues
show
Unused Code introduced by
The call to add_filter() has too many arguments starting with 10.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
58
59
        add_action('admin_menu', [$this, 'menu']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
60
    }
61
62
    /**
63
     * Loads the menu inside WordPress admin sidebar.
64
     */
65
    public function menu()
66
    {
67
        $menu = add_menu_page(
68
            'Symfony Forms',
69
            'Symfony Forms',
70
            'manage_options',
71
            'symfony-form',
72
            [$this, 'generalPage']
73
        );
74
        add_action("load-$menu", [$this, 'generalPageScreenOptions']);
0 ignored issues
show
Unused Code introduced by
The call to the function add_action() seems unnecessary as the function has no side-effects.
Loading history...
75
76
        foreach ($this->formWrapperRegistry->get() as $key => $formWrapper) {
0 ignored issues
show
Bug introduced by
The expression $this->formWrapperRegistry->get() of type array<integer,object<LIN...rm\Wrapper\FormWrapper> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
77
            $name = ucfirst($formWrapper->getName());
78
            $slug = 'wp-symfony-form-' . preg_replace('/\s+/', '', strtolower($formWrapper->getName()));
79
80
            $subMenu = add_submenu_page(
0 ignored issues
show
Unused Code introduced by
$subMenu is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
                'symfony-form',
82
                $name,
83
                $name,
84
                'manage_options',
85
                $slug,
86
                [$this, 'logArchivePage']
87
            );
88
89
            $this->forms[] = [
90
                'name' => $name,
91
                'link' => '<a href="?page=' . $slug . '">/wp-admin/admin.php?page=' . $slug . '</a>',
92
            ];
93
94
//            do_action('wp-symfony-form-load-'. $subMenu, ['name' => $name]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
//            add_action('wp-symfony-form-load-'. $subMenu, [$this, 'logArchivePageScreenOptions']);
96
97
//            add_action('wp-symfony-form-load-' . $subMenu, function () use ($name) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
//                add_screen_option('per_page', [
99
//                    'label'   => 'Logs',
100
//                    'default' => 10,
101
//                    'option'  => 'logs_per_page',
102
//                ]);
103
//
104
//                $this->logsTable = new LogsTable(
105
//                    $name,
106
//                    new YamlStorage()
107
//                );
108
//            });
109
        }
110
    }
111
112
    /**
113
     * Adds main form archive page screen options.
114
     */
115
    public function generalPageScreenOptions()
116
    {
117
        add_screen_option('per_page', [
118
            'label'   => 'Forms',
119
            'default' => 10,
120
            'option'  => 'forms_per_page',
121
        ]);
122
123
        $this->formsTable = new FormsTable(
124
            new InMemoryStorage(
125
                $this->forms
126
            )
127
        );
128
    }
129
130
//    /**
131
//     * Adds email log archive page screen options.
132
//     */
133
//    public function logArchivePageScreenOptions()
134
//    {
135
//        add_screen_option('per_page', [
136
//            'label'   => 'Logs',
137
//            'default' => 10,
138
//            'option'  => 'logs_per_page',
139
//        ]);
140
//
141
//        $this->logsTable = new LogsTable(
142
//            $
143
//            new YamlStorage()
144
//        );
145
//    }
146
147
    /**
148
     * Renders main form archive page.
149
     */
150
    public function generalPage()
151
    {
152
        ?>
153
        <div class="wrap">
154
            <h2><?php _e('General', \WPSymfonyForm::TRANSLATION_DOMAIN); ?></h2>
155
            <div id="poststuff">
156
                <div id="post-body" class="metabox-holder">
157
                    <div id="post-body-content">
158
                        <div class="meta-box-sortables ui-sortable">
159
                            <form method="post">
160
                                <?php
161
                                $this->formsTable->prepare_items();
162
                                $this->formsTable->display(); ?>
163
                            </form>
164
                        </div>
165
                    </div>
166
                </div>
167
                <br class="clear">
168
            </div>
169
        </div>
170
        <?php
171
    }
172
173
    /**
174
     * Renders log archive page.
175
     */
176
    public function logArchivePage()
177
    {
178
        ?>
179
        <div class="wrap">
180
            <h2><?php _e('Sent emails logs', \WPSymfonyForm::TRANSLATION_DOMAIN); ?></h2>
181
            <div id="poststuff">
182
                <div id="post-body" class="metabox-holder">
183
                    <div id="post-body-content">
184
                        <div class="meta-box-sortables ui-sortable">
185
                            <form method="post">
186
                                <?php
187
                                $this->logsTable->prepare_items();
188
                                $this->logsTable->display(); ?>
189
                            </form>
190
                        </div>
191
                    </div>
192
                </div>
193
                <br class="clear">
194
            </div>
195
        </div>
196
        <?php
197
    }
198
}
199