WidgetMapOverwriteValidationCommand::execute()   C
last analyzed

Complexity

Conditions 12
Paths 30

Size

Total Lines 69
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 5.7089
c 0
b 0
f 0
cc 12
eloc 46
nc 30
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Victoire\Bundle\WidgetMapBundle\Command;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Helper\Table;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Victoire\Bundle\CoreBundle\Entity\View;
11
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
12
13
class WidgetMapOverwriteValidationCommand extends ContainerAwareCommand
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function configure()
19
    {
20
        parent::configure();
21
22
        $this
23
            ->setName('victoire:widget-map:validate-overwrite')
24
            ->setDescription('Check for each View if there is conflicts between WidgetMaps.');
25
    }
26
27
    /**
28
     * @param InputInterface  $input
29
     * @param OutputInterface $output
30
     *
31
     * @throws \Exception
32
     *
33
     * @return bool
34
     */
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $table = new Table($output);
38
        $table->setHeaders([
39
            'Contextual View',
40
            'WidgetMaps in conflict',
41
            'Parent WidgetMap',
42
            'Slot',
43
            'Position',
44
        ]);
45
46
        /** @var EntityManager $entityManager */
47
        $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager');
48
        $contextualViewWarmer = $this->getContainer()->get('victoire_widget_map.contextual_view_warmer');
49
50
        $conflictNb = 0;
51
52
        $views = $entityManager->getRepository('Victoire\Bundle\CoreBundle\Entity\View')->findAll();
53
        foreach ($views as $view) {
54
            $widgetMaps = $contextualViewWarmer->warm($view);
55
56
            foreach ($widgetMaps as $widgetMap) {
57
                $positions = [WidgetMap::POSITION_BEFORE, WidgetMap::POSITION_AFTER];
58
                $children = [];
59
                foreach ($positions as $position) {
60
                    $children[$position] = null;
61
                    $matchingChildren = [];
62
63
                    foreach ($widgetMap->getContextualChildren($position) as $_child) {
64
                        if (null === $_child->getSubstituteForView($view)) {
65
                            $children[$position] = $_child;
66
                            $matchingChildren[] = $_child->getId();
67
                            $parent = $_child->getParent()->getId();
68
                            $slot = $_child->getSlot();
69
                        }
70
                    }
71
72
                    if (!$children[$position] && $widgetMap->getReplaced()) {
73
                        foreach ($widgetMap->getReplaced()->getContextualChildren($position) as $_child) {
74
                            if (null === $_child->getSubstituteForView($view)) {
75
                                $matchingChildren[] = $_child->getId();
76
                                $parent = $_child->getParent()->getId();
77
                                $slot = $_child->getSlot();
78
                            }
79
                        }
80
                    }
81
82
                    if (count($matchingChildren) > 1) {
83
                        $conflictNb++;
84
                        $table->addRow([
85
                            $view->getId(),
86
                            implode(', ', $matchingChildren),
87
                            $parent,
0 ignored issues
show
Bug introduced by
The variable $parent does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
88
                            $slot,
0 ignored issues
show
Bug introduced by
The variable $slot does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
89
                            $position,
90
                        ]);
91
                    }
92
                }
93
            }
94
        }
95
96
        if ($conflictNb > 0) {
97
            $output->writeln(sprintf('<error>%s WidgetMaps conflicts found.</error>', $conflictNb));
98
            $output->writeln('<error>At least one of those WidgetMaps must have a replace_id with action "overwrite".</error>');
99
            $table->render();
100
        } else {
101
            $output->writeln('<info>No conflict found.</info>');
102
        }
103
    }
104
}
105