Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
BodyTrait
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 setBody
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 appendToBody
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 indentBody
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 getBody
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasBody
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Code\Generator\Traits;
15
16/**
17 * Body trait
18 *
19 * @category   Pop
20 * @package    Pop\Code
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    5.0.0
25 */
26trait BodyTrait
27{
28
29    /**
30     * Function body
31     * @var ?string
32     */
33    protected ?string $body = null;
34
35    /**
36     * Set the function body
37     *
38     * @param  string $body
39     * @param  int    $indent
40     * @return static
41     */
42    public function setBody(string $body, int $indent = 4): static
43    {
44        $this->body = $this->printIndent() . str_repeat(' ', $indent) .
45            str_replace(PHP_EOL, PHP_EOL . $this->printIndent() . str_repeat(' ', $indent), $body);
46        return $this;
47    }
48
49    /**
50     * Append to the function body
51     *
52     * @param  string $body
53     * @return static
54     */
55    public function appendToBody(string $body): static
56    {
57        $body = str_replace(PHP_EOL, PHP_EOL . $this->printIndent() . '    ', $body);
58        $this->body .= PHP_EOL . $this->printIndent() . '    ' . $body;
59        return $this;
60    }
61
62    /**
63     * Append to the function body
64     *
65     * @param  int $indent
66     * @return static
67     */
68    public function indentBody(int $indent): static
69    {
70        $indent = (int)$indent;
71
72        if ($indent > 0) {
73            $this->body = str_repeat(' ', $indent) . str_replace(PHP_EOL, PHP_EOL . str_repeat(' ', $indent), $this->body);
74        } else if ($indent < 0) {
75            $indent    = abs($indent);
76            $bodyLines = explode(PHP_EOL, $this->body);
77            foreach ($bodyLines as $i => $bodyLine) {
78                if (substr($bodyLine, 0, $indent) == str_repeat(' ', $indent)) {
79                    $bodyLines[$i] = substr($bodyLine, $indent);
80                }
81            }
82            $this->body = implode(PHP_EOL, $bodyLines);
83        }
84
85        return $this;
86    }
87
88    /**
89     * Get the function body
90     *
91     * @return string|null
92     */
93    public function getBody(): string|null
94    {
95        return $this->body;
96    }
97
98    /**
99     * Has method body
100     *
101     * @return bool
102     */
103    public function hasBody(): bool
104    {
105        return ($this->body !== null);
106    }
107
108}