Completed
Push — master ( 13445f...4ae133 )
by Mathieu
02:50
created

FormWidget::setLayout()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 18
loc 18
rs 9.4285
cc 3
eloc 12
nc 3
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A FormWidget::setSidebars() 0 8 2
1
<?php
2
3
namespace Charcoal\Admin\Widget;
4
5
use InvalidArgumentException;
6
7
use \Pimple\Container;
8
9
// From `charcoal-app`
10
use \Charcoal\App\Template\WidgetFactory;
11
12
/// From `charcoal-ui`
13
use \Charcoal\Ui\Form\FormInterface;
14
//use \Charcoal\Ui\Form\FormTrait;
15
use \Charcoal\Ui\Layout\LayoutAwareInterface;
16
use \Charcoal\Ui\Layout\LayoutAwareTrait;
17
18
use \Charcoal\Admin\AdminWidget;
19
//use \Charcoal\Admin\Ui\FormInterface;
20
use \Charcoal\Admin\Ui\FormTrait;
21
use \Charcoal\Admin\Ui\FormGroupInterface;
22
use \Charcoal\Admin\Widget\LayoutWidget;
23
24
// Local namespace dependencies
25
use \Charcoal\Admin\Widget\FormGroupWidget;
26
27
/**
28
 *
29
 */
30
class FormWidget extends AdminWidget implements
31
    FormInterface,
32
    LayoutAwareInterface
33
{
34
    use FormTrait;
35
    use LayoutAwareTrait;
36
37
    protected $sidebars = [];
38
39
    /**
40
    * @var WidgetFactory $widgetFactory
41
    */
42
    private $widgetFactory;
43
44
    public function setDependencies(Container $container)
45
    {
46
        //$this->setFormGroupBuilder($container['form/group/builder']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
90% 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...
47
        $this->setLayoutBuilder($container['layout/builder']);
48
    }
49
50
    /**
51
     * @param array|null $data
52
     * @return FormGroupInterface
53
     */
54
    public function createGroup(array $data = null)
55
    {
56
        $widget_type = (isset($data['widget_type']) ? $data['widget_type'] : 'charcoal/admin/widget/formGroup');
57
        $group = $this->widgetFactory()->create($widget_type, [
58
            'logger' => $this->logger
59
        ]);
60
        $group->setForm($this);
61
        if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
62
            $group->setData($data);
63
        }
64
        return $group;
65
66
    }
67
68
    /**
69
     * @param array $data
70
     * @return FormPropertyInterface
71
     */
72
    public function createFormProperty(array $data = null)
73
    {
74
        $p = new FormPropertyWidget([
75
            'logger'=>$this->logger
76
        ]);
77
        if ($data !== null) {
78
            $p->setData($data);
79
        }
80
        return $p;
81
    }
82
83
    /**
84
     *
85
     */
86
    public function setSidebars(array $sidebars)
87
    {
88
        $this->sidebars = [];
89
        foreach ($sidebars as $sidebarIdent => $sidebar) {
90
            $this->addSidebar($sidebarIdent, $sidebar);
91
        }
92
        return $this;
93
    }
94
95
    /**
96
     * @param array|FormSidebarWidget $sidebar
97
     * @throws InvalidArgumentException
98
     * @return FormWidget Chainable
99
     */
100 View Code Duplication
    public function addSidebar($sidebarIdent, $sidebar)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        if (!is_string($sidebarIdent)) {
103
            throw new InvalidArgumentException(
104
                'Sidebar ident must be a string'
105
            );
106
        }
107
        if (($sidebar instanceof FormSidebarWidget)) {
108
            $this->sidebars[$sidebarIdent] = $sidebar;
109
        } else if (is_array($sidebar)) {
110
            $s = new FormSidebarWidget([
111
                'logger'=>$this->logger
112
            ]);
113
            $s->setForm($this);
114
            $s->setData($sidebar);
115
            $this->sidebars[$sidebarIdent] = $s;
116
        } else {
117
            throw new InvalidArgumentException(
118
                'Sidebar must be a FormSidebarWidget object or an array'
119
            );
120
        }
121
        return $this;
122
    }
123
124
    /**
125
     * @return FormSidebarWidget
126
     */
127 View Code Duplication
    public function sidebars()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
sidebars uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
128
    {
129
        $sidebars = $this->sidebars;
130
        if (!is_array($this->sidebars)) {
131
            yield null;
132
        } else {
133
            uasort($sidebars, ['self', 'sortSidebarsByPriority']);
134
            foreach ($sidebars as $sidebar) {
135
                /*if ($sidebar->widget_type() != '') {
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% 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...
136
                    $GLOBALS['widget_template'] = $sidebar->widget_type();
137
                } else {
138
                    $GLOBALS['widget_template'] = 'charcoal/admin/widget/form.sidebar';
139
                }*/
140
                $GLOBALS['widget_template'] = 'charcoal/admin/widget/form.sidebar';
141
                yield $sidebar->ident() => $sidebar;
142
            }
143
        }
144
    }
145
146
    /**
147
     * To be called with uasort().
148
     *
149
     * @param FormGroupInterface $a Item "a" to compare, for sorting.
150
     * @param FormGroupInterface $b Item "b" to compaer, for sorting.
151
     * @return integer Sorting value: -1, 0, or 1
152
     */
153 View Code Duplication
    protected static function sortSidebarsByPriority(FormGroupInterface $a, FormGroupInterface $b)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        $a = $a->priority();
156
        $b = $b->priority();
157
158
        if ($a == $b) {
159
            return 1;
160
// Should be 0?
161
        }
162
163
        return ($a < $b) ? (-1) : 1;
164
    }
165
166
    /**
167
     * @return WidgetFactory
168
     */
169
    private function widgetFactory()
170
    {
171
        if ($this->widgetFactory === null) {
172
            $this->widgetFactory = new WidgetFactory();
173
        }
174
        return $this->widgetFactory;
175
    }
176
}
177