MongoYamlFixture   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 3.66 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 15
lcom 1
cbo 2
dl 3
loc 82
rs 10
c 4
b 1
f 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C createObject() 3 70 15

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
namespace Khepin\YamlFixturesBundle\Fixture;
4
5
use Doctrine\Common\Util\Inflector;
6
7
class MongoYamlFixture extends AbstractFixture
8
{
9
    /**
10
     * Creates and returns one object based on the given data and metadata
11
     *
12
     * @param $class object's class name
13
     * @param $data array of the object's fixture data
14
     * @param $metadata the class metadata for doctrine
15
     * @param $embedded true for embedded documents
16
     * @return Object
17
     */
18
    public function createObject($class, $data, $metadata, $options = array())
19
    {
20
        // options to state if a document is to be embedded or persisted on its own
21
        $embedded = isset($options['embedded']);
22
        $mapping = array_keys($metadata->fieldMappings);
23
        // Instantiate new object
24
        $object = $this->makeInstance($class, $data);
25
        unset($data['__construct']);
26
27
        foreach ($data as $field => $value) {
28
            // Add the fields defined in the fixtures file
29
            $method = Inflector::camelize('set_' . $field);
30
            // This is a standard field
31
            if (in_array($field, $mapping)) {
32
                // Dates need to be converted to DateTime objects
33
                $type = $metadata->fieldMappings[$field]['type'];
34
                if ($type == 'many') {
35
                    $method = Inflector::camelize('add_'.$field);
36
                    // EmbedMany
37
                    if (
38
                        isset($metadata->fieldMappings[$field]['embedded']) &&
39
                        $metadata->fieldMappings[$field]['embedded']) {
40
                        foreach ($value as $embedded_value) {
41
                            $embed_class = $metadata->fieldMappings[$field]['targetDocument'];
42
                            $embed_data = $embedded_value;
43
                            $embed_meta = $this->getMetaDataForClass($embed_class);
44
                            $value = $this->createObject($embed_class, $embed_data, $embed_meta, array(
45
                                'embedded' => true));
46
                            $object->$method($value);
47
                        }
48
                    //ReferenceMany
49
                    } else {
50
                        foreach ($value as $reference_object) {
51
                            $object->$method($this->loader->getReference($reference_object));
52
                        }
53
                    }
54
                } else {
55 View Code Duplication
                    if ($type == 'datetime' || $type == 'date' || $type == 'time') {
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...
56
                        $value = new \DateTime($value);
57
                    }
58
                    if ($type == 'one') {
59
                        // EmbedOne
60
                        if (
61
                            isset($metadata->fieldMappings[$field]['embedded']) &&
62
                            $metadata->fieldMappings[$field]['embedded']) {
63
                            $embed_class = $metadata->fieldMappings[$field]['targetDocument'];
64
                            $embed_data = $value;
65
                            $embed_meta = $this->getMetaDataForClass($embed_class);
66
                            $value = $this->createObject($embed_class, $embed_data, $embed_meta, array(
67
                                'embedded' => true));
68
                        // ReferenceOne
69
                        } else {
70
                            $value = $this->loader->getReference($value);
71
                        }
72
                    }
73
                    $object->$method($value);
74
                }
75
            } else {
76
                // The key is not a field's name but the name of a method to be called
77
                $object->$method($value);
78
            }
79
        }
80
81
        // Save a reference to the current object
82
        if (!$embedded) {
83
            $this->runServiceCalls($object);
84
        }
85
86
        return $object;
87
    }
88
}
89