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

Admin::subMenu()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 17
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the WPSymfonyForm plugin.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\WPSymfonyForm\Admin;
13
14
use LIN3S\WPSymfonyForm\Admin\Storage\InMemoryStorage;
15
use LIN3S\WPSymfonyForm\Admin\Storage\Storage;
16
use LIN3S\WPSymfonyForm\Admin\Storage\YamlStorage;
17
use LIN3S\WPSymfonyForm\Admin\Views\Components\FormsTable;
18
use LIN3S\WPSymfonyForm\Admin\Views\Components\LogsTable;
19
use LIN3S\WPSymfonyForm\Admin\Views\Form;
20
use LIN3S\WPSymfonyForm\Admin\Views\General;
21
use LIN3S\WPSymfonyForm\Registry\FormWrapperRegistry;
22
23
/**
24
 * Main admin class.
25
 *
26
 * @author Beñat Espiña <[email protected]>
27
 */
28
class Admin
29
{
30
    /**
31
     * The logs table.
32
     *
33
     * @var LogsTable
34
     */
35
    public $logsTable;
36
37
    /**
38
     * The form wrapper registry.
39
     *
40
     * @var FormWrapperRegistry
41
     */
42
    private $formWrapperRegistry;
43
44
    /**
45
     * Array which contains the forms.
46
     *
47
     * @var array
48
     */
49
    private $forms;
50
51
    /**
52
     * The storage strategy.
53
     *
54
     * @var Storage
55
     */
56
    private $storage;
57
58
    /**
59
     * Constructor.
60
     *
61
     * @param FormWrapperRegistry $formWrapperRegistry The form wrapper registry
62
     */
63
    public function __construct(FormWrapperRegistry $formWrapperRegistry)
64
    {
65
        $this->storage = new YamlStorage(); // TODO: This is hardcoded for now
66
        $this->formWrapperRegistry = $formWrapperRegistry;
67
        $this->forms = [];
68
        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...
69
            return $value;
70
        }, 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...
71
72
        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...
73
        add_action('admin_menu', [$this, 'subMenu']);
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...
74
    }
75
76
    /**
77
     * Loads the menu inside WordPress admin sidebar.
78
     */
79
    public function menu()
80
    {
81
        $view = new General(
82
            new FormsTable(
83
                new InMemoryStorage(
84
                    $this->forms()
85
                )
86
            )
87
        );
88
89
        $menu = add_menu_page(
90
            'Symfony Forms',
91
            'Symfony Forms',
92
            'manage_options',
93
            'symfony-form',
94
            [$view, 'display']
95
        );
96
        add_action("load-$menu", [$view, 'screenOptions']);
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...
97
98
        add_submenu_page(
99
            'symfony-form',
100
            __('General', \WPSymfonyForm::TRANSLATION_DOMAIN),
101
            __('General', \WPSymfonyForm::TRANSLATION_DOMAIN),
102
            'manage_options',
103
            'symfony-form'
104
        );
105
    }
106
107
    /**
108
     * Loads the form sub menus inside WordPress admin sidebar.
109
     */
110
    public function subMenu()
111
    {
112
        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...
113
            $slug = 'wp-symfony-form-' . preg_replace('/\s+/', '', strtolower($formWrapper->getName()));
114
            $name = ucfirst($formWrapper->getName());
115
            $view = new Form(
116
                $name,
117
                new LogsTable(
118
                    $formWrapper->getName(),
119
                    $this->storage
120
                )
121
            );
122
123
            $subMenu = add_submenu_page(
124
                'symfony-form',
125
                $name,
126
                $name,
127
                'manage_options',
128
                $slug,
129
                [$view, 'display']
130
            );
131
            add_action("load-$subMenu", [$view, 'screenOptions']);
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...
132
        }
133
    }
134
135
    /**
136
     * Populates the forms with its name and its link, returning the built array.
137
     *
138
     * @return array
139
     */
140
    private function forms()
141
    {
142
        if (!empty($this->forms)) {
143
            return $this->forms;
144
        }
145
146
        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...
147
            $slug = 'wp-symfony-form-' . preg_replace('/\s+/', '', strtolower($formWrapper->getName()));
148
            $name = ucfirst($formWrapper->getName());
149
150
            $this->forms[] = [
151
                'name' => $name,
152
                'link' => '<a href="?page=' . $slug . '">/wp-admin/admin.php?page=' . $slug . '</a>',
153
            ];
154
        }
155
156
        return $this->forms;
157
    }
158
}
159