Completed
Push — develop ( 1b34c6...481302 )
by Paul
02:47
created

AbstractPhpRenderer   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 162
rs 10
c 0
b 0
f 0
wmc 15

11 Methods

Rating   Name   Duplication   Size   Complexity  
A increaseIndent() 0 4 1
A indent() 0 8 2
A decreaseIndent() 0 4 1
A multiple() 0 6 2
A remove() 0 4 1
A begin() 0 4 1
A concat() 0 4 1
A add() 0 8 2
A get() 0 3 1
A __construct() 0 3 1
A doc() 0 10 2
1
<?php
2
3
namespace PhpUnitGen\Renderer;
4
5
use PhpUnitGen\Configuration\ConfigurationInterface\ConfigInterface;
6
use PhpUnitGen\Renderer\Helper\RendererHelper;
0 ignored issues
show
Bug introduced by
The type PhpUnitGen\Renderer\Helper\RendererHelper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
/**
9
 * Class AbstractRendererInterface.
10
 *
11
 * @author     Paul Thébaud <[email protected]>.
12
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
13
 * @license    https://opensource.org/licenses/MIT The MIT license.
14
 * @link       https://github.com/paul-thebaud/phpunit-generator
15
 * @since      Class available since Release 2.0.0.
16
 */
17
abstract class AbstractPhpRenderer
18
{
19
    /**
20
     * @var ConfigInterface $config The configuration.
21
     */
22
    protected $config;
23
24
    /**
25
     * @var int $indent The indentation level.
26
     */
27
    protected $indent = 0;
28
29
    /**
30
     * @var string $content The content to show.
31
     */
32
    protected $content = '';
33
34
    /**
35
     * AbstractRendererInterface constructor.
36
     *
37
     * @param ConfigInterface $config The configuration to use.
38
     */
39
    public function __construct(ConfigInterface $config)
40
    {
41
        $this->config = $config;
42
    }
43
44
    /**
45
     * Begin a new render (clear content).
46
     *
47
     * @return AbstractPhpRenderer $this.
48
     */
49
    protected function begin(): AbstractPhpRenderer
50
    {
51
        $this->content = '';
52
        return $this;
53
    }
54
55
    /**
56
     * Add a new line to content.
57
     *
58
     * @param string $line The new line (optional).
59
     *
60
     * @return AbstractPhpRenderer $this.
61
     */
62
    protected function add(string $line = ''): AbstractPhpRenderer
63
    {
64
        if ($line === '') {
65
            $this->content .= "\n";
66
        } else {
67
            $this->content .= $this->indent($line) . "\n";
68
        }
69
        return $this;
70
    }
71
72
    /**
73
     * Add new lines to content.
74
     *
75
     * @param array $lines New lines.
76
     *
77
     * @return AbstractPhpRenderer $this.
78
     */
79
    protected function multiple(array $lines): AbstractPhpRenderer
80
    {
81
        foreach ($lines as $line) {
82
            $this->add($line);
83
        }
84
        return $this;
85
    }
86
87
    /**
88
     * Add documentation block to content.
89
     *
90
     * @param array $documentation The documentation lines.
91
     *
92
     * @return AbstractPhpRenderer $this.
93
     */
94
    protected function doc(array $documentation): AbstractPhpRenderer
95
    {
96
        $lines = ['/**'];
97
        foreach ($documentation as $line) {
98
            $lines[] = ' * ' . $line;
99
        }
100
        $lines[] = ' */';
101
102
        $this->multiple($lines);
103
        return $this;
104
    }
105
106
    /**
107
     * Add string at end of content.
108
     *
109
     * @param string $string The string to concat.
110
     *
111
     * @return AbstractPhpRenderer $this.
112
     */
113
    protected function concat(string $string): AbstractPhpRenderer
114
    {
115
        $this->content .= $string;
116
        return $this;
117
    }
118
119
    /**
120
     * Remove n character at the content string.
121
     *
122
     * @param int $number The number of char to remove.
123
     *
124
     * @return AbstractPhpRenderer $this.
125
     */
126
    protected function remove(int $number = 1): AbstractPhpRenderer
127
    {
128
        $this->content = substr($this->content, 0, (-$number));
129
        return $this;
130
    }
131
132
    /**
133
     * Increase the indent level.
134
     *
135
     * @return AbstractPhpRenderer $this.
136
     */
137
    protected function increaseIndent(): AbstractPhpRenderer
138
    {
139
        $this->indent++;
140
        return $this;
141
    }
142
143
    /**
144
     * Decrease the indent level.
145
     *
146
     * @return AbstractPhpRenderer $this.
147
     */
148
    protected function decreaseIndent(): AbstractPhpRenderer
149
    {
150
        $this->indent++;
151
        return $this;
152
    }
153
154
    /**
155
     * Get the content.
156
     *
157
     * @return string The rendered content.
158
     */
159
    protected function get(): string
160
    {
161
        return $this->content;
162
    }
163
164
    /**
165
     * Add indent to a line line.
166
     *
167
     * @param string $line The line to indent.
168
     *
169
     * @return string The indented line.
170
     */
171
    private function indent(string $line): string
172
    {
173
        $indent = $this->indent;
174
        while ($indent > 0) {
175
            $line = '    ' . $line;
176
            $indent--;
177
        }
178
        return $line;
179
    }
180
}
181