Passed
Branch dev_2x (3e8772)
by Adrian
01:42
created

ComputedPropertyObserver::observeBaseEntity()   B

Complexity

Conditions 11
Paths 16

Size

Total Lines 58
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 58
rs 7.3166
cc 11
nc 16
nop 1

How to fix   Long Method    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
declare(strict_types=1);
3
4
namespace Sirius\Orm\CodeGenerator\Observer;
5
6
use Nette\PhpGenerator\ClassType;
7
use Sirius\Orm\Blueprint\ComputedProperty;
8
use Sirius\Orm\Blueprint\Mapper;
9
use Sirius\Orm\Helpers\Str;
10
11
class ComputedPropertyObserver extends Base
12
{
13
    /**
14
     * @var ComputedProperty
15
     */
16
    protected $property;
17
18
    public function with(ComputedProperty $column)
19
    {
20
        $clone           = clone($this);
21
        $clone->property = $column;
22
23
        return $clone;
24
    }
25
26
    public function observe(string $key, $object)
27
    {
28
        if ($key == $this->property->getMapper()->getName() . '_base_entity') {
29
            return $this->observeBaseEntity($object);
30
        }
31
32
        return $object;
33
    }
34
35
    public function __toString()
36
    {
37
        return sprintf('Observer for column %s of mapper %s', $this->property->getName(), $this->property->getMapper()->getName());
38
    }
39
40
    public function observeBaseEntity(ClassType $class): ClassType
41
    {
42
        $name = $this->property->getName();
43
        $type = $this->property->getType();
44
45
        if (is_string($type) && (class_exists($type) || strpos($type, '\\') !== false)) {
46
            /** @scrutinizer ignore-deprecated */ $class->getNamespace()->addUse($type, null, $alias);
47
        } else {
48
            $alias = $type;
49
        }
50
51
        if ($this->property->getMapper()->getEntityStyle() === Mapper::ENTITY_STYLE_PROPERTIES) {
52
            $class->addComment(sprintf(
53
                '@property %s $%s',
54
                $alias ? $alias . ($this->property->getNullable() ? '|null' : '') : 'mixed',
55
                $name
56
            ));
57
58
            if (($body = $this->property->getSetterBody())) {
59
                $setter = $class->addMethod(Str::methodName($name . ' Attribute', 'set'));
60
                $setter->setVisibility(ClassType::VISIBILITY_PROTECTED);
61
                $setter->addParameter('value')
62
                       ->setNullable($this->property->getNullable())
63
                       ->setType($alias);
64
                $setter->setBody($body);
65
                $setter->setComment($this->property->getSetterComment());
66
            }
67
68
            if (($body = $this->property->getGetterBody())) {
69
                $getter = $class->addMethod(Str::methodName($name . ' Attribute', 'get'));
70
                $getter->setVisibility(ClassType::VISIBILITY_PROTECTED);
71
                $getter->setReturnType($alias);
72
                $getter->setReturnNullable($this->property->getNullable());
73
                $getter->setBody($body);
74
                $getter->setComment($this->property->getGetterComment());
75
            }
76
        } else {
77
            if (($body = $this->property->getSetterBody())) {
78
                $setter = $class->addMethod(Str::methodName($name, 'set'));
79
                $setter->setVisibility(ClassType::VISIBILITY_PUBLIC);
80
                $setter->addParameter('value')
81
                       ->setType($type)
82
                       ->setNullable($this->property->getNullable());
83
                $setter->setBody($body);
84
                $setter->setComment($this->property->getSetterComment());
85
            }
86
87
            if (($body = $this->property->getGetterBody())) {
88
                $getter = $class->addMethod(Str::methodName($name, 'get'));
89
                $getter->setVisibility(ClassType::VISIBILITY_PUBLIC);
90
                $getter->setBody($body);
91
                $getter->setReturnType($type);
92
                $getter->setReturnNullable($this->property->getNullable());
93
                $getter->setComment($this->property->getGetterComment());
94
            }
95
        }
96
97
        return $class;
98
    }
99
}
100