Completed
Push — master ( 704bed...4ce755 )
by Vitaly
03:37 queued 01:01
created

FunctionGenerator::defLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 03.09.16 at 11:30
5
 */
6
namespace samsonphp\generator;
7
8
/**
9
 * Function generation class.
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
class FunctionGenerator extends AbstractGenerator
14
{
15
    /** @var string Function name */
16
    protected $name;
17
18
    /** @var array Collection of function arguments */
19
    protected $arguments = [];
20
21
    /** @var array Collection of function arguments descriptions */
22
    protected $argumentDescriptions = [];
23
24
    /**
25
     * FunctionGenerator constructor.
26
     *
27
     * @param GenericGenerator $parent Parent Generator
28
     * @param string           $name Function name
29
     */
30
    public function __construct(string $name, GenericGenerator $parent = null)
31
    {
32
        $this->name = $name;
33
34
        parent::__construct($parent);
35
    }
36
37
    /**
38
     * Set function argument.
39
     *
40
     * @param string      $name        Argument name
41
     * @param string|null $type        Argument type
42
     * @param string      $description Argument description
43
     *
44
     * @return FunctionGenerator
45
     */
46
    public function defArgument(string $name, string $type = null, string $description = null) : FunctionGenerator
47
    {
48
        $this->arguments[$name] = $type;
49
        $this->argumentDescriptions[$name] = $description;
50
51
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function code(int $indentation = 0) : string
58
    {
59
        $innerIndentation = $this->indentation(1);
60
61
        $formattedCode = [
62
            $this->buildDefinition() . '(' . $this->buildArguments() . ')',
63
            '{'
64
        ];
65
        // Prepend inner indentation to code
66
        foreach ($this->code as $codeLine) {
67
            $formattedCode[] = $innerIndentation.$codeLine;
68
        }
69
        $formattedCode[] = '}';
70
71
        $this->generatedCode .= implode("\n".$this->indentation($indentation), $formattedCode);
72
73
        return $this->generatedCode;
74
    }
75
76
    /**
77
     * Build function definition.
78
     *
79
     * @return string Function definition
80
     */
81
    protected function buildDefinition()
82
    {
83
        return 'function ' . $this->name;
84
    }
85
86
    /**
87
     * Build function arguments.
88
     *
89
     * @return string
90
     */
91
    protected function buildArguments() : string
92
    {
93
        $argumentsString = [];
94
        foreach ($this->arguments as $argumentName => $argumentType) {
95
            // Group name with type
96
            $argumentsString[] = ($argumentType !== null ? $argumentType . ' ' : '') . '$' . $argumentName;
97
        }
98
99
        return implode(', ', $argumentsString);
100
    }
101
102
    /**
103
     * Add function code line.
104
     *
105
     * @param string $code Code line
106
     *
107
     * @return $this
108
     */
109
    public function defLine(string $code)
110
    {
111
        $this->code[] = $code;
112
113
        return $this;
114
    }
115
}
116