Completed
Push — master ( 2c86a7...690ed0 )
by
unknown
36:26 queued 28:50
created

WidgetMapManager   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 286
Duplicated Lines 2.1 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 39
lcom 1
cbo 7
dl 6
loc 286
rs 8.2857
c 5
b 1
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B insert() 0 32 3
D move() 6 35 9
C delete() 0 43 7
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   

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:

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
12
class WidgetMapManager
13
{
14
    private $em;
15
    private $builder;
16
17
    public function __construct(EntityManager $em, WidgetMapBuilder $builder)
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...
18
    {
19
        $this->em = $em;
20
        $this->builder = $builder;
21
    }
22
23
    /**
24
     * Insert a WidgetMap in a view at given position.
25
     *
26
     * @param string $slotId
27
     */
28
    public function insert(Widget $widget, View $view, $slotId, $position, $widgetReference)
29
    {
30
        $quantum = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->findOneBy([
31
            'view'     => $view,
32
            'slot'     => $slotId,
33
            'position' => $position,
34
            'parent'   => $widgetReference,
35
            'action' => [
36
                WidgetMap::ACTION_CREATE,
37
                WidgetMap::ACTION_OVERWRITE
38
            ]
39
        ]);
40
41
        if ($quantum) {
42
            $widget->setWidgetMap($quantum);
43
            $view->addWidgetMap($quantum);
44
        } else {
45
            $parent = null;
46
            if ($widgetReference) {
47
                $parent = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find($widgetReference);
48
            }
49
            //create the new widget map
50
            $widgetMapEntry = new WidgetMap();
51
            $widgetMapEntry->setAction(WidgetMap::ACTION_CREATE);
52
            $widgetMapEntry->setSlot($slotId);
53
            $widgetMapEntry->setPosition($position);
54
            $widgetMapEntry->setParent($parent);
0 ignored issues
show
Bug introduced by
It seems like $parent defined by $this->em->getRepository...>find($widgetReference) on line 47 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...
55
            $widget->setWidgetMap($widgetMapEntry);
56
57
            $view->addWidgetMap($widgetMapEntry);
58
        }
59
    }
60
61
    /**
62
     * moves a widget in a view.
63
     *
64
     * @param View  $view
65
     * @param array $sortedWidget
66
     *
67
     * @return void
68
     */
69
    public function move(View $view, $sortedWidget)
70
    {
71
        /** @var WidgetMap $parentWidgetMap */
72
        $parentWidgetMap = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find((int) $sortedWidget['parentWidgetMap']);
73
        $position = $sortedWidget['position'];
74
        $slot = $sortedWidget['slot'];
75
        /** @var WidgetMap $widgetMap */
76
        $widgetMap = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find((int) $sortedWidget['widgetMap']);
77
78
        $originalParent = $widgetMap->getParent();
79
        $originalPosition = $widgetMap->getPosition();
80
81
        $children = $widgetMap->getChildren($view);
82
        $beforeChild = !empty($children[WidgetMap::POSITION_BEFORE]) ? $children[WidgetMap::POSITION_BEFORE] : null;
83
        $afterChild = !empty($children[WidgetMap::POSITION_AFTER]) ? $children[WidgetMap::POSITION_AFTER] : null;
84
85
        $parentWidgetMapChildren = $this->getChildrenByView($parentWidgetMap);
86
87
        $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...
88
89
        // If the moved widgetMap has someone at both his before and after, arbitrary move UP the before side
90
        // and find the first place after the before widgetMap hierarchy to place the after widgetMap.
91
        $this->moveChildren($view, $beforeChild, $afterChild, $originalParent, $originalPosition);
92
93
        foreach ($parentWidgetMapChildren['views'] as $_view) {
94
            if ($_view !== $view) {
95 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...
96
                    $parentWidgetMapChildren['before'][$_view->getId()]->setParent($widgetMap);
97
                }
98 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...
99
                    $parentWidgetMapChildren['after'][$_view->getId()]->setParent($widgetMap);
100
                }
101
            }
102
        }
103
    }
104
105
    /**
106
     * Delete the widget from the view.
107
     *
108
     * @param View   $view
109
     * @param Widget $widget
110
     *
111
     * @throws \Exception Widget map does not exists
112
     */
113
    public function delete(View $view, Widget $widget)
114
    {
115
        $this->builder->build($view);
116
117
        $widgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($widget, $view);
118
        $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...
119
120
        $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...
121
        $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...
122
123
        $children = $widgetMap->getChildren($view);
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...
124
        $beforeChild = !empty($children[WidgetMap::POSITION_BEFORE]) ? $children[WidgetMap::POSITION_BEFORE] : null;
125
        $afterChild = !empty($children[WidgetMap::POSITION_AFTER]) ? $children[WidgetMap::POSITION_AFTER] : null;
126
127
        //we remove the widget from the current view
128
        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...
129
            // If the widgetMap has substitutes, delete them or transform them in create mode
130
            if (count($widgetMap->getSubstitutes()) > 0) {
131
                foreach ($widgetMap->getSubstitutes() as $substitute) {
0 ignored issues
show
Bug introduced by
The method getSubstitutes 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...
132
                    if ($substitute->getAction() === WidgetMap::ACTION_OVERWRITE) {
133
                        $substitute->setAction(WidgetMap::ACTION_CREATE);
134
                        $substitute->setReplaced(null);
135
                    } else {
136
                        $view->removeWidgetMap($widgetMap);
137
                    }
138
                }
139
            }
140
            //remove the widget map from the slot
141
            $view->removeWidgetMap($widgetMap);
142
        } else {
143
            //the widget is owned by another view (a parent)
144
            //so we add a new widget map that indicates we delete this widget
145
            $replaceWidgetMap = new WidgetMap();
146
            $replaceWidgetMap->setAction(WidgetMap::ACTION_DELETE);
147
            $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...
148
            $replaceWidgetMap->setSlot($slot);
149
            $replaceWidgetMap->setReplaced($widgetMap);
150
151
            $view->addWidgetMap($replaceWidgetMap);
152
        }
153
154
        $this->moveChildren($view, $beforeChild, $afterChild, $originalParent, $originalPosition);
155
    }
156
157
    /**
158
     * the widget is owned by another view (a parent)
159
     * so we add a new widget map that indicates we delete this widget.
160
     *
161
     * @param View      $view
162
     * @param WidgetMap $originalWidgetMap
163
     * @param Widget    $widgetCopy
164
     *
165
     * @throws \Exception
166
     */
167
    public function overwrite(View $view, WidgetMap $originalWidgetMap, Widget $widgetCopy)
168
    {
169
        $widgetMap = new WidgetMap();
170
        $widgetMap->setAction(WidgetMap::ACTION_OVERWRITE);
171
        $widgetMap->setReplaced($originalWidgetMap);
172
        $widgetCopy->setWidgetMap($widgetMap);
173
        $widgetMap->setSlot($originalWidgetMap->getSlot());
174
        $widgetMap->setPosition($originalWidgetMap->getPosition());
175
        $widgetMap->setParent($originalWidgetMap->getParent());
176
177
        $view->addWidgetMap($widgetMap);
178
    }
179
180
    /**
181
     * If the moved widgetMap has someone at both his before and after, arbitrary move UP the before side
182
     * and find the first place after the before widgetMap hierarchy to place the after widgetMap.
183
     *
184
     * @param View $view
185
     * @param $beforeChild
186
     * @param $afterChild
187
     * @param $originalParent
188
     * @param $originalPosition
189
     */
190
    public function moveChildren(View $view, $beforeChild, $afterChild, $originalParent, $originalPosition)
191
    {
192
        if ($beforeChild && $afterChild) {
193
            $this->moveWidgetMap($view, $beforeChild, $originalParent, $originalPosition);
194
195
            $child = $beforeChild;
196
            while ($child->getChild(WidgetMap::POSITION_AFTER)) {
197
                $child = $child->getChild(WidgetMap::POSITION_AFTER);
198
            }
199
            if ($afterChild->getId() !== $child->getId()) {
200
                $this->moveWidgetMap($view, $afterChild, $child);
201
            }
202
        } elseif ($beforeChild) {
203
            $this->moveWidgetMap($view, $beforeChild, $originalParent, $originalPosition);
204
        } elseif ($afterChild) {
205
            $this->moveWidgetMap($view, $afterChild, $originalParent, $originalPosition);
206
        }
207
    }
208
209
    /**
210
     * Create a copy of a WidgetMap in "overwrite" mode and insert it in the given view.
211
     *
212
     * @param WidgetMap $widgetMap
213
     * @param View      $view
214
     *
215
     * @throws \Exception
216
     *
217
     * @return WidgetMap
218
     */
219
    protected function cloneWidgetMap(WidgetMap $widgetMap, View $view)
220
    {
221
        $originalWidgetMap = $widgetMap;
222
        $widgetMap = clone $widgetMap;
223
        $widgetMap->setId(null);
224
        $widgetMap->setAction(WidgetMap::ACTION_OVERWRITE);
225
        $widgetMap->setReplaced($originalWidgetMap);
226
        $widgetMap->setView($view);
227
        $view->addWidgetMap($widgetMap);
228
        $this->em->persist($widgetMap);
229
230
        return $widgetMap;
231
    }
232
233
    /**
234
     * Move given WidgetMap as a child of given parent at given position and slot.
235
     *
236
     * @param View      $view
237
     * @param WidgetMap $widgetMap
238
     * @param bool      $parent
239
     * @param bool      $position
240
     * @param bool      $slot
241
     *
242
     * @return WidgetMap
243
     */
244
    protected function moveWidgetMap(View $view, WidgetMap $widgetMap, $parent = false, $position = false, $slot = false)
245
    {
246
        if ($widgetMap->getView() !== $view) {
247
            $widgetMap = $this->cloneWidgetMap($widgetMap, $view);
248
        }
249
250
        if ($parent !== false) {
251
            if ($oldParent = $widgetMap->getParent()) {
252
                $oldParent->removeChild($widgetMap);
253
            }
254
            $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...
255
            if ($parent) {
256
                $parent->addChild($widgetMap);
0 ignored issues
show
Bug introduced by
The method addChild cannot be called on $parent (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
257
            }
258
        }
259
        if ($position !== false) {
260
            $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...
261
        }
262
        if ($slot !== false) {
263
            $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...
264
        }
265
266
        return $widgetMap;
267
    }
268
269
    /**
270
     * Find return all the given WidgetMap children for each view where it's related.
271
     *
272
     * @param WidgetMap $widgetMap
273
     *
274
     * @return mixed
275
     */
276
    protected function getChildrenByView(WidgetMap $widgetMap)
277
    {
278
        $beforeChilds = $widgetMap->getChilds(WidgetMap::POSITION_BEFORE);
279
        $afterChilds = $widgetMap->getChilds(WidgetMap::POSITION_AFTER);
280
281
        $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...
282
        $childrenByView['before'] = [];
283
        $childrenByView['after'] = [];
284
        foreach ($beforeChilds as $beforeChild) {
285
            $view = $beforeChild->getView();
286
            $childrenByView['views'][] = $view;
287
            $childrenByView['before'][$view->getId()] = $beforeChild;
288
        }
289
        foreach ($afterChilds as $afterChild) {
290
            $view = $afterChild->getView();
291
            $childrenByView['views'][] = $view;
292
            $childrenByView['after'][$view->getId()] = $afterChild;
293
        }
294
295
        return $childrenByView;
296
    }
297
}
298