Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
27 / 27 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| InterfaceGenerator | |
100.00% |
27 / 27 |
|
100.00% |
8 / 8 |
15 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setParent | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getParent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasParent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
6 | |||
| formatConstants | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| formatMethods | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| __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 | * Interface 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 InterfaceGenerator extends AbstractClassGenerator |
| 27 | { |
| 28 | |
| 29 | /** |
| 30 | * Parent interfaces that are extended |
| 31 | * @var ?string |
| 32 | */ |
| 33 | protected ?string $parent = null; |
| 34 | |
| 35 | /** |
| 36 | * Constructor |
| 37 | * |
| 38 | * Instantiate the interface generator object |
| 39 | * |
| 40 | * @param string $name |
| 41 | * @param ?string $parent |
| 42 | */ |
| 43 | public function __construct(string $name, ?string $parent = null) |
| 44 | { |
| 45 | $this->setName($name); |
| 46 | $this->setParent($parent); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Set the interface parent |
| 51 | * |
| 52 | * @param ?string $parent |
| 53 | * @return InterfaceGenerator |
| 54 | */ |
| 55 | public function setParent(?string $parent = null): InterfaceGenerator |
| 56 | { |
| 57 | $this->parent = $parent; |
| 58 | return $this; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the interface parent |
| 63 | * |
| 64 | * @return string|null |
| 65 | */ |
| 66 | public function getParent(): string|null |
| 67 | { |
| 68 | return $this->parent; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Has parent |
| 73 | * |
| 74 | * @return bool |
| 75 | */ |
| 76 | public function hasParent(): bool |
| 77 | { |
| 78 | return ($this->parent !== null); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Render interface |
| 83 | * |
| 84 | * @return string |
| 85 | */ |
| 86 | public function render(): string |
| 87 | { |
| 88 | $this->output = ($this->namespace !== null) ? $this->namespace->render() . PHP_EOL : null; |
| 89 | $this->output .= ($this->docblock !== null) ? $this->docblock->render() : null; |
| 90 | $this->output .= 'interface ' . $this->name; |
| 91 | |
| 92 | if ($this->parent !== null) { |
| 93 | $this->output .= ' extends ' . $this->parent; |
| 94 | } |
| 95 | |
| 96 | $this->output .= PHP_EOL . '{' . PHP_EOL; |
| 97 | |
| 98 | if ($this->hasConstants()) { |
| 99 | $this->output .= $this->formatConstants(); |
| 100 | } |
| 101 | if ($this->hasMethods()) { |
| 102 | $this->output .= $this->formatMethods(); |
| 103 | } |
| 104 | |
| 105 | $this->output .= PHP_EOL . '}' . PHP_EOL; |
| 106 | |
| 107 | return $this->output; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Format the constants |
| 112 | * |
| 113 | * @return string |
| 114 | */ |
| 115 | protected function formatConstants(): string |
| 116 | { |
| 117 | $constants = null; |
| 118 | |
| 119 | foreach ($this->constants as $constant) { |
| 120 | $constants .= $constant->render() . PHP_EOL; |
| 121 | } |
| 122 | |
| 123 | return $constants; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Format the methods |
| 128 | * |
| 129 | * @return string |
| 130 | */ |
| 131 | protected function formatMethods(): string |
| 132 | { |
| 133 | $methods = null; |
| 134 | |
| 135 | foreach ($this->methods as $method) { |
| 136 | $methods .= $method->render() . PHP_EOL; |
| 137 | } |
| 138 | |
| 139 | return $methods; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Print interface |
| 144 | * |
| 145 | * @return string |
| 146 | */ |
| 147 | public function __toString(): string |
| 148 | { |
| 149 | return $this->render(); |
| 150 | } |
| 151 | |
| 152 | } |