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