Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| TraitGenerator | |
100.00% |
31 / 31 |
|
100.00% |
6 / 6 |
16 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
8 | |||
| formatConstants | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| formatProperties | |
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 | * Trait 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 TraitGenerator extends AbstractClassGenerator |
| 28 | { |
| 29 | |
| 30 | use Traits\UseTrait, Traits\PropertiesTrait; |
| 31 | |
| 32 | /** |
| 33 | * Constructor |
| 34 | * |
| 35 | * Instantiate the trait generator object |
| 36 | * |
| 37 | * @param string $name |
| 38 | */ |
| 39 | public function __construct(string $name) |
| 40 | { |
| 41 | $this->setName($name); |
| 42 | } |
| 43 | /** |
| 44 | * Render class |
| 45 | * |
| 46 | * @return string |
| 47 | */ |
| 48 | public function render(): string |
| 49 | { |
| 50 | $this->output = ($this->namespace !== null) ? $this->namespace->render() . PHP_EOL : null; |
| 51 | $this->output .= ($this->docblock !== null) ? $this->docblock->render() : null; |
| 52 | $this->output .= $this->formatAttributes(false); |
| 53 | $this->output .= 'trait ' . $this->name; |
| 54 | |
| 55 | $this->output .= PHP_EOL . '{'; |
| 56 | |
| 57 | if ($this->hasUses()) { |
| 58 | $this->output .= PHP_EOL; |
| 59 | foreach ($this->uses as $ns => $as) { |
| 60 | // Unlike a namespace-level `use Foo\Bar as Baz;` import, a trait-use inside a |
| 61 | // class-like body has no whole-trait aliasing syntax -- `use SomeTrait as Alias;` |
| 62 | // here is not valid PHP (only per-method conflict resolution, `use A, B { A::m as |
| 63 | // n; }`, exists, and this simple $ns => $as map has no way to represent that). Any |
| 64 | // alias is therefore intentionally ignored when rendering a trait-use. |
| 65 | $this->output .= $this->printIndent() . 'use ' . $ns . ';' . PHP_EOL; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if ($this->hasConstants()) { |
| 70 | $this->output .= $this->formatConstants() . PHP_EOL; |
| 71 | } |
| 72 | if ($this->hasProperties()) { |
| 73 | $this->output .= $this->formatProperties() . PHP_EOL; |
| 74 | } |
| 75 | if ($this->hasMethods()) { |
| 76 | $this->output .= $this->formatMethods() . PHP_EOL; |
| 77 | } |
| 78 | $this->output .= '}' . PHP_EOL; |
| 79 | |
| 80 | return $this->output; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Format the constants |
| 85 | * |
| 86 | * @return string |
| 87 | */ |
| 88 | protected function formatConstants(): string |
| 89 | { |
| 90 | $constants = ''; |
| 91 | |
| 92 | foreach ($this->constants as $constant) { |
| 93 | $constants .= PHP_EOL . $constant->render(); |
| 94 | } |
| 95 | |
| 96 | return $constants; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Format the properties |
| 101 | * |
| 102 | * @return string |
| 103 | */ |
| 104 | protected function formatProperties(): string |
| 105 | { |
| 106 | $props = ''; |
| 107 | |
| 108 | foreach ($this->properties as $prop) { |
| 109 | $props .= PHP_EOL . $prop->render(); |
| 110 | } |
| 111 | |
| 112 | return $props; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Format the methods |
| 117 | * |
| 118 | * @return string |
| 119 | */ |
| 120 | protected function formatMethods(): string |
| 121 | { |
| 122 | $methods = ''; |
| 123 | |
| 124 | foreach ($this->methods as $method) { |
| 125 | $methods .= PHP_EOL . $method->render(); |
| 126 | } |
| 127 | |
| 128 | return $methods; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Print class |
| 133 | * |
| 134 | * @return string |
| 135 | */ |
| 136 | public function __toString(): string |
| 137 | { |
| 138 | return $this->render(); |
| 139 | } |
| 140 | |
| 141 | } |