Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
42 / 42 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| InterfaceGenerator | |
100.00% |
42 / 42 |
|
100.00% |
11 / 11 |
24 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| addParent | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| addParents | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getParents | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasParents | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasParent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| removeParent | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| render | |
100.00% |
13 / 13 |
|
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 | declare(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 | */ |
| 15 | namespace Pop\Code\Generator; |
| 16 | |
| 17 | /** |
| 18 | * Interface 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 | */ |
| 27 | class InterfaceGenerator extends AbstractClassGenerator |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Parent interfaces that are extended -- a PHP interface can extend more than one interface |
| 32 | * at once (`interface Foo extends A, B`), unlike a class, which can only extend one |
| 33 | * @var array |
| 34 | */ |
| 35 | protected array $parents = []; |
| 36 | |
| 37 | /** |
| 38 | * Constructor |
| 39 | * |
| 40 | * Instantiate the interface generator object |
| 41 | * |
| 42 | * @param string $name |
| 43 | * @param mixed $parent |
| 44 | */ |
| 45 | public function __construct(string $name, mixed $parent = null) |
| 46 | { |
| 47 | $this->setName($name); |
| 48 | |
| 49 | if ($parent !== null) { |
| 50 | if (is_array($parent)) { |
| 51 | $this->addParents($parent); |
| 52 | } else if (str_contains($parent, ',')) { |
| 53 | $this->addParents(array_map('trim', explode(',', $parent))); |
| 54 | } else { |
| 55 | $this->addParent($parent); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Add a parent interface |
| 62 | * |
| 63 | * @param string $parent |
| 64 | * @return InterfaceGenerator |
| 65 | */ |
| 66 | public function addParent(string $parent): InterfaceGenerator |
| 67 | { |
| 68 | if (!in_array($parent, $this->parents)) { |
| 69 | $this->parents[] = $parent; |
| 70 | } |
| 71 | |
| 72 | return $this; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Add parent interfaces |
| 77 | * |
| 78 | * @param array $parents |
| 79 | * @return InterfaceGenerator |
| 80 | */ |
| 81 | public function addParents(array $parents): InterfaceGenerator |
| 82 | { |
| 83 | foreach ($parents as $parent) { |
| 84 | $this->addParent($parent); |
| 85 | } |
| 86 | |
| 87 | return $this; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the parent interfaces |
| 92 | * |
| 93 | * @return array |
| 94 | */ |
| 95 | public function getParents(): array |
| 96 | { |
| 97 | return $this->parents; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Has parent interfaces |
| 102 | * |
| 103 | * @return bool |
| 104 | */ |
| 105 | public function hasParents(): bool |
| 106 | { |
| 107 | return (!empty($this->parents)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Has a specific parent interface |
| 112 | * |
| 113 | * @param string $parent |
| 114 | * @return bool |
| 115 | */ |
| 116 | public function hasParent(string $parent): bool |
| 117 | { |
| 118 | return (in_array($parent, $this->parents)); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Remove a parent interface |
| 123 | * |
| 124 | * @param string $parent |
| 125 | * @return InterfaceGenerator |
| 126 | */ |
| 127 | public function removeParent(string $parent): InterfaceGenerator |
| 128 | { |
| 129 | if (in_array($parent, $this->parents)) { |
| 130 | $key = array_search($parent, $this->parents); |
| 131 | unset($this->parents[$key]); |
| 132 | } |
| 133 | |
| 134 | return $this; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Render interface |
| 139 | * |
| 140 | * @return string |
| 141 | */ |
| 142 | public function render(): string |
| 143 | { |
| 144 | $this->output = ($this->namespace !== null) ? $this->namespace->render() . PHP_EOL : null; |
| 145 | $this->output .= ($this->docblock !== null) ? $this->docblock->render() : null; |
| 146 | $this->output .= $this->formatAttributes(false); |
| 147 | $this->output .= 'interface ' . $this->name; |
| 148 | |
| 149 | if ($this->hasParents()) { |
| 150 | $this->output .= ' extends ' . implode(', ', $this->parents); |
| 151 | } |
| 152 | |
| 153 | $this->output .= PHP_EOL . '{' . PHP_EOL; |
| 154 | |
| 155 | if ($this->hasConstants()) { |
| 156 | $this->output .= $this->formatConstants(); |
| 157 | } |
| 158 | if ($this->hasMethods()) { |
| 159 | $this->output .= $this->formatMethods(); |
| 160 | } |
| 161 | |
| 162 | $this->output .= PHP_EOL . '}' . PHP_EOL; |
| 163 | |
| 164 | return $this->output; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Format the constants |
| 169 | * |
| 170 | * @return string |
| 171 | */ |
| 172 | protected function formatConstants(): string |
| 173 | { |
| 174 | $constants = null; |
| 175 | |
| 176 | foreach ($this->constants as $constant) { |
| 177 | $constants .= $constant->render() . PHP_EOL; |
| 178 | } |
| 179 | |
| 180 | return $constants; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Format the methods |
| 185 | * |
| 186 | * @return string |
| 187 | */ |
| 188 | protected function formatMethods(): string |
| 189 | { |
| 190 | $methods = null; |
| 191 | |
| 192 | foreach ($this->methods as $method) { |
| 193 | $methods .= $method->render() . PHP_EOL; |
| 194 | } |
| 195 | |
| 196 | return $methods; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Print interface |
| 201 | * |
| 202 | * @return string |
| 203 | */ |
| 204 | public function __toString(): string |
| 205 | { |
| 206 | return $this->render(); |
| 207 | } |
| 208 | |
| 209 | } |