Completed
Pull Request — master (#341)
by De Cramer
05:04
created

FmlComponent   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 11.27 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 8
dl 8
loc 71
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
C display() 8 46 7

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 eXpansion\Framework\MlComposeBundle\UiComponents;
4
5
use eXpansion\Framework\Core\Model\Gui\ManialinkInterface;
6
use eXpansion\Framework\Core\Plugins\Gui\ActionFactory;
7
use FML\Controls\Control;
8
use FML\Controls\Frame;
9
use oliverde8\AssociativeArraySimplified\AssociativeArray;
10
use Oliverde8\PageCompose\Block\BlockDefinitionInterface;
11
use Oliverde8\PageCompose\Service\UiComponents;
12
use Oliverde8\PageCompose\UiComponent\AbstractUiComponent;
13
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
14
use Symfony\Component\PropertyAccess\PropertyAccess;
15
16
/**
17
 * Class BaseFmlComponent
18
 *
19
 * @author    de Cramer Oliver<[email protected]>
20
 * @copyright 2018 Oliverde8
21
 * @package eXpansion\Framework\MlComposeBundle\UiComponents
22
 */
23
class  FmlComponent extends AbstractUiComponent
0 ignored issues
show
Coding Style introduced by
Expected 1 space between class keyword and class name; 2 found
Loading history...
24
{
25
    /**
26
     * @var ActionFactory
27
     */
28
    protected $actionFactory;
29
    protected $fmlClass;
30
31
    public function __construct(UiComponents $uiComponents, ActionFactory $actionFactory, $fmlClass)
32
    {
33
        parent::__construct($uiComponents);
34
        $this->propertAccess = PropertyAccess::createPropertyAccessor();
0 ignored issues
show
Bug introduced by
The property propertAccess does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
        $this->actionFactory = $actionFactory;
36
        $this->fmlClass = $fmlClass;
37
    }
38
39
    /**
40
     * Display the component.
41
     *
42
     * @param BlockDefinitionInterface $blockDefinition
43
     * @param array ...$args
44
     *
45
     * @return string
46
     */
47
    public function display(BlockDefinitionInterface $blockDefinition, ...$args)
48
    {
49
        $expressionLanguage = new ExpressionLanguage();
50
        $configuration = new AssociativeArray($blockDefinition->getConfiguration());
51
        $class = $this->fmlClass;
52
53
        $arguments = [
54
            'factory' => $args[0],
55
            'manialink' => $args[1],
56
            'args' => $args,
57
        ];
58
59
        /** @var Control  $component */
60
        $component = new $class($configuration->get('id', $blockDefinition->getUniqueKey()));
61
62
        foreach ($blockDefinition->getSubBlocks() as $block) {
63
            /** @var Frame  $component */
64
            $component->addChild($this->uiComponents->display($block, ...$args));
0 ignored issues
show
Documentation introduced by
$this->uiComponents->display($block, ...$args) is of type null|string, but the function expects a object<FML\Types\Renderable>.

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...
65
        }
66
67
        foreach ($configuration->get('expr', []) as $key => $value) {
68
            $function = ucwords("set$key");
69
            $component->$function($expressionLanguage->evaluate($value, $arguments));
70
        }
71
72 View Code Duplication
        foreach ($configuration->get('args', []) as $key => $value) {
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...
73
            $function = ucwords("set$key");
74
            $component->$function(...$value);
75
        }
76
77 View Code Duplication
        foreach ($configuration->get('def', []) as $key => $value) {
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...
78
            $function = ucwords("set$key");
79
            $component->$function($value);
80
        }
81
82
        if ($args[1] instanceof ManialinkInterface) {
0 ignored issues
show
Bug introduced by
The class eXpansion\Framework\Core...\Gui\ManialinkInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
83
            if ($configuration->get('action')) {
84
                $callback = [$configuration->get('action/0/service', $args[0]), $configuration->get('action/0/method')];
85
                $this->actionFactory->createManialinkAction($args[1], $callback, [], false);
86
            }
87
        } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
88
            // Log warning!
89
        }
90
91
        return $component;
92
    }
93
}
94