Passed
Push — master ( a74354...254a60 )
by Andrea
18:29 queued 11s
created

DoctrineFieldReader::object2View()   C

Complexity

Conditions 13
Paths 24

Size

Total Lines 41
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 16.4272

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 33
nc 24
nop 3
dl 0
loc 41
ccs 24
cts 33
cp 0.7272
crap 16.4272
rs 6.6166
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Entity;
4
5
use Exception;
6
use Doctrine\Common\Inflector\Inflector;
7
use Cdf\BiCoreBundle\Utils\FieldType\FieldTypeUtils;
8
9
class DoctrineFieldReader
10
{
11
    private $tableprefix;
12
13 7
    public function __construct($tableprefix)
14
    {
15 7
        $this->tableprefix = $tableprefix;
16 7
    }
17
18 7
    public function getField2Object($fieldname, $object, $decodifiche = null)
19
    {
20 7
        $property = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $property is dead and can be removed.
Loading history...
21 7
        $field = '';
22 7
        $propertyfound = false;
23 7
        $subfields = explode('.', str_replace($this->tableprefix, '', $fieldname));
24 7
        foreach ($subfields as $field) {
25 7
            $property = $this->getObjectProperty($field, $object);
26 7
            if ($property) {
27 7
                $object = $object->$property();
28 7
                $propertyfound = true;
29
            }
30
        }
31 7
        if (!$propertyfound) {
32
            throw new Exception('Proprietà '.$field.' non trovata per '.$fieldname);
33
        }
34 7
        if ($decodifiche) {
35
            if (key_exists($object, $decodifiche)) {
36
                $object = $decodifiche[$object];
37
            }
38
        }
39
40 7
        return $object;
41
    }
42
43 7
    public function object2View($object, $type = null, $decodifiche = null)
44
    {
45 7
        $risposta = null;
46
47 7
        if ($decodifiche) {
48
            $type = 'string';
49
        }
50
51 7
        if (!is_null($object)) {
52 7
            switch (strtolower($this->getObjectType($type, $object))) {
53 7
                case 'array':
54 1
                    $risposta = print_r($object, true);
55 1
                    break;
56 7
                case 'date':
57 1
                    $risposta = $object->format(FieldTypeUtils::getEnvVar("DATE_FORMAT", "d/m/Y"));
58 1
                    break;
59 7
                case 'datetime':
60 2
                    $risposta = $object->format(FieldTypeUtils::getEnvVar("DATETIME_FORMAT", "d/m/Y H:i"));
61 2
                    break;
62 7
                case 'string2datetime':
63
                    $time = strtotime($object);
64
                    $risposta = date(FieldTypeUtils::getEnvVar("DATETIME_FORMAT", "d/m/Y H:i"), $time);
65
                    break;
66 7
                case 'string2date':
67
                    $time = strtotime($object);
68
                    $risposta = date(FieldTypeUtils::getEnvVar("DATE_FORMAT", "d/m/Y"), $time);
69
                    break;
70 7
                case 'string2bool':
71
                    $risposta = $object ? 'SI' : 'NO';
72
                    break;
73 7
                case 'boolean':
74 5
                    $risposta = $object ? 'SI' : 'NO';
75 5
                    break;
76 7
                case 'string':
77
                default:
78 7
                    $risposta = $object;
79 7
                    break;
80
            }
81
        }
82
83 7
        return $risposta;
84
    }
85
86 7
    private function getObjectType($type, $object)
87
    {
88 7
        if (null === $type) {
89
            $tipo = is_object($object) ?
90
                    get_class($object) :
91
                    gettype($object);
92
        } else {
93 7
            $tipo = $type;
94
        }
95
96 7
        return $tipo;
97
    }
98
99 7
    private function getObjectProperty($field, $object)
100
    {
101 7
        $property = 'get'.Inflector::camelize(ucfirst($field));
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Inflector\Inflector::camelize() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

101
        $property = 'get'./** @scrutinizer ignore-deprecated */ Inflector::camelize(ucfirst($field));
Loading history...
102 7
        if (method_exists($object, $property)) {
103 7
            return $property;
104
        }
105 7
        $property = 'get'.ucfirst($field);
106 7
        if (method_exists($object, $property)) {
107
            return $property;
108
        }
109 7
        $property = 'is'.ucfirst($field);
110 7
        if (method_exists($object, $property)) {
111 1
            return $property;
112
        }
113 7
        $property = 'has'.ucfirst($field);
114 7
        if (method_exists($object, $property)) {
115
            return $property;
116
        }
117 7
    }
118
}
119