Completed
Push — master ( 4ce755...511437 )
by Vitaly
02:11
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
    use CodeTrait;
16
    
17
    /** @var string Function name */
18
    protected $name;
19
20
    /** @var array Collection of function arguments */
21
    protected $arguments = [];
22
23
    /** @var array Collection of function arguments descriptions */
24
    protected $argumentDescriptions = [];
25
26
    /**
27
     * FunctionGenerator constructor.
28
     *
29
     * @param GenericGenerator $parent Parent Generator
30
     * @param string           $name Function name
31
     */
32
    public function __construct(string $name, GenericGenerator $parent = null)
33
    {
34
        $this->name = $name;
35
36
        parent::__construct($parent);
37
    }
38
39
    /**
40
     * Set function argument.
41
     *
42
     * @param string      $name        Argument name
43
     * @param string|null $type        Argument type
44
     * @param string      $description Argument description
45
     *
46
     * @return FunctionGenerator
47
     */
48
    public function defArgument(string $name, string $type = null, string $description = null) : FunctionGenerator
49
    {
50
        $this->arguments[$name] = $type;
51
        $this->argumentDescriptions[$name] = $description;
52
53
        return $this;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function code(int $indentation = 0) : string
60
    {
61
        $innerIndentation = $this->indentation(1);
62
63
        $formattedCode = [
64
            $this->buildDefinition() . '(' . $this->buildArguments() . ')',
65
            '{'
66
        ];
67
        // Prepend inner indentation to code
68
        foreach ($this->code as $codeLine) {
69
            $formattedCode[] = $innerIndentation.$codeLine;
70
        }
71
        $formattedCode[] = '}';
72
73
        $this->generatedCode .= implode("\n".$this->indentation($indentation), $formattedCode);
74
75
        return $this->generatedCode;
76
    }
77
78
    /**
79
     * Build function definition.
80
     *
81
     * @return string Function definition
82
     */
83
    protected function buildDefinition()
84
    {
85
        return 'function ' . $this->name;
86
    }
87
88
    /**
89
     * Build function arguments.
90
     *
91
     * @return string
92
     */
93
    protected function buildArguments() : string
94
    {
95
        $argumentsString = [];
96
        foreach ($this->arguments as $argumentName => $argumentType) {
97
            // Group name with type
98
            $argumentsString[] = ($argumentType !== null ? $argumentType . ' ' : '') . '$' . $argumentName;
99
        }
100
101
        return implode(', ', $argumentsString);
102
    }
103
}
104