Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
FunctionGenerator | |
100.00% |
23 / 23 |
|
100.00% |
5 / 5 |
11 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
setAsClosure | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isClosure | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
6 | |||
__toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Code\Generator; |
15 | |
16 | /** |
17 | * Function generator class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Code |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 5.0.0 |
25 | */ |
26 | class FunctionGenerator extends AbstractGenerator |
27 | { |
28 | |
29 | use Traits\NameTrait, Traits\DocblockTrait, Traits\FunctionTrait, Traits\BodyTrait; |
30 | |
31 | /** |
32 | * Function interface flag |
33 | * @var bool |
34 | */ |
35 | protected bool $closure = false; |
36 | |
37 | /** |
38 | * Function body |
39 | * @var ?string |
40 | */ |
41 | protected ?string $body = null; |
42 | |
43 | /** |
44 | * Function indent |
45 | * @var int |
46 | */ |
47 | protected int $indent = 0; |
48 | |
49 | /** |
50 | * Constructor |
51 | * |
52 | * Instantiate the function generator object |
53 | * |
54 | * @param ?string $name |
55 | * @param bool $closure |
56 | */ |
57 | public function __construct(?string $name = null, bool $closure = false) |
58 | { |
59 | if ($name !== null) { |
60 | $this->setName($name); |
61 | } |
62 | $this->setAsClosure($closure); |
63 | } |
64 | |
65 | /** |
66 | * Set the function closure flag |
67 | * |
68 | * @param bool $closure |
69 | * @return FunctionGenerator |
70 | */ |
71 | public function setAsClosure(bool $closure = false): FunctionGenerator |
72 | { |
73 | $this->closure = $closure; |
74 | return $this; |
75 | } |
76 | |
77 | /** |
78 | * Get the function closure flag |
79 | * |
80 | * @return bool |
81 | */ |
82 | public function isClosure(): bool |
83 | { |
84 | return $this->closure; |
85 | } |
86 | |
87 | /** |
88 | * Render function |
89 | * |
90 | * @throws Exception |
91 | * @return string |
92 | */ |
93 | public function render(): string |
94 | { |
95 | if ($this->name === null) { |
96 | throw new Exception('Error: The function name has not been set.'); |
97 | } |
98 | |
99 | $args = $this->formatArguments(); |
100 | |
101 | $this->output = PHP_EOL . (($this->docblock !== null) ? $this->docblock->render() : null); |
102 | if ($this->closure) { |
103 | $this->output .= $this->printIndent() . '$' . $this->name .' = function(' . $args . ')'; |
104 | } else { |
105 | $this->output .= $this->printIndent() . 'function ' . $this->name . '(' . $args . ')'; |
106 | } |
107 | |
108 | if ($this->hasReturnTypes()) { |
109 | $this->output .= ': ' . implode('|', $this->returnTypes); |
110 | } |
111 | |
112 | $this->output .= PHP_EOL . $this->printIndent() . '{' . PHP_EOL; |
113 | $this->output .= $this->body. PHP_EOL; |
114 | $this->output .= $this->printIndent() . '}'; |
115 | |
116 | if ($this->closure) { |
117 | $this->output .= ';'; |
118 | } |
119 | |
120 | $this->output .= PHP_EOL; |
121 | |
122 | return $this->output; |
123 | } |
124 | |
125 | /** |
126 | * Print function |
127 | * |
128 | * @return string |
129 | */ |
130 | public function __toString(): string |
131 | { |
132 | return $this->render(); |
133 | } |
134 | |
135 | } |