Completed
Branch feature/moar-test-optimizing (8cffd1)
by Lucas
19:31 queued 13:44
created

TranslatableFieldsCompilerPass   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 29.87 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 97.62%
Metric Value
wmc 10
lcom 1
cbo 6
dl 23
loc 77
ccs 41
cts 42
cp 0.9762
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 8 8 2
C getTranslatableFields() 15 37 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
 * build a list of all services that have translatable mappings
4
 *
5
 * this can be used by whoever needs to know where translatables are..
6
 */
7
8
namespace Graviton\DocumentBundle\DependencyInjection\Compiler;
9
10
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\DocumentMap;
11
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Document;
12
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedMany;
13
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedOne;
14
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Field;
15
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16
use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18
/**
19
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
20
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
21
 * @link     http://swisscom.ch
22
 */
23
class TranslatableFieldsCompilerPass implements CompilerPassInterface
24
{
25
    /**
26
     * @var DocumentMap
27
     */
28
    private $documentMap;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param DocumentMap $documentMap Document map
34
     */
35 2
    public function __construct(DocumentMap $documentMap)
36
    {
37 2
        $this->documentMap = $documentMap;
38 2
    }
39
40
    /**
41
     * Make translatable fields map and set it to parameter
42
     *
43
     * @param ContainerBuilder $container container builder
44
     * @return void
45
     */
46 2 View Code Duplication
    public function process(ContainerBuilder $container)
0 ignored issues
show
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...
47
    {
48 2
        $map = [];
49 2
        foreach ($this->documentMap->getDocuments() as $document) {
50 2
            $map[$document->getClass()] = $this->getTranslatableFields($document);
51 1
        }
52 2
        $container->setParameter('graviton.document.type.translatable.fields', $map);
53 2
    }
54
55
    /**
56
     * Get document fields
57
     *
58
     * @param Document $document Document
59
     * @param string   $prefix   Field prefix
60
     * @return array
61
     */
62 2
    private function getTranslatableFields(Document $document, $prefix = '')
63
    {
64 2
        $reflection = new \ReflectionClass($document->getClass());
65 2 View Code Duplication
        if ($reflection->implementsInterface('Graviton\I18nBundle\Document\TranslatableDocumentInterface')) {
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...
66 2
            $instance = $reflection->newInstanceWithoutConstructor();
67 2
            $translatableFields = $instance->getTranslatableFields();
68 1
        } else {
69
            $translatableFields = [];
70
        }
71
72 2
        $result = [];
73 2
        foreach ($document->getFields() as $field) {
74 2
            if ($field instanceof Field) {
75 2
                if (in_array($field->getFieldName(), $translatableFields, true)) {
76 2
                    $result[] = $prefix.$field->getExposedName();
77 1
                }
78 1
            } elseif ($field instanceof EmbedOne) {
79 2
                $result = array_merge(
80 1
                    $result,
81 2
                    $this->getTranslatableFields(
82 2
                        $field->getDocument(),
83 2
                        $prefix.$field->getExposedName().'.'
84 1
                    )
85 1
                );
86 1 View Code Duplication
            } elseif ($field instanceof EmbedMany) {
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...
87 2
                $result = array_merge(
88 1
                    $result,
89 2
                    $this->getTranslatableFields(
90 2
                        $field->getDocument(),
91 2
                        $prefix.$field->getExposedName().'.0.'
92 1
                    )
93 1
                );
94 1
            }
95 1
        }
96
97 2
        return $result;
98
    }
99
}
100