Test Failed
Branch v5 (12d602)
by Alexey
04:51
created

ClassGenerator::generate()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 8
nop 0
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class generator
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
12
namespace Inji\CodeGenerator;
13
14
class ClassGenerator extends \InjiObject {
15
16
    public $propertys = [];
17
    public $methods = [];
18
    public $name = 'class';
19
    public $extends = '';
20
21
    public function addProperty($name, $value = null, $static = false, $security = 'public') {
22
        $this->propertys[$name] = new Property();
23
        $this->propertys[$name]->name = $name;
24
        $this->propertys[$name]->value = $value;
25
        $this->propertys[$name]->static = $static;
26
        $this->propertys[$name]->security = $security;
27
    }
28
29
    public function addMethod($name, $body = '', $propertys = [], $static = false, $security = 'public') {
30
        $this->methods[$name] = new Method();
31
        $this->methods[$name]->name = $name;
32
        $this->methods[$name]->body = $body;
33
        $this->methods[$name]->propertys = $propertys;
34
        $this->methods[$name]->static = $static;
35
        $this->methods[$name]->security = $security;
36
    }
37
38
    public function generate() {
39
        $code = 'class ' . $this->name . ' ';
40
        if ($this->extends) {
41
            $code .= 'extends ' . $this->extends . ' ';
42
        }
43
        $code .= "{\n";
44
        foreach ($this->propertys as $property) {
45
            $code .= '    ' . str_replace("\n", "\n    ", $property->generate()) . "\n";
46
        }
47
        foreach ($this->methods as $method) {
48
            $code .= '    ' . str_replace("\n", "\n    ", $method->generate()) . "\n";
49
        }
50
        $code .= "}\n";
51
        return $code;
52
    }
53
54
}
55