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($this->arguments) . ')', |
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
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function defComment() : CommentsGenerator |
92
|
|
|
{ |
93
|
|
|
return new FunctionCommentsGenerator($this->arguments, $this->argumentDescriptions, $this); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|