Passed
Push — master ( 432b63...d5390f )
by Andrea
37:11 queued 18:11
created

DoctrineFieldReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Cdf\BiCoreBundle\Utils\Entity;
4
5
class DoctrineFieldReader
6
{
7
8
    private $tableprefix;
9
10 7
    public function __construct($tableprefix)
11
    {
12 7
        $this->tableprefix = $tableprefix;
13 7
    }
14 7
    public function getField2Object($fieldname, $object, $decodifiche = null)
15
    {
16 7
        $property = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $property is dead and can be removed.
Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
17 7
        $field = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
18 7
        $propertyfound = false;
19 7
        $subfields = explode('.', str_replace($this->tableprefix, '', $fieldname));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
20 7
        foreach ($subfields as $field) {
21 7
            $property = $this->getObjectProperty($field, $object);
22 7
            if ($property) {
23 7
                $object = $object->$property();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
24 7
                $propertyfound = true;
25
            }
26
        }
27 7
        if (!$propertyfound) {
28
            throw new \Exception('Proprietà ' . $field . ' non trovata per ' . $fieldname);
29
        }
30 7
        if ($decodifiche) {
31
            if (key_exists($object, $decodifiche)) {
32
                $object = $decodifiche[$object];
33
            }
34
        }
35
36 7
        return $object;
37
    }
38 7
    public function object2View($object, $type = null, $decodifiche = null)
39
    {
40 7
        $risposta = null;
41
42 7
        if ($decodifiche) {
43
            $type = 'string';
44
        }
45
46 7
        if (!is_null($object)) {
47 7
            switch (strtolower($this->getObjectType($type, $object))) {
48 7
                case 'array':
49 1
                    $risposta = print_r($object, true);
50 1
                    break;
51 7
                case 'date':
52 1
                    $risposta = $object->format('d/m/Y');
53 1
                    break;
54 7
                case 'datetime':
55 2
                    $risposta = $object->format('d/m/Y H:i');
56 2
                    break;
57 7
                case 'boolean':
58 5
                    $risposta = $object ? 'SI' : 'NO';
59 5
                    break;
60 7
                case 'string':
61
                default:
62 7
                    $risposta = $object;
63 7
                    break;
64
            }
65
        }
66
67 7
        return $risposta;
68
    }
69 7
    private function getObjectType($type, $object)
70
    {
71 7
        if (null === $type) {
72
            $tipo = is_object($object) ?
73
                    get_class($object) :
74
                    gettype($object);
75
        } else {
76 7
            $tipo = $type;
77
        }
78 7
        return $tipo;
79
    }
80 7
    private function getObjectProperty($field, $object)
81
    {
82 7
        $property = 'get' . ucfirst($field);
83 7
        if (method_exists($object, $property)) {
84 7
            return $property;
85
        }
86 7
        $property = 'is' . ucfirst($field);
87 7
        if (method_exists($object, $property)) {
88 1
            return $property;
89
        }
90 7
        $property = 'has' . ucfirst($field);
91 7
        if (method_exists($object, $property)) {
92
            return $property;
93
        }
94 7
    }
95
}
96