TwigActionTranslationFilter::actionTranslation()   D
last analyzed

Complexity

Conditions 10
Paths 6

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 4.8196
cc 10
eloc 20
nc 6
nop 1

How to fix   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
/*
4
 * This file is part of the Admin Bundle.
5
 *
6
 * Copyright (c) 2015-2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\CMSKernel\Infrastructure\Lin3sAdminBundle\Twig;
13
14
use Symfony\Component\Translation\TranslatorInterface;
15
16
/**
17
 * Twig action translation filter.
18
 *
19
 * @author Beñat Espiña <[email protected]>
20
 */
21
class TwigActionTranslationFilter extends \Twig_Extension
22
{
23
    /**
24
     * The translator.
25
     *
26
     * @var TranslatorInterface
27
     */
28
    private $translator;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param TranslatorInterface $translator The translator
34
     */
35
    public function __construct(TranslatorInterface $translator)
36
    {
37
        $this->translator = $translator;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getFilters()
44
    {
45
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array('action_tra...'actionTranslation'))); (array<string,Twig_SimpleFilter>) is incompatible with the return type declared by the interface Twig_ExtensionInterface::getFilters of type Twig_Filter[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
46
            'action_translation' => new \Twig_SimpleFilter('action_translation', [$this, 'actionTranslation']),
47
        ];
48
    }
49
50
    /**
51
     * Callback of action translation Twig filter that returns resultant array.
52
     *
53
     * @param string $options The action options
54
     *
55
     * @return array|string
56
     */
57
    public function actionTranslation($options)
58
    {
59
        if (!is_array($options)) {
60
            return $this->translator->trans($options, [], 'CmsKernelAdminBridge');
61
        }
62
63
        $resultOptions = [];
64
        foreach ($options as $optionKey => $option) {
65
            $option = null === json_decode($option, true) ? $option : json_decode($option, true);
66
67
            if (is_array($option)) {
68
                foreach ($option as $iterationKey => $iteration) {
69
                    if (is_array($iteration)) {
70
                        foreach ($iteration as $iKey => $i) {
71
                            if (is_array($i)) {
72
                                foreach ($i as $iiKey => $ii) {
73
                                    $resultOptions[$optionKey][$iterationKey][$iKey][$iiKey] = $this->translator->trans($ii, [], 'CmsKernelAdminBridge');
74
                                }
75
                            } else {
76
                                $resultOptions[$optionKey][$iterationKey][$iKey] = $this->translator->trans($i, [], 'CmsKernelAdminBridge');
77
                            }
78
                        }
79
                    } else {
80
                        $resultOptions[$optionKey][$iterationKey] = $this->translator->trans($iteration, [], 'CmsKernelAdminBridge');
81
                    }
82
                }
83
            } else {
84
                $resultOptions[$optionKey] = $this->translator->trans($option, [], 'CmsKernelAdminBridge');
85
            }
86
        }
87
88
        return $resultOptions;
89
    }
90
}
91