Completed
Pull Request — master (#81)
by
unknown
28:56
created

ContainerParameterResolver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 59.68 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 37
loc 62
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getReferenceValue() 0 4 1
B getExpressionLanguage() 37 37 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
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace Kaliop\eZMigrationBundle\Core\ReferenceResolver;
7
8
use eZ\Publish\API\Repository\Values\Content\Content;
9
use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\ExpressionLanguage\ExpressionFunction;
12
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
13
use eZ\Publish\Core\FieldType;
14
15
class ContainerParameterResolver extends AbstractResolver
16
{
17
    protected $referencePrefixes = ['%'];
18
19
    /**
20
     * @var ContainerInterface
21
     */
22
    private $container;
23
24
    public function __construct(ContainerInterface $container)
25
    {
26
        parent::__construct();
27
        $this->container = $container;
28
    }
29
30
    /**
31
     * @param string $stringIdentifier
32
     * @return mixed
33
     */
34
    public function getReferenceValue($stringIdentifier)
35
    {
36
        return $this->container->getParameter(trim($stringIdentifier, '%'));
37
    }
38
39 View Code Duplication
    private function getExpressionLanguage()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
40
    {
41
        {
42
            $language = new ExpressionLanguage();
43
            $language->addFunction(
44
                new ExpressionFunction(
45
                    'relatedContentIds',
46
                    function() {
47
                        throw new \Exception('ref function does not support compilation');
48
                    },
49
                    function($foo, Content $content, $fieldDefinitionIdentifier) {
50
                        $fieldValue = $content->getFieldValue($fieldDefinitionIdentifier);
51
                        if ($fieldValue instanceof FieldType\Relation\Value) {
52
                            return [$fieldValue->destinationContentId];
53
                        } elseif ($fieldValue instanceof FieldType\RelationList\Value) {
54
                            return $fieldValue->destinationContentIds;
55
                        } else {
56
                            throw new \Exception("Expected a Relation or RelationList field value");
57
                        }
58
                    }
59
                )
60
            );
61
            $language->addFunction(
62
                new ExpressionFunction(
63
                    'collection_item',
64
                    function() {
65
                        throw new \Exception('ref function does not support compilation');
66
                    },
67
                    function($foo, $identifier) {
68
                        return $this->referenceResolver->getReferenceValue('reference:collection_item_' . $identifier);
0 ignored issues
show
Bug introduced by
The property referenceResolver 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...
69
                    }
70
                )
71
            );
72
73
            return $language;
74
        }
75
    }
76
}
77