YamlDriver   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 69
Duplicated Lines 63.77 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 44
loc 69
c 0
b 0
f 0
wmc 15
lcom 0
cbo 3
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B addMetadataFor() 5 19 6
D addFieldMapping() 39 39 9

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
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
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 Cubiche\Core\Metadata\Tests\Fixtures\Driver;
13
14
use Cubiche\Core\Metadata\ClassMetadataInterface;
15
use Cubiche\Core\Metadata\Driver\AbstractYamlDriver;
16
use Cubiche\Core\Metadata\PropertyMetadata;
17
18
/**
19
 * YamlDriver class.
20
 *
21
 * @author Ivannis Suárez Jerez <[email protected]>
22
 */
23
class YamlDriver extends AbstractYamlDriver
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    protected function addMetadataFor(array $config, ClassMetadataInterface $classMetadata)
29
    {
30
        if (isset($config['fields'])) {
31
            foreach ($config['fields'] as $fieldName => $mapping) {
32
                if (!isset($mapping['fieldName'])) {
33
                    $mapping['fieldName'] = $fieldName;
34
                }
35
36
                $booleanAttributes = array('id', 'unique');
37 View Code Duplication
                foreach ($mapping as $key => $value) {
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...
38
                    if (in_array($key, $booleanAttributes)) {
39
                        $mapping[$key] = (true === $mapping[$key]);
40
                    }
41
                }
42
43
                $this->addFieldMapping($classMetadata, $mapping);
44
            }
45
        }
46
    }
47
48
    /**
49
     * @param ClassMetadataInterface $classMetadata
50
     * @param array                  $mapping
51
     */
52 View Code Duplication
    private function addFieldMapping(ClassMetadataInterface $classMetadata, $mapping)
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...
53
    {
54
        if (isset($mapping['name'])) {
55
            $name = $mapping['name'];
56
            if (!isset($mapping['fieldName'])) {
57
                $mapping['fieldName'] = $name;
58
            }
59
        } elseif (isset($mapping['fieldName'])) {
60
            $name = $mapping['fieldName'];
61
            if (!isset($mapping['name'])) {
62
                $mapping['name'] = $name;
63
            }
64
        } else {
65
            throw new \InvalidArgumentException('Cannot infer a name from the mapping');
66
        }
67
68
        $isIdentifier = false;
69
        if (isset($mapping['id']) && $mapping['id'] === true) {
70
            $isIdentifier = true;
71
            $mapping['name'] = '_id';
72
        }
73
74
        $propertyMetadata = new PropertyMetadata(
75
            $classMetadata->className(),
76
            $mapping['fieldName']
77
        );
78
79
        $propertyMetadata->addMetadata('identifier', $isIdentifier);
80
        $propertyMetadata->addMetadata('name', $mapping['name']);
81
82
        if (isset($mapping['type'])) {
83
            $propertyMetadata->addMetadata('type', $mapping['type']);
84
            if (isset($mapping['of'])) {
85
                $propertyMetadata->addMetadata('of', $mapping['of']);
86
            }
87
        }
88
89
        $classMetadata->addPropertyMetadata($propertyMetadata);
90
    }
91
}
92