Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| NamespaceGenerator | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| render | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
7 | |||
| __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 | * 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 | */ |
| 27 | class NamespaceGenerator extends AbstractGenerator |
| 28 | { |
| 29 | |
| 30 | use Traits\NameTrait, Traits\DocblockTrait, Traits\UseTrait; |
| 31 | |
| 32 | /** |
| 33 | * Constructor |
| 34 | * |
| 35 | * Instantiate the namespace generator object |
| 36 | * |
| 37 | * @param ?string $namespace |
| 38 | * @param int $indent |
| 39 | */ |
| 40 | public function __construct(?string $namespace = null, int $indent = 0) |
| 41 | { |
| 42 | if ($namespace !== null) { |
| 43 | $this->setName($namespace); |
| 44 | } |
| 45 | $this->setIndent($indent); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Render namespace |
| 50 | * |
| 51 | * @return string |
| 52 | */ |
| 53 | public function render(): string |
| 54 | { |
| 55 | // Only create a fresh docblock if one hasn't already been set -- render() used to |
| 56 | // unconditionally overwrite $this->docblock, silently discarding any docblock a caller |
| 57 | // had set via DocblockTrait's setDocblock()/setDesc(), despite this class mixing that |
| 58 | // trait in. |
| 59 | if ($this->docblock === null) { |
| 60 | $this->docblock = new DocblockGenerator(null, $this->indent); |
| 61 | } |
| 62 | if (!$this->docblock->hasTag('namespace')) { |
| 63 | $this->docblock->addTag('namespace'); |
| 64 | } |
| 65 | |
| 66 | $this->output = $this->docblock->render(); |
| 67 | |
| 68 | if (!empty($this->name)) { |
| 69 | $this->output .= $this->printIndent() . 'namespace ' . $this->name . ';' . PHP_EOL; |
| 70 | } |
| 71 | |
| 72 | if ($this->hasUses()) { |
| 73 | $this->output .= PHP_EOL; |
| 74 | foreach ($this->uses as $ns => $as) { |
| 75 | $this->output .= $this->printIndent() . 'use '; |
| 76 | $this->output .= $ns; |
| 77 | if ($as !== null) { |
| 78 | $this->output .= ' as ' . $as; |
| 79 | } |
| 80 | $this->output .= ';' . PHP_EOL . PHP_EOL; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return $this->output; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Print namespace |
| 89 | * |
| 90 | * @return string |
| 91 | */ |
| 92 | public function __toString(): string |
| 93 | { |
| 94 | return $this->render(); |
| 95 | } |
| 96 | |
| 97 | } |