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

Admin::forms()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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