WidgetMapManager   B
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 300
Duplicated Lines 2 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 40
lcom 1
cbo 8
dl 6
loc 300
rs 8.2608
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B insert() 0 32 3
D move() 6 33 9
C delete() 0 49 8
A overwrite() 0 12 1
B moveChildren() 0 18 7
A cloneWidgetMap() 0 13 1
C moveWidgetMap() 0 24 7
A getChildrenByView() 0 21 3

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like WidgetMapManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WidgetMapManager, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Victoire\Bundle\WidgetMapBundle\Manager;
4
5
use Doctrine\Orm\EntityManager;
6
use Victoire\Bundle\CoreBundle\Entity\View;
7
use Victoire\Bundle\WidgetBundle\Entity\Widget;
8
use Victoire\Bundle\WidgetMapBundle\Builder\WidgetMapBuilder;
9
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
10
use Victoire\Bundle\WidgetMapBundle\Helper\WidgetMapHelper;
11
use Victoire\Bundle\WidgetMapBundle\Resolver\WidgetMapChildrenResolver;
12
13
class WidgetMapManager
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
14
{
15
    private $em;
16
    private $builder;
17
    private $resolver;
18
19
    /**
20
     * WidgetMapManager constructor.
21
     *
22
     * @param EntityManager             $em
23
     * @param WidgetMapBuilder          $builder
24
     * @param WidgetMapChildrenResolver $resolver
25
     */
26
    public function __construct(
27
        EntityManager $em,
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
28
        WidgetMapBuilder $builder,
29
        WidgetMapChildrenResolver $resolver
30
    ) {
31
        $this->em = $em;
32
        $this->builder = $builder;
33
        $this->resolver = $resolver;
34
    }
35
36
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$widget" missing
Loading history...
introduced by
Doc comment for parameter "$view" missing
Loading history...
introduced by
Doc comment for parameter "$position" missing
Loading history...
introduced by
Doc comment for parameter "$widgetReference" missing
Loading history...
37
     * Insert a WidgetMap in a view at given position.
38
     *
39
     * @param string $slotId
0 ignored issues
show
introduced by
Doc comment for parameter $slotId does not match actual variable name $widget
Loading history...
40
     */
41
    public function insert(Widget $widget, View $view, $slotId, $position, $widgetReference)
42
    {
43
        $quantum = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->findOneBy([
44
            'view'     => $view,
45
            'slot'     => $slotId,
46
            'position' => $position,
47
            'parent'   => $widgetReference,
48
            'action'   => [
49
                WidgetMap::ACTION_CREATE,
50
                WidgetMap::ACTION_OVERWRITE,
51
            ],
52
        ]);
53
54
        if ($quantum) {
55
            $widget->setWidgetMap($quantum);
56
            $view->addWidgetMap($quantum);
57
        } else {
58
            $parent = null;
59
            if ($widgetReference) {
60
                $parent = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find($widgetReference);
61
            }
62
            //create the new widget map
63
            $widgetMapEntry = new WidgetMap();
64
            $widgetMapEntry->setAction(WidgetMap::ACTION_CREATE);
65
            $widgetMapEntry->setSlot($slotId);
66
            $widgetMapEntry->setPosition($position);
67
            $widgetMapEntry->setParent($parent);
0 ignored issues
show
Bug introduced by
It seems like $parent defined by $this->em->getRepository...>find($widgetReference) on line 60 can also be of type object; however, Victoire\Bundle\WidgetMa...\WidgetMap::setParent() does only seem to accept null|object<Victoire\Bun...undle\Entity\WidgetMap>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
            $widget->setWidgetMap($widgetMapEntry);
69
70
            $view->addWidgetMap($widgetMapEntry);
71
        }
72
    }
73
74
    /**
75
     * moves a widget in a view.
76
     *
77
     * @param View  $view
78
     * @param array $sortedWidget
79
     */
80
    public function move(View $view, $sortedWidget)
81
    {
82
        /** @var WidgetMap $parentWidgetMap */
83
        $parentWidgetMap = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find((int) $sortedWidget['parentWidgetMap']);
84
        $position = $sortedWidget['position'];
85
        $slot = $sortedWidget['slot'];
86
        /** @var WidgetMap $widgetMap */
87
        $widgetMap = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find((int) $sortedWidget['widgetMap']);
88
89
        $originalParent = $widgetMap->getParent();
90
        $originalPosition = $widgetMap->getPosition();
91
92
        $children = $this->resolver->getChildren($widgetMap, $view);
93
        $beforeChild = !empty($children[WidgetMap::POSITION_BEFORE]) ? $children[WidgetMap::POSITION_BEFORE] : null;
94
        $afterChild = !empty($children[WidgetMap::POSITION_AFTER]) ? $children[WidgetMap::POSITION_AFTER] : null;
95
96
        $parentWidgetMapChildren = $this->getChildrenByView($parentWidgetMap);
97
98
        $widgetMap = $this->moveWidgetMap($view, $widgetMap, $parentWidgetMap, $position, $slot);
0 ignored issues
show
Documentation introduced by
$parentWidgetMap is of type object<Victoire\Bundle\W...undle\Entity\WidgetMap>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
100
        $this->moveChildren($view, $beforeChild, $afterChild, $originalParent, $originalPosition);
101
102
        foreach ($parentWidgetMapChildren['views'] as $_view) {
103
            if ($_view !== $view) {
104 View Code Duplication
                if (isset($parentWidgetMapChildren['before'][$_view->getId()]) && $parentWidgetMapChildren['before'][$_view->getId()]->getPosition() == $widgetMap->getPosition()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
105
                    $parentWidgetMapChildren['before'][$_view->getId()]->setParent($widgetMap);
106
                }
107 View Code Duplication
                if (isset($parentWidgetMapChildren['after'][$_view->getId()]) && $parentWidgetMapChildren['after'][$_view->getId()]->getPosition() == $widgetMap->getPosition()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
108
                    $parentWidgetMapChildren['after'][$_view->getId()]->setParent($widgetMap);
109
                }
110
            }
111
        }
112
    }
113
114
    /**
115
     * Delete the widget from the view.
116
     *
117
     * @param View   $view
118
     * @param Widget $widget
119
     *
120
     * @throws \Exception Widget map does not exists
121
     */
122
    public function delete(View $view, Widget $widget)
123
    {
124
        $this->builder->build($view);
125
126
        $widgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($widget, $view);
127
        $slot = $widgetMap->getSlot();
0 ignored issues
show
Bug introduced by
The method getSlot does only exist in Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap, but not in Victoire\Bundle\WidgetMa...getMapNotFoundException.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
128
129
        $originalParent = $widgetMap->getParent();
0 ignored issues
show
Bug introduced by
The method getParent does only exist in Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap, but not in Victoire\Bundle\WidgetMa...getMapNotFoundException.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
130
        $originalPosition = $widgetMap->getPosition();
0 ignored issues
show
Bug introduced by
The method getPosition does only exist in Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap, but not in Victoire\Bundle\WidgetMa...getMapNotFoundException.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
131
132
        $children = $this->resolver->getChildren($widgetMap, $view);
0 ignored issues
show
Bug introduced by
It seems like $widgetMap defined by \Victoire\Bundle\WidgetM...AndView($widget, $view) on line 126 can also be of type object<Victoire\Bundle\W...etMapNotFoundException>; however, Victoire\Bundle\WidgetMa...Resolver::getChildren() does only seem to accept object<Victoire\Bundle\W...undle\Entity\WidgetMap>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
133
        $beforeChild = !empty($children[WidgetMap::POSITION_BEFORE]) ? $children[WidgetMap::POSITION_BEFORE] : null;
134
        $afterChild = !empty($children[WidgetMap::POSITION_AFTER]) ? $children[WidgetMap::POSITION_AFTER] : null;
135
136
        //we remove the widget from the current view
137
        if ($widgetMap->getView() === $view) {
0 ignored issues
show
Bug introduced by
The method getView does only exist in Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap, but not in Victoire\Bundle\WidgetMa...getMapNotFoundException.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
138
            // If the widgetMap has substitutes, delete them or transform them in create mode
139
            if (count($widgetMap->getAllSubstitutes()) > 0) {
140
                foreach ($widgetMap->getAllSubstitutes() as $substitute) {
0 ignored issues
show
Bug introduced by
The method getAllSubstitutes does only exist in Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap, but not in Victoire\Bundle\WidgetMa...getMapNotFoundException.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
141
                    if ($substitute->getAction() === WidgetMap::ACTION_OVERWRITE) {
142
                        $substitute->setAction(WidgetMap::ACTION_CREATE);
143
                        $substitute->setReplaced(null);
144
                    } else {
145
                        $view->removeWidgetMap($widgetMap);
146
                    }
147
                }
148
            }
149
            //remove the widget map from the slot
150
            $view->removeWidgetMap($widgetMap);
151
        } else {
152
            //the widget is owned by another view (a parent)
153
            //so we add a new widget map that indicates we delete this widget
154
            $replaceWidgetMap = new WidgetMap();
155
            $replaceWidgetMap->setAction(WidgetMap::ACTION_DELETE);
156
            $replaceWidgetMap->setWidget($widget);
0 ignored issues
show
Deprecated Code introduced by
The method Victoire\Bundle\WidgetMa...\WidgetMap::setWidget() has been deprecated.

This method has been deprecated.

Loading history...
157
            $replaceWidgetMap->setSlot($slot);
158
            $replaceWidgetMap->setReplaced($widgetMap);
0 ignored issues
show
Bug introduced by
It seems like $widgetMap defined by \Victoire\Bundle\WidgetM...AndView($widget, $view) on line 126 can also be of type object<Victoire\Bundle\W...etMapNotFoundException>; however, Victoire\Bundle\WidgetMa...idgetMap::setReplaced() does only seem to accept object<Victoire\Bundle\W...undle\Entity\WidgetMap>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
159
160
            $view->addWidgetMap($replaceWidgetMap);
161
        }
162
163
        //Move children for current WidgetMap View
164
        $this->moveChildren($view, $beforeChild, $afterChild, $originalParent, $originalPosition);
165
166
        //Move children WidgetMap for children from other View
167
        foreach ($widgetMap->getChildren() as $child) {
0 ignored issues
show
Bug introduced by
The method getChildren does only exist in Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap, but not in Victoire\Bundle\WidgetMa...getMapNotFoundException.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
168
            $this->moveWidgetMap($child->getView(), $child, $originalParent, $originalPosition);
169
        }
170
    }
171
172
    /**
173
     * the widget is owned by another view (a parent)
174
     * so we add a new widget map that indicates we delete this widget.
175
     *
176
     * @param View      $view
177
     * @param WidgetMap $originalWidgetMap
178
     * @param Widget    $widgetCopy
179
     *
180
     * @throws \Exception
181
     */
182
    public function overwrite(View $view, WidgetMap $originalWidgetMap, Widget $widgetCopy)
183
    {
184
        $widgetMap = new WidgetMap();
185
        $widgetMap->setAction(WidgetMap::ACTION_OVERWRITE);
186
        $widgetMap->setReplaced($originalWidgetMap);
187
        $widgetCopy->setWidgetMap($widgetMap);
188
        $widgetMap->setSlot($originalWidgetMap->getSlot());
189
        $widgetMap->setPosition($originalWidgetMap->getPosition());
190
        $widgetMap->setParent($originalWidgetMap->getParent());
191
192
        $view->addWidgetMap($widgetMap);
193
    }
194
195
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$beforeChild" missing
Loading history...
introduced by
Doc comment for parameter "$afterChild" missing
Loading history...
introduced by
Doc comment for parameter "$originalParent" missing
Loading history...
introduced by
Doc comment for parameter "$originalPosition" missing
Loading history...
196
     * If the moved widgetMap has someone at both his before and after, arbitrary move UP the before side
197
     * and find the first place after the before widgetMap hierarchy to place the after widgetMap.
198
     *
199
     * @param View $view
0 ignored issues
show
introduced by
Expected 13 spaces after parameter type; 1 found
Loading history...
200
     * @param $beforeChild
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
201
     * @param $afterChild
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
202
     * @param $originalParent
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
203
     * @param $originalPosition
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
204
     */
205
    public function moveChildren(View $view, $beforeChild, $afterChild, $originalParent, $originalPosition)
206
    {
207
        if ($beforeChild && $afterChild) {
208
            $this->moveWidgetMap($view, $beforeChild, $originalParent, $originalPosition);
209
210
            $child = $beforeChild;
211
            while ($child->getChild(WidgetMap::POSITION_AFTER)) {
212
                $child = $child->getChild(WidgetMap::POSITION_AFTER);
213
            }
214
            if ($afterChild->getId() !== $child->getId()) {
215
                $this->moveWidgetMap($view, $afterChild, $child);
216
            }
217
        } elseif ($beforeChild) {
218
            $this->moveWidgetMap($view, $beforeChild, $originalParent, $originalPosition);
219
        } elseif ($afterChild) {
220
            $this->moveWidgetMap($view, $afterChild, $originalParent, $originalPosition);
221
        }
222
    }
223
224
    /**
225
     * Create a copy of a WidgetMap in "overwrite" mode and insert it in the given view.
226
     *
227
     * @param WidgetMap $widgetMap
228
     * @param View      $view
229
     *
230
     * @throws \Exception
231
     *
232
     * @return WidgetMap
233
     */
234
    protected function cloneWidgetMap(WidgetMap $widgetMap, View $view)
235
    {
236
        $originalWidgetMap = $widgetMap;
237
        $widgetMap = clone $widgetMap;
238
        $widgetMap->setId(null);
239
        $widgetMap->setAction(WidgetMap::ACTION_OVERWRITE);
240
        $widgetMap->setReplaced($originalWidgetMap);
241
        $widgetMap->setView($view);
242
        $view->addWidgetMap($widgetMap);
243
        $this->em->persist($widgetMap);
244
245
        return $widgetMap;
246
    }
247
248
    /**
249
     * Move given WidgetMap as a child of given parent at given position and slot.
250
     *
251
     * @param View      $view
252
     * @param WidgetMap $widgetMap
253
     * @param bool      $parent
254
     * @param bool      $position
255
     * @param bool      $slot
256
     *
257
     * @return WidgetMap
258
     */
259
    protected function moveWidgetMap(View $view, WidgetMap $widgetMap, $parent = false, $position = false, $slot = false)
260
    {
261
        if ($widgetMap->getView() !== $view) {
262
            $widgetMap = $this->cloneWidgetMap($widgetMap, $view);
263
        }
264
265
        if ($parent !== false) {
266
            if ($oldParent = $widgetMap->getParent()) {
267
                $oldParent->removeChild($widgetMap);
268
            }
269
            $widgetMap->setParent($parent);
0 ignored issues
show
Documentation introduced by
$parent is of type boolean, but the function expects a null|object<Victoire\Bun...undle\Entity\WidgetMap>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
270
            if ($parent) {
271
                $parent->addChild($widgetMap);
272
            }
273
        }
274
        if ($position !== false) {
275
            $widgetMap->setPosition($position);
0 ignored issues
show
Documentation introduced by
$position is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
276
        }
277
        if ($slot !== false) {
278
            $widgetMap->setSlot($slot);
0 ignored issues
show
Documentation introduced by
$slot is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
279
        }
280
281
        return $widgetMap;
282
    }
283
284
    /**
285
     * Find return all the given WidgetMap children for each view where it's related.
286
     *
287
     * @param WidgetMap $widgetMap
288
     *
289
     * @return mixed
290
     */
291
    protected function getChildrenByView(WidgetMap $widgetMap)
292
    {
293
        $beforeChilds = $widgetMap->getContextualChildren(WidgetMap::POSITION_BEFORE);
294
        $afterChilds = $widgetMap->getContextualChildren(WidgetMap::POSITION_AFTER);
295
296
        $childrenByView['views'] = [];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$childrenByView was never initialized. Although not strictly required by PHP, it is generally a good practice to add $childrenByView = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
297
        $childrenByView['before'] = [];
298
        $childrenByView['after'] = [];
299
        foreach ($beforeChilds as $beforeChild) {
300
            $view = $beforeChild->getView();
301
            $childrenByView['views'][] = $view;
302
            $childrenByView['before'][$view->getId()] = $beforeChild;
303
        }
304
        foreach ($afterChilds as $afterChild) {
305
            $view = $afterChild->getView();
306
            $childrenByView['views'][] = $view;
307
            $childrenByView['after'][$view->getId()] = $afterChild;
308
        }
309
310
        return $childrenByView;
311
    }
312
}
313