Completed
Push — master ( 66af9c...165352 )
by Fabian
02:21
created

MetadataMapper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 69.14 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 8
dl 56
loc 81
ccs 56
cts 56
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C map() 56 77 10

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
/**
4
 * Copyright 2015 Fabian Grutschus. All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without modification,
7
 * are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *   list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *   this list of conditions and the following disclaimer in the documentation
14
 *   and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 *
27
 * The views and conclusions contained in the software and documentation are those
28
 * of the authors and should not be interpreted as representing official policies,
29
 * either expressed or implied, of the copyright holders.
30
 *
31
 * @author    Fabian Grutschus <[email protected]>
32
 * @copyright 2015 Fabian Grutschus. All rights reserved.
33
 * @license   BSD-2-Clause
34
 */
35
36
namespace Fabiang\DoctrineDynamic\Mapper;
37
38
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
39
use Fabiang\DoctrineDynamic\Configuration\Entity as EntityConfiguration;
40
41
class MetadataMapper implements Mapper
42
{
43
44 1
    public function map(ClassMetadata $metadata, EntityConfiguration $configuration)
45
    {
46 1
        if ($configuration->getRepository()) {
47 1
            $metadata->customRepositoryClassName = $configuration->getRepository();
0 ignored issues
show
Bug introduced by
Accessing customRepositoryClassName on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
48 1
        }
49
50 1
        foreach ($configuration->getFields() as $field) {
51 1 View Code Duplication
            foreach ($field->getOneToOne() as $oneToOne) {
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...
52
                $oneToOneConfig = [
53 1
                    'fieldName'    => $field->getName(),
54 1
                    'targetEntity' => $oneToOne->getTargetEntity(),
55 1
                    'inversedBy'   => $oneToOne->getInversedBy(),
56 1
                    'mappedBy'     => $oneToOne->getMappedBy(),
57 1
                ];
58
59 1
                $joinColumn = $oneToOne->getJoinColumn();
60 1
                if (null !== $joinColumn) {
61 1
                    $oneToOneConfig['joinColumns'] = [
62
                        [
63 1
                            'name'                 => $joinColumn->getName(),
64 1
                            'referencedColumnName' => $joinColumn->getReferencedColumnName(),
65
                        ]
66 1
                    ];
67 1
                }
68
69 1
                $metadata->mapOneToOne($oneToOneConfig);
70 1
            }
71
72 1 View Code Duplication
            foreach ($field->getManyToOne() as $manyToOne) {
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...
73
                $manyToOneConfig = [
74 1
                    'fieldName'    => $field->getName(),
75 1
                    'targetEntity' => $manyToOne->getTargetEntity(),
76 1
                    'inversedBy'   => $manyToOne->getInversedBy(),
77 1
                ];
78
79 1
                $joinColumn = $manyToOne->getJoinColumn();
80 1
                if (null !== $joinColumn) {
81 1
                    $manyToOneConfig['joinColumns'] = [
82
                        [
83 1
                            'name'                 => $joinColumn->getName(),
84 1
                            'referencedColumnName' => $joinColumn->getReferencedColumnName(),
85
                        ]
86 1
                    ];
87 1
                }
88
89 1
                $metadata->mapManyToOne($manyToOneConfig);
90 1
            }
91
92 1
            foreach ($field->getOneToMany() as $oneToMany) {
93
                $oneToMany = [
94 1
                    'fieldName'    => $field->getName(),
95 1
                    'targetEntity' => $oneToMany->getTargetEntity(),
96 1
                    'mappedBy'     => $oneToMany->getMappedBy(),
97 1
                ];
98
99 1
                $metadata->mapOneToMany($oneToMany);
100 1
            }
101
102 1 View Code Duplication
            foreach ($field->getManyToMany() as $manyToMany) {
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...
103
                $manyToManyConfig = [
104 1
                    'fieldName'    => $field->getName(),
105 1
                    'targetEntity' => $manyToMany->getTargetEntity(),
106 1
                    'inversedBy'   => $manyToMany->getInversedBy(),
107 1
                    'mappedBy'     => $manyToMany->getMappedBy(),
108 1
                ];
109
110 1
                $joinTable = $manyToMany->getJoinTable();
111 1
                if (null !== $joinTable) {
112 1
                    $manyToManyConfig['joinTable'] = [
113 1
                        'name' => $joinTable->getName(),
114
                    ];
115 1
                }
116
117 1
                $metadata->mapManyToMany($manyToManyConfig);
118 1
            }
119 1
        }
120 1
    }
121
}
122