OrmYamlFixture   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 6.98 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 9
c 5
b 0
f 1
lcom 1
cbo 2
dl 3
loc 43
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D createObject() 3 40 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
namespace Khepin\YamlFixturesBundle\Fixture;
4
5
use Doctrine\Common\Util\Inflector;
6
7
class OrmYamlFixture extends AbstractFixture
8
{
9
    public function createObject($class, $data, $metadata, $options = array())
10
    {
11
        $mapping = array_keys($metadata->fieldMappings);
12
        $associations = array_keys($metadata->associationMappings);
13
14
        $object = $this->makeInstance($class, $data);
15
        unset($data['__construct']);
16
17
        foreach ($data as $field => $value) {
18
            // Add the fields defined in the fistures file
19
            $method = Inflector::camelize('set_' . $field);
20
            //
21
            if (in_array($field, $mapping)) {
22
                // Dates need to be converted to DateTime objects
23
                $type = $metadata->fieldMappings[$field]['type'];
24 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...
25
                    $value = new \DateTime($value);
26
                }
27
                $object->$method($value);
28
            } elseif (in_array($field, $associations)) { // This field is an association
29
                if (is_array($value)) { // The field is an array of associations
30
                    $referenceArray = array();
31
                    foreach ($value as $referenceObject) {
32
                        $referenceArray[] = $this->loader->getReference($referenceObject);
33
                    }
34
                    $object->$method($referenceArray);
35
                } else {
36
                    $object->$method($this->loader->getReference($value));
37
                }
38
            } else {
39
                // It's a method call that will set a field named differently
40
                // eg: FOSUserBundle ->setPlainPassword sets the password after
41
                // Encrypting it
42
                $object->$method($value);
43
            }
44
        }
45
        $this->runServiceCalls($object);
46
47
        return $object;
48
    }
49
}
50