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

ClassMethod   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 42
c 0
b 0
f 0
dl 0
loc 126
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Blueprint;
5
6
use Nette\PhpGenerator\ClassType;
7
8
/**
9
 * This class is used to register methods for various classes
10
 * (entity, mapper, query)
11
 */
12
class ClassMethod extends Base
13
{
14
    protected $name;
15
16
    protected $visibility = ClassType::VISIBILITY_PUBLIC;
17
18
    protected $parameters = [];
19
20
    protected $returnType = null;
21
22
    protected $body;
23
24
    protected $comments = '';
25
26
    public static function make(string $name)
27
    {
28
        return (new static)->setName($name);
29
    }
30
31
    public function getErrors(): array
32
    {
33
        $errors = [];
34
35
        if (! $this->name) {
36
            $errors[] = 'Method requires a name';
37
        }
38
39
        if (! in_array($this->visibility, [ClassType::VISIBILITY_PUBLIC, ClassType::VISIBILITY_PROTECTED])) {
40
            $errors[] = 'Wrong method visilibity type. Only `public` and `protected` are allowed.';
41
        }
42
43
        return $errors;
44
    }
45
46
    public function getName(): string
47
    {
48
        return $this->name;
49
    }
50
51
    public function setName(string $name): ClassMethod
52
    {
53
        $this->name = $name;
54
55
        return $this;
56
    }
57
58
    public function getParameters(): array
59
    {
60
        return $this->parameters;
61
    }
62
63
    public function addParameter(string $name, string $type = null, $default = null)
64
    {
65
        $this->parameters[$name] = ['type' => $type, $default => $default];
66
67
        return $this;
68
    }
69
70
    public function getReturnType(): ?string
71
    {
72
        return $this->returnType;
73
    }
74
75
    public function setReturnType(string $returnType = null): ClassMethod
76
    {
77
        $this->returnType = $returnType;
78
79
        return $this;
80
    }
81
82
    public function getBody(): string
83
    {
84
        return $this->body;
85
    }
86
87
    public function setBody(string $body): ClassMethod
88
    {
89
        $this->body = $body;
90
91
        return $this;
92
    }
93
94
95
    public function getVisibility(): string
96
    {
97
        return $this->visibility;
98
    }
99
100
    public function setVisibility(string $visibility): ClassMethod
101
    {
102
        $this->visibility = $visibility;
103
104
        return $this;
105
    }
106
107
    public function getComments(): ?string
108
    {
109
        return $this->comments;
110
    }
111
112
    public function setComments(string $comments): ClassMethod
113
    {
114
        $this->comments = $comments;
115
116
        return $this;
117
    }
118
119
    protected function addMethodToClass(ClassType $class)
120
    {
121
        $method = $class->addMethod($this->getName());
122
123
        if ($this->returnType) {
124
            $method->setReturnType($this->returnType);
125
        }
126
127
        if ($this->comments) {
128
            $method->addComment($this->comments);
129
        }
130
131
        if ($this->body) {
132
            $method->setBody($this->body);
133
        }
134
135
        foreach ($this->parameters as $name => $details) {
136
            $param = $method->addParameter($name, $details['default']);
137
            $param->setType($details['type']);
138
        }
139
    }
140
}
141