Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| BodyTrait | |
100.00% |
19 / 19 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
1 / 1 |
| setBody | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| appendToBody | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| indentBody | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| getBody | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasBody | |
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\Traits; |
| 16 | |
| 17 | /** |
| 18 | * Body trait |
| 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 | trait BodyTrait |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Function body |
| 32 | * @var ?string |
| 33 | */ |
| 34 | protected ?string $body = null; |
| 35 | |
| 36 | /** |
| 37 | * Set the function body |
| 38 | * |
| 39 | * @param string $body |
| 40 | * @param int $indent |
| 41 | * @return static |
| 42 | */ |
| 43 | public function setBody(string $body, int $indent = 4): static |
| 44 | { |
| 45 | $this->body = $this->printIndent() . str_repeat(' ', $indent) . |
| 46 | str_replace(PHP_EOL, PHP_EOL . $this->printIndent() . str_repeat(' ', $indent), $body); |
| 47 | return $this; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Append to the function body |
| 52 | * |
| 53 | * @param string $body |
| 54 | * @return static |
| 55 | */ |
| 56 | public function appendToBody(string $body): static |
| 57 | { |
| 58 | $body = str_replace(PHP_EOL, PHP_EOL . $this->printIndent() . ' ', $body); |
| 59 | $this->body .= PHP_EOL . $this->printIndent() . ' ' . $body; |
| 60 | return $this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Append to the function body |
| 65 | * |
| 66 | * @param int $indent |
| 67 | * @return static |
| 68 | */ |
| 69 | public function indentBody(int $indent): static |
| 70 | { |
| 71 | $indent = (int)$indent; |
| 72 | |
| 73 | if ($indent > 0) { |
| 74 | $this->body = str_repeat(' ', $indent) . str_replace(PHP_EOL, PHP_EOL . str_repeat(' ', $indent), $this->body); |
| 75 | } else if ($indent < 0) { |
| 76 | $indent = abs($indent); |
| 77 | $bodyLines = explode(PHP_EOL, $this->body); |
| 78 | foreach ($bodyLines as $i => $bodyLine) { |
| 79 | if (substr($bodyLine, 0, $indent) == str_repeat(' ', $indent)) { |
| 80 | $bodyLines[$i] = substr($bodyLine, $indent); |
| 81 | } |
| 82 | } |
| 83 | $this->body = implode(PHP_EOL, $bodyLines); |
| 84 | } |
| 85 | |
| 86 | return $this; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get the function body |
| 91 | * |
| 92 | * @return string|null |
| 93 | */ |
| 94 | public function getBody(): string|null |
| 95 | { |
| 96 | return $this->body; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Has method body |
| 101 | * |
| 102 | * @return bool |
| 103 | */ |
| 104 | public function hasBody(): bool |
| 105 | { |
| 106 | return ($this->body !== null); |
| 107 | } |
| 108 | |
| 109 | } |