WidgetMapChildrenResolver::getChildren()   D
last analyzed

Complexity

Conditions 9
Paths 13

Size

Total Lines 44
Code Lines 22

Duplication

Lines 14
Ratio 31.82 %

Importance

Changes 0
Metric Value
dl 14
loc 44
rs 4.909
c 0
b 0
f 0
cc 9
eloc 22
nc 13
nop 2
1
<?php
2
3
namespace Victoire\Bundle\WidgetMapBundle\Resolver;
4
5
use Symfony\Bridge\Monolog\Logger;
6
use Victoire\Bundle\CoreBundle\Entity\View;
7
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
8
9
class WidgetMapChildrenResolver
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
10
{
11
    private $logger;
12
13
    /**
14
     * WidgetMapChildrenResolver constructor.
15
     *
16
     * @param Logger $logger
17
     */
18
    public function __construct(Logger $logger)
19
    {
20
        $this->logger = $logger;
21
    }
22
23
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$widgetMap" missing
Loading history...
introduced by
Doc comment for parameter "$view" missing
Loading history...
24
     * Return "after" and "before" children,
25
     * based on contextual View and its Templates.
26
     *
27
     * @return array
28
     */
29
    public function getChildren(WidgetMap $widgetMap, View $view = null)
30
    {
31
        $positions = [WidgetMap::POSITION_BEFORE, WidgetMap::POSITION_AFTER];
32
        $children = [];
33
        foreach ($positions as $position) {
34
            $matchingChildren = [];
35
36
            //Position is null by default
37
            $children[$position] = null;
38
39
            //Pass through all current WidgetMap children for a given position
40 View Code Duplication
            foreach ($widgetMap->getContextualChildren($position) as $_child) {
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...
41
                //If child don't have a substitute for this View and Templates, this is the one
42
                if (null === $_child->getSubstituteForView($view)) {
0 ignored issues
show
Bug introduced by
It seems like $view defined by parameter $view on line 29 can be null; however, Victoire\Bundle\WidgetMa...:getSubstituteForView() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
43
                    $children[$position] = $_child;
44
                    $matchingChildren[] = $_child->getId();
45
                }
46
            }
47
48
            //If children has not been found for this position
49
            //and current WidgetMap is a substitute
50
            if (!$children[$position] && $widgetMap->getReplaced()) {
51
                //Pass through all replaced WidgetMap children for a given position
52 View Code Duplication
                foreach ($widgetMap->getReplaced()->getContextualChildren($position) as $_child) {
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...
53
                    //If child don't have a substitute for this View and Templates, this is the one
54
                    if (null === $_child->getSubstituteForView($view)) {
0 ignored issues
show
Bug introduced by
It seems like $view defined by parameter $view on line 29 can be null; however, Victoire\Bundle\WidgetMa...:getSubstituteForView() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
55
                        $children[$position] = $_child;
56
                        $matchingChildren[] = $_child->getId();
57
                    }
58
                }
59
            }
60
61
            $matchingChildren = array_unique($matchingChildren);
62
            if (count($matchingChildren) > 1) {
63
                $this->logger->critical(sprintf(
64
                    'Conflict found between WidgetMaps %s for View %s',
65
                    implode(', ', $matchingChildren),
66
                    $view->getId()
0 ignored issues
show
Bug introduced by
It seems like $view is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
67
                ));
68
            }
69
        }
70
71
        return $children;
72
    }
73
74
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$widgetMap" missing
Loading history...
introduced by
Doc comment for parameter "$position" missing
Loading history...
75
     * @param $position
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
76
     * @param View|null $view
0 ignored issues
show
introduced by
Doc comment for parameter $view does not match actual variable name $position
Loading history...
77
     *
78
     * @return bool
79
     */
80
    public function hasChildren(WidgetMap $widgetMap, $position, View $view = null)
81
    {
82
        foreach ($this->getChildren($widgetMap, $view) as $child) {
83
            if ($child && $child->getPosition() === $position) {
84
                return true;
85
            }
86
        }
87
88
        return false;
89
    }
90
}
91