Entity::toArray()   C
last analyzed

Complexity

Conditions 15
Paths 72

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 29.4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
ccs 21
cts 35
cp 0.6
rs 5.0504
cc 15
eloc 27
nc 72
nop 1
crap 29.4

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 WebCMS\Entity;
4
5
use Doctrine\ORM\Mapping as orm;
6
7
/**
8
 * Basic entity with identificator.
9
 * @orm\mappedSuperclass
10
 * @author Tomáš Voslař <tomas.voslar at webcook.cz>
11
 * @property-read int $id
12
 */
13
abstract class Entity extends \Nette\Object
14
{
15
    /**
16
     * @orm\id
17
     * @orm\generatedValue
18
     * @orm\column(type="integer")
19
     * @var int
20
     */
21
    public $id;
22
23
    /**
24
     * @return int
25
     */
26 41
    public function getId()
27
    {
28 41
        return $this->id;
29
    }
30
31
    /**
32
     * Converts object into array.
33
     * @return Array
34
     */
35 3
    public function toArray($notEmptyValues = false)
36
    {
37 3
        $props = $this->getReflection()->getProperties();
38 3
        if ($this->getReflection()->getParentClass()) {
39 3
            $temp = $this->getReflection()->getParentClass()->getProperties();
40 3
            $props = array_merge($props, $temp);
41 3
        }
42
43 3
        if (strpos($this->getReflection()->getName(), '__CG__') !== FALSE) {
44
            $props = $this->getReflection()->getParentClass()->getProperties();
45
        }
46
47 3
        $array = array();
48 3
        foreach ($props as $prop) {
49 3
            $getter = 'get'.ucfirst($prop->getName());
50
51 3
            if (method_exists($this, $getter)) {
52 3
                $empty = $this->$getter();
53 3
                $empty = is_null($empty) || empty($empty);
54
55 3
                if (!is_object($this->$getter())) {
56 3
                    if (($notEmptyValues && !$empty) || !$empty) {
57 1
                        $array[$prop->getName()] = $this->$getter();
58 1
                    }
59 3
                } elseif (is_object($this->$getter())) {
60
                    if (method_exists($this->$getter(), 'getId')) {
61
                        $array[$prop->getName()] = $this->$getter()->getId();
62
                    } elseif ($this->$getter() instanceof \DateTime) {
63
                        $array[$prop->getName()] = $this->$getter()->format('d.m.Y');
64
                    } elseif ($this->$getter() instanceof \Doctrine\Common\Collections\Collection) {
65
                        $tmp = array();
66
                        foreach ($this->$getter() as $item) {
67
                            $tmp[] = $item->getId();
68
                        }
69
70
                        $array[$prop->getName()] = $tmp;
71
                    }
72
                }
73 3
            }
74 3
        }
75
76 3
        return $array;
77
    }
78
}
79