Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.77% |
30 / 31 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| TraitGenerator | |
96.77% |
30 / 31 |
|
83.33% |
5 / 6 |
14 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| render | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
6.01 | |||
| 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 | /** |
| 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 | * Trait 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 TraitGenerator extends AbstractClassGenerator |
| 27 | { |
| 28 | |
| 29 | use Traits\UseTrait, Traits\PropertiesTrait; |
| 30 | |
| 31 | /** |
| 32 | * Constructor |
| 33 | * |
| 34 | * Instantiate the trait generator object |
| 35 | * |
| 36 | * @param string $name |
| 37 | */ |
| 38 | public function __construct(string $name) |
| 39 | { |
| 40 | $this->setName($name); |
| 41 | } |
| 42 | /** |
| 43 | * Render class |
| 44 | * |
| 45 | * @return string |
| 46 | */ |
| 47 | public function render(): string |
| 48 | { |
| 49 | $this->output = ($this->namespace !== null) ? $this->namespace->render() . PHP_EOL : null; |
| 50 | $this->output .= ($this->docblock !== null) ? $this->docblock->render() : null; |
| 51 | $this->output .= 'trait ' . $this->name; |
| 52 | |
| 53 | $this->output .= PHP_EOL . '{'; |
| 54 | |
| 55 | if ($this->hasUses()) { |
| 56 | $this->output .= PHP_EOL; |
| 57 | foreach ($this->uses as $ns => $as) { |
| 58 | $this->output .= $this->printIndent() . 'use '; |
| 59 | $this->output .= $ns; |
| 60 | if ($as !== null) { |
| 61 | $this->output .= ' as ' . $as; |
| 62 | } |
| 63 | $this->output .= ';' . PHP_EOL; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $this->output .= $this->formatConstants() . PHP_EOL; |
| 68 | $this->output .= $this->formatProperties() . PHP_EOL; |
| 69 | $this->output .= $this->formatMethods() . PHP_EOL; |
| 70 | $this->output .= '}' . PHP_EOL; |
| 71 | |
| 72 | return $this->output; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Format the constants |
| 77 | * |
| 78 | * @return string |
| 79 | */ |
| 80 | protected function formatConstants(): string |
| 81 | { |
| 82 | $constants = null; |
| 83 | |
| 84 | foreach ($this->constants as $constant) { |
| 85 | $constants .= PHP_EOL . $constant->render(); |
| 86 | } |
| 87 | |
| 88 | return $constants; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Format the properties |
| 93 | * |
| 94 | * @return string |
| 95 | */ |
| 96 | protected function formatProperties(): string |
| 97 | { |
| 98 | $props = null; |
| 99 | |
| 100 | foreach ($this->properties as $prop) { |
| 101 | $props .= PHP_EOL . $prop->render(); |
| 102 | } |
| 103 | |
| 104 | return $props; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Format the methods |
| 109 | * |
| 110 | * @return string |
| 111 | */ |
| 112 | protected function formatMethods(): string |
| 113 | { |
| 114 | $methods = null; |
| 115 | |
| 116 | foreach ($this->methods as $method) { |
| 117 | $methods .= PHP_EOL . $method->render(); |
| 118 | } |
| 119 | |
| 120 | return $methods; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Print class |
| 125 | * |
| 126 | * @return string |
| 127 | */ |
| 128 | public function __toString(): string |
| 129 | { |
| 130 | return $this->render(); |
| 131 | } |
| 132 | |
| 133 | } |