Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| MethodGenerator | |
100.00% |
19 / 19 |
|
100.00% |
3 / 3 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
7 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Code\Generator; |
| 15 | |
| 16 | /** |
| 17 | * Namespace generator class |
| 18 | * |
| 19 | * @category Pop |
| 20 | * @package Pop\Code |
| 21 | * @author Nick Sagona, III <dev@noladev.com> |
| 22 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 23 | * @license https://www.popphp.org/license New BSD License |
| 24 | * @version 5.0.4 |
| 25 | */ |
| 26 | class MethodGenerator extends AbstractClassElementGenerator |
| 27 | { |
| 28 | |
| 29 | use Traits\NameTrait, Traits\DocblockTrait, Traits\AbstractFinalTrait, Traits\FunctionTrait, Traits\BodyTrait; |
| 30 | |
| 31 | /** |
| 32 | * Method body |
| 33 | * @var ?string string |
| 34 | */ |
| 35 | protected ?string $body = null; |
| 36 | |
| 37 | /** |
| 38 | * Constructor |
| 39 | * |
| 40 | * Instantiate the method generator object |
| 41 | * |
| 42 | * @param string $name |
| 43 | * @param string $visibility |
| 44 | * @param bool $static |
| 45 | * @throws Exception |
| 46 | */ |
| 47 | public function __construct(string $name, string $visibility = 'public', bool $static = false) |
| 48 | { |
| 49 | $this->setName($name); |
| 50 | $this->setVisibility($visibility); |
| 51 | $this->setAsStatic($static); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Render method |
| 56 | * |
| 57 | * @return string |
| 58 | */ |
| 59 | public function render(): string |
| 60 | { |
| 61 | $final = ($this->final) ? 'final ' : null; |
| 62 | $abstract = ($this->abstract) ? 'abstract ' : null; |
| 63 | $static = ($this->static) ? ' static' : null; |
| 64 | $args = $this->formatArguments(); |
| 65 | |
| 66 | $this->output = PHP_EOL . (($this->docblock !== null) ? $this->docblock->render() : null); |
| 67 | $this->output .= $this->printIndent() . $final . $abstract . $this->visibility . |
| 68 | $static . ' function ' . $this->name . '(' . $args . ')'; |
| 69 | |
| 70 | if ($this->hasReturnTypes()) { |
| 71 | $this->output .= ': ' . implode('|', $this->returnTypes); |
| 72 | } |
| 73 | |
| 74 | if (!empty($this->body)) { |
| 75 | $this->output .= PHP_EOL . $this->printIndent() . '{' . PHP_EOL; |
| 76 | $this->output .= $this->body. PHP_EOL; |
| 77 | $this->output .= $this->printIndent() . '}'; |
| 78 | } else { |
| 79 | $this->output .= ';'; |
| 80 | } |
| 81 | |
| 82 | return $this->output; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Print method |
| 87 | * |
| 88 | * @return string |
| 89 | */ |
| 90 | public function __toString(): string |
| 91 | { |
| 92 | return $this->render(); |
| 93 | } |
| 94 | |
| 95 | } |