Completed
Pull Request — master (#2)
by Beñat
02:35
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\Action\ActionNotFoundException;
15
use LIN3S\WPSymfonyForm\Action\LocalStorageAction;
16
use LIN3S\WPSymfonyForm\Admin\Storage\InMemoryStorage;
17
use LIN3S\WPSymfonyForm\Admin\Storage\Storage;
18
use LIN3S\WPSymfonyForm\Admin\Storage\YamlStorage;
19
use LIN3S\WPSymfonyForm\Admin\Views\Components\FormsTable;
20
use LIN3S\WPSymfonyForm\Admin\Views\Components\LogsTable;
21
use LIN3S\WPSymfonyForm\Admin\Views\Form;
22
use LIN3S\WPSymfonyForm\Admin\Views\General;
23
use LIN3S\WPSymfonyForm\Registry\FormWrapperRegistry;
24
use LIN3S\WPSymfonyForm\Wrapper\FormWrapper;
25
26
/**
27
 * Main admin class.
28
 *
29
 * @author Beñat Espiña <[email protected]>
30
 */
31
class Admin
32
{
33
    /**
34
     * The form wrapper registry.
35
     *
36
     * @var FormWrapperRegistry
37
     */
38
    private $formWrapperRegistry;
39
40
    /**
41
     * Array which contains the forms.
42
     *
43
     * @var array
44
     */
45
    private $forms;
46
47
    /**
48
     * Constructor.
49
     *
50
     * @param FormWrapperRegistry $formWrapperRegistry The form wrapper registry
51
     */
52
    public function __construct(FormWrapperRegistry $formWrapperRegistry)
53
    {
54
        $this->formWrapperRegistry = $formWrapperRegistry;
55
        $this->forms = [];
56
        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...
57
            return $value;
58
        }, 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...
59
60
        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...
61
        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...
62
    }
63
64
    /**
65
     * Loads the menu inside WordPress admin sidebar.
66
     */
67
    public function menu()
68
    {
69
        $view = new General(
70
            new FormsTable(
71
                new InMemoryStorage(
72
                    $this->forms()
73
                )
74
            )
75
        );
76
77
        $menu = add_menu_page(
78
            'Symfony Forms',
79
            'Symfony Forms',
80
            'manage_options',
81
            'symfony-form',
82
            [$view, 'display']
83
        );
84
        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...
85
86
        add_submenu_page(
87
            'symfony-form',
88
            __('General', \WPSymfonyForm::TRANSLATION_DOMAIN),
89
            __('General', \WPSymfonyForm::TRANSLATION_DOMAIN),
90
            'manage_options',
91
            'symfony-form'
92
        );
93
    }
94
95
    /**
96
     * Loads the form sub menus inside WordPress admin sidebar.
97
     */
98
    public function subMenu()
99
    {
100
        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...
101
            $slug = 'wp-symfony-form-' . preg_replace('/\s+/', '', strtolower($formWrapper->getName()));
102
            $name = ucfirst($formWrapper->getName());
103
            $view = new Form(
104
                $name,
105
                new LogsTable(
106
                    $formWrapper->getName(),
107
                    $this->storage($formWrapper)
108
                )
109
            );
110
111
            $subMenu = add_submenu_page(
112
                'symfony-form',
113
                $name,
114
                $name,
115
                'manage_options',
116
                $slug,
117
                [$view, 'display']
118
            );
119
            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...
120
        }
121
    }
122
123
    /**
124
     * Resolves and gets the proper storage for the given form wrapper.
125
     *
126
     * @param FormWrapper $formWrapper The form wrapper
127
     *
128
     * @throws ActionNotFoundException when required action type not found
129
     *
130
     * @return Storage
131
     */
132
    private function storage(FormWrapper $formWrapper)
133
    {
134
        foreach ($formWrapper->getSuccessActions() as $action) {
135
            if ($action instanceof LocalStorageAction) {
136
                return new YamlStorage();
137
            }
138
        }
139
140
        throw new ActionNotFoundException();
141
    }
142
143
    /**
144
     * Populates the forms with its name and its link, returning the built array.
145
     *
146
     * @return array
147
     */
148
    private function forms()
149
    {
150
        if (!empty($this->forms)) {
151
            return $this->forms;
152
        }
153
154
        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...
155
            $slug = 'wp-symfony-form-' . preg_replace('/\s+/', '', strtolower($formWrapper->getName()));
156
            $name = ucfirst($formWrapper->getName());
157
158
            $this->forms[] = [
159
                'name' => $name,
160
                'link' => '<a href="?page=' . $slug . '">/wp-admin/admin.php?page=' . $slug . '</a>',
161
            ];
162
        }
163
164
        return $this->forms;
165
    }
166
}
167