ViewReferenceHelper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 6.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 5
loc 75
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C generateViewReferenceId() 5 27 7
B buildViewReferenceRecursively() 0 20 6

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\ViewReferenceBundle\Helper;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Component\PropertyAccess\PropertyAccessor;
7
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
8
use Victoire\Bundle\CoreBundle\Entity\View;
9
use Victoire\Bundle\CoreBundle\Entity\WebViewInterface;
10
use Victoire\Bundle\I18nBundle\Entity\ViewTranslation;
11
use Victoire\Bundle\ViewReferenceBundle\Builder\ViewReferenceBuilder;
12
13
/**
14
 * ref: victoire_view_reference.helper.
15
 */
16
class ViewReferenceHelper
17
{
18
    /**
19
     * @var ViewReferenceBuilder
20
     */
21
    private $viewReferenceBuilder;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param ViewReferenceBuilder $viewReferenceBuilder
27
     */
28
    public function __construct(ViewReferenceBuilder $viewReferenceBuilder)
29
    {
30
        $this->viewReferenceBuilder = $viewReferenceBuilder;
31
    }
32
33
    /**
34
     * @param View  $view
35
     * @param mixed $entityId
36
     *
37
     * @return string
38
     */
39
    public static function generateViewReferenceId(View $view, $entityId = null)
40
    {
41
        $id = $view->getId();
42
        if ($view instanceof BusinessPage && $view->getEntity()) {
43
            $id = $view->getTemplate()->getId();
44
            $accessor = new PropertyAccessor();
45
            $entity = $view->getEntity();
46 View Code Duplication
            if (method_exists($entity, 'getId')) {
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...
47
                $entityId = $entity->getId();
48
            } else {
49
                $entityId = $accessor->getValue($entity, $view->getBusinessEntity()->getBusinessIdentifiers()->first()->getName());
50
            }
51
        } elseif (!$view instanceof WebViewInterface) {
52
            return $view->getId();
53
        }
54
55
        $refId = sprintf('ref_%s', $id);
56
        if ($entityId) {
57
            $refId .= '_'.$entityId;
58
        }
59
        /** @var string $currentLocale */
60
        if ('' != $currentLocale = $view->getCurrentLocale()) {
61
            $refId .= '_'.$currentLocale;
62
        }
63
64
        return $refId;
65
    }
66
67
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$entityManager" missing
Loading history...
introduced by
Doc comment for parameter "$isRoot" missing
Loading history...
68
     * @param WebViewInterface[] $tree
69
     */
70
    public function buildViewReferenceRecursively($tree, EntityManager $entityManager, $isRoot = true)
71
    {
72
        foreach ($tree as $branch) {
73
            /** @var View $view */
74
            $view = $branch['view'];
75
            /** @var ViewTranslation $translation */
76
            foreach ($view->getTranslations() as $translation) {
77
                if (true === $isRoot || $translation->getLocale() == $view->getParent()->getCurrentLocale()) {
78
                    $view->setCurrentLocale($translation->getLocale());
79
                    $viewReference = $this->viewReferenceBuilder->buildViewReference($view, $entityManager);
80
                    if (!empty($branch['children'])) {
81
                        /** @var WebViewInterface $children */
82
                        $children = $branch['children'];
83
                        $this->buildViewReferenceRecursively($children, $entityManager, false);
0 ignored issues
show
Documentation introduced by
$children is of type object<Victoire\Bundle\C...ntity\WebViewInterface>, but the function expects a array<integer,object<Vic...tity\WebViewInterface>>.

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...
84
                    }
85
                    $view->setReference($viewReference, $translation->getLocale());
86
                }
87
            }
88
        }
89
    }
90
}
91