Completed
Pull Request — master (#2)
by Beñat
06:48
created

Admin::menu()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 44
rs 8.8571
cc 2
eloc 29
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\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
    }
65
66
    /**
67
     * Loads the menu inside WordPress admin sidebar.
68
     */
69
    public function menu()
70
    {
71
        $view = new General(
72
            new FormsTable(
73
                new InMemoryStorage(
74
                    $this->forms()
75
                )
76
            )
77
        );
78
79
        $menu = add_menu_page(
80
            'Symfony Forms',
81
            'Symfony Forms',
82
            'manage_options',
83
            'symfony-form',
84
            [$view, 'display']
85
        );
86
        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...
87
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
        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...
97
            $slug = 'wp-symfony-form-' . preg_replace('/\s+/', '', strtolower($formWrapper->getName()));
98
            $name = ucfirst($formWrapper->getName());
99
            $view = new Form(
100
                $name,
101
                new LogsTable(
102
                    $formWrapper->getName(),
103
                    $this->storage
104
                )
105
            );
106
107
            $subMenu = add_submenu_page('symfony-form', $name, $name, 'manage_options', $slug, function () use ($view) {
108
                $view->display();
109
            });
110
            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...
111
        }
112
    }
113
114
    public function forms()
115
    {
116
        if (!empty($this->forms)) {
117
            return $this->forms;
118
        }
119
120
        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...
121
            $slug = 'wp-symfony-form-' . preg_replace('/\s+/', '', strtolower($formWrapper->getName()));
122
            $name = ucfirst($formWrapper->getName());
123
124
            $this->forms[] = [
125
                'name' => $name,
126
                'link' => '<a href="?page=' . $slug . '">/wp-admin/admin.php?page=' . $slug . '</a>',
127
            ];
128
        }
129
130
        return $this->forms;
131
    }
132
}
133