Completed
Pull Request — master (#338)
by Leny
07:33
created

ViewTranslationManager::cloneView()   C

Complexity

Conditions 12
Paths 4

Size

Total Lines 66
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 66
rs 5.9123
cc 12
eloc 48
nc 4
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\I18nBundle\Manager;
4
5
use Doctrine\Orm\EntityManager;
6
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate;
7
use Victoire\Bundle\CoreBundle\Entity\View;
8
use Victoire\Bundle\PageBundle\Entity\BasePage;
9
10
/**
11
 * Page helper
12
 * ref: victoire_i18n.view_translation_manager.
13
 */
14
class ViewTranslationManager
15
{
16
    protected $parameterConverter;
17
    protected $businessEntityHelper;
18
    protected $em;
19
    protected $viewReferenceBuilder;
20
    protected $viewReferenceHelper;
21
    protected $viewReferenceProvider;
22
23
    /**
24
     * Constructor.
25
     *
26
     * @param EntityManager $entityManager
27
     */
28
    public function __construct(EntityManager $entityManager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
29
    {
30
        $this->em = $entityManager;
31
    }
32
33
    /**
34
     * this methods allow you to add a translation to any view
35
     * recursively to its subview.
36
     *
37
     * @param View $view, the view to translatate
0 ignored issues
show
Bug introduced by
There is no parameter named $view,. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
38
     * @param $templatename the new name of the view
39
     * @param $loopindex the current loop of iteration in recursion
40
     * @param $locale the target locale to translate view
41
     *
42
     * @return View
43
     */
44
    public function addTranslation(View $view, $viewName, $locale)
45
    {
46
        $template = null;
47
        if ($view->getTemplate()) {
48
            $template = $view->getTemplate();
49
            if ($template->getI18n()->getTranslation($locale)) {
0 ignored issues
show
Bug introduced by
The method getI18n cannot be called on $template (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
50
                $template = $template->getI18n()->getTranslation($locale);
0 ignored issues
show
Bug introduced by
The method getI18n cannot be called on $template (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
51
            } else {
52
                $templateName = $template->getName().'-'.$locale;
0 ignored issues
show
Bug introduced by
The method getName cannot be called on $template (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
53
                $this->em->refresh($view);
54
                $template = $this->addTranslation($template, $templateName, $locale);
0 ignored issues
show
Documentation introduced by
$template is of type string, but the function expects a object<Victoire\Bundle\CoreBundle\Entity\View>.

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...
55
            }
56
        }
57
        $view->setLocale($locale);
58
        $view->setTemplate($template);
59
        $clonedView = $this->cloneView($view, $viewName, $locale);
0 ignored issues
show
Unused Code introduced by
The call to ViewTranslationManager::cloneView() has too many arguments starting with $locale.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
60
        if ($clonedView instanceof BasePage && $view->getTemplate()) {
61
            $template->addPage($clonedView);
62
        }
63
        $i18n = $view->getI18n();
64
        $i18n->setTranslation($locale, $clonedView);
0 ignored issues
show
Bug introduced by
The method setTranslation cannot be called on $i18n (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
65
        $this->em->persist($clonedView);
66
        $this->em->refresh($view);
67
        $this->em->flush();
68
69
        return $clonedView;
70
    }
71
72
    /**
73
     * this methods allows you to clone a view and its widgets and also the widgetmap.
74
     *
75
     * @param View $view
76
     * @param $templateName
77
     *
78
     * @return View
79
     */
80
    public function cloneView(View $view, $templateName = null)
81
    {
82
        $clonedView = clone $view;
83
        $this->em->refresh($view);
84
        $clonedView->setSlug(null);
85
        $widgetMapClone = $clonedView->getWidgetMaps();
86
        $arrayMapOfWidgetMap = [];
87
        if (null !== $templateName) {
88
            $clonedView->setName($templateName);
89
        }
90
91
        $clonedView->setId(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Victoire\Bundle\CoreBundle\Entity\id>.

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...
92
        $this->em->persist($clonedView);
93
94
        if (!$clonedView instanceof BusinessTemplate) {
95
            $widgetLayoutSlots = [];
96
            $newWidgets = [];
97
            foreach ($clonedView->getWidgets() as $widgetKey => $widgetVal) {
0 ignored issues
show
Bug introduced by
The expression $clonedView->getWidgets() of type string is not traversable.
Loading history...
Deprecated Code introduced by
The method Victoire\Bundle\CoreBund...tity\View::getWidgets() has been deprecated with message: Get widgets.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
98
                $clonedWidget = clone $widgetVal;
99
                $clonedWidget->setId(null);
100
                $clonedWidget->setView($clonedView);
101
                $this->em->persist($clonedWidget);
102
                $newWidgets[] = $clonedWidget;
103
                $arrayMapOfWidgetMap[$widgetVal->getId()] = $clonedWidget;
104
                if ($widgetVal instanceof WidgetLayout) {
0 ignored issues
show
Bug introduced by
The class Victoire\Bundle\I18nBundle\Manager\WidgetLayout 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...
105
                    $id = $widgetVal->getId();
106
                    $widgetLayoutSlots[$id] = $clonedWidget;
107
                }
108
            }
109
            $clonedView->setWidgets($newWidgets);
0 ignored issues
show
Bug introduced by
The method setWidgets() does not seem to exist on object<Victoire\Bundle\CoreBundle\Entity\View>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
            $this->em->persist($clonedView);
111
            $this->em->flush();
112
            $widgetSlotMap = [];
113
            foreach ($widgetLayoutSlots as $_id => $_widget) {
114
                foreach ($clonedView->getWidgets() as $_clonedWidget) {
0 ignored issues
show
Bug introduced by
The expression $clonedView->getWidgets() of type string is not traversable.
Loading history...
Deprecated Code introduced by
The method Victoire\Bundle\CoreBund...tity\View::getWidgets() has been deprecated with message: Get widgets.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
115
                    if (preg_match('/^'.$_id.'_(.)/', $_clonedWidget->getSlot(), $matches)) {
116
                        $newSlot = $_widget->getId().'_'.$matches[1];
117
                        $oldSlot = $_clonedWidget->getSlot();
118
                        $_clonedWidget->setSlot($newSlot);
119
                        $widgetSlotMap[$oldSlot] = $newSlot;
120
                    }
121
                }
122
            }
123
124
            $this->em->flush();
125
            foreach ($widgetMapClone as $wigetSlotCloneKey => $widgetSlotCloneVal) {
126
                foreach ($widgetSlotCloneVal as $widgetMapItemKey => $widgetMapItemVal) {
127
                    if (isset($arrayMapOfWidgetMap[$widgetMapItemVal['widgetId']])) {
128
                        $widgetId = $arrayMapOfWidgetMap[$widgetMapItemVal['widgetId']]->getId();
129
                        $widgetMapItemVal['widgetId'] = $widgetId;
130
                        if (array_key_exists($wigetSlotCloneKey, $widgetSlotMap)) {
131
                            $wigetSlotCloneKey = $widgetSlotMap[$wigetSlotCloneKey];
132
                        }
133
                        $widgetMapClone[$wigetSlotCloneKey][$widgetMapItemKey] = $widgetMapItemVal;
134
                    }
135
                }
136
            }
137
138
            $clonedView->setSlots([]);
0 ignored issues
show
Bug introduced by
The method setSlots() does not seem to exist on object<Victoire\Bundle\CoreBundle\Entity\View>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
            $clonedView->setWidgetMap($widgetMapClone);
0 ignored issues
show
Bug introduced by
The method setWidgetMap() does not exist on Victoire\Bundle\CoreBundle\Entity\View. Did you maybe mean setWidgetMaps()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
140
            $this->em->persist($clonedView);
141
            $this->em->flush();
142
        }
143
144
        return $clonedView;
145
    }
146
}
147