Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
MethodGenerator
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
4 / 4
14
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
1
 addPromotedArgument
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 render
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
7
 __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 * Namespace 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 MethodGenerator extends AbstractClassElementGenerator
28{
29
30    use Traits\AbstractFinalTrait, Traits\FunctionTrait, Traits\BodyTrait;
31
32    /**
33     * Method body
34     * @var ?string string
35     */
36    protected ?string $body = null;
37
38    /**
39     * Constructor
40     *
41     * Instantiate the method generator object
42     *
43     * @param  string $name
44     * @param  string $visibility
45     * @param  bool $static
46     * @throws Exception
47     */
48    public function __construct(string $name, string $visibility = 'public', bool $static = false)
49    {
50        $this->setName($name);
51        $this->setVisibility($visibility);
52        $this->setAsStatic($static);
53    }
54
55    /**
56     * Add a promoted constructor argument
57     *
58     * @param  string  $name
59     * @param  string  $visibility
60     * @param  mixed   $value
61     * @param  ?string $type
62     * @param  bool    $readonly
63     * @param  array   $attributes
64     * @throws Exception
65     * @return MethodGenerator
66     */
67    public function addPromotedArgument(
68        string $name, string $visibility, mixed $value = new NoValue(), ?string $type = null, bool $readonly = false,
69        array $attributes = []
70    ): MethodGenerator
71    {
72        if ($this->name !== '__construct') {
73            throw new Exception('Error: Promoted arguments are only valid on a constructor.');
74        }
75
76        $visibility = strtolower($visibility);
77        if (!in_array($visibility, self::VALID_VISIBILITIES)) {
78            throw new Exception("Error: The visibility '" . $visibility . "' is not allowed.");
79        }
80
81        if ($readonly && ($type === null)) {
82            throw new Exception('Error: A readonly property must have a type.');
83        }
84
85        $this->addArgument($name, $value, $type, false, false, $attributes);
86        $this->arguments[$name]['promotedVisibility'] = $visibility;
87        $this->arguments[$name]['promotedReadonly']   = $readonly;
88
89        return $this;
90    }
91
92    /**
93     * Render method
94     *
95     * @return string
96     */
97    public function render(): string
98    {
99        $final    = ($this->final) ? 'final ' : null;
100        $abstract = ($this->abstract) ? 'abstract ' : null;
101        $static   = ($this->static) ? ' static' : null;
102        $args     = $this->formatArguments();
103
104        $this->output = PHP_EOL . (($this->docblock !== null) ? $this->docblock->render() : null);
105        $this->output .= $this->formatAttributes();
106        $this->output .= $this->printIndent() . $final . $abstract . $this->visibility .
107            $static . ' function ' . $this->name . '(' . $args . ')';
108
109        if ($this->hasReturnTypes()) {
110            $this->output .= ': ' . implode('|', $this->returnTypes);
111        }
112
113        if (!empty($this->body)) {
114            $this->output .= PHP_EOL . $this->printIndent() . '{' . PHP_EOL;
115            $this->output .= $this->body. PHP_EOL;
116            $this->output .= $this->printIndent() . '}';
117        } else {
118            $this->output .= ';';
119        }
120
121        return $this->output;
122    }
123
124    /**
125     * Print method
126     *
127     * @return string
128     */
129    public function __toString(): string
130    {
131        return $this->render();
132    }
133
134}