Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| NamespaceGenerator | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| render | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
5 | |||
| __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-2026 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-2026 NOLA Interactive, LLC. |
| 23 | * @license https://www.popphp.org/license New BSD License |
| 24 | * @version 5.0.5 |
| 25 | */ |
| 26 | class NamespaceGenerator extends AbstractGenerator |
| 27 | { |
| 28 | |
| 29 | use Traits\NameTrait, Traits\DocblockTrait, Traits\UseTrait; |
| 30 | |
| 31 | /** |
| 32 | * Constructor |
| 33 | * |
| 34 | * Instantiate the namespace generator object |
| 35 | * |
| 36 | * @param ?string $namespace |
| 37 | * @param int $indent |
| 38 | */ |
| 39 | public function __construct(?string $namespace = null, int $indent = 0) |
| 40 | { |
| 41 | if ($namespace !== null) { |
| 42 | $this->setName($namespace); |
| 43 | } |
| 44 | $this->setIndent($indent); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Render namespace |
| 49 | * |
| 50 | * @return string |
| 51 | */ |
| 52 | public function render(): string |
| 53 | { |
| 54 | $this->docblock = new DocblockGenerator(null, $this->indent); |
| 55 | $this->docblock->addTag('namespace'); |
| 56 | |
| 57 | $this->output = $this->docblock->render(); |
| 58 | |
| 59 | if (!empty($this->name)) { |
| 60 | $this->output .= $this->printIndent() . 'namespace ' . $this->name . ';' . PHP_EOL; |
| 61 | } |
| 62 | |
| 63 | if ($this->hasUses()) { |
| 64 | $this->output .= PHP_EOL; |
| 65 | foreach ($this->uses as $ns => $as) { |
| 66 | $this->output .= $this->printIndent() . 'use '; |
| 67 | $this->output .= $ns; |
| 68 | if ($as !== null) { |
| 69 | $this->output .= ' as ' . $as; |
| 70 | } |
| 71 | $this->output .= ';' . PHP_EOL . PHP_EOL; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return $this->output; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Print namespace |
| 80 | * |
| 81 | * @return string |
| 82 | */ |
| 83 | public function __toString(): string |
| 84 | { |
| 85 | return $this->render(); |
| 86 | } |
| 87 | |
| 88 | } |