Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
98.55% covered (success)
98.55%
68 / 69
93.33% covered (success)
93.33%
14 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClassGenerator
98.55% covered (success)
98.55%
68 / 69
93.33% covered (success)
93.33%
14 / 15
37
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 setParent
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getParent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasParent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addInterface
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 addInterfaces
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getInterfaces
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasInterfaces
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasInterface
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeInterface
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 render
96.55% covered (success)
96.55%
28 / 29
0.00% covered (danger)
0.00%
0 / 1
13
 formatConstants
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 formatProperties
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 formatMethods
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 __toString
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-2023 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;
15
16/**
17 * Class generator class
18 *
19 * @category   Pop
20 * @package    Pop\Code
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    4.1.0
25 */
26class ClassGenerator extends AbstractClassGenerator
27{
28
29    use Traits\UseTrait, Traits\PropertiesTrait, Traits\AbstractFinalTrait;
30
31    /**
32     * Parent class that is extended
33     * @var string
34     */
35    protected $parent = null;
36
37    /**
38     * Interfaces that are implemented
39     * @var array
40     */
41    protected $interfaces = [];
42
43    /**
44     * Constructor
45     *
46     * Instantiate the class generator object
47     *
48     * @param  string  $name
49     * @param  string  $parent
50     * @param  mixed   $interface
51     * @param  boolean $abstract
52     */
53    public function __construct($name, $parent = null, $interface = null, $abstract = false)
54    {
55        $this->setName($name);
56
57        if (null !== $parent) {
58            $this->setParent($parent);
59        }
60
61        if (null !== $interface) {
62            if (is_array($interface)) {
63                $this->addInterfaces($interface);
64            } else if (strpos($interface, ',') !== false) {
65                $this->addInterfaces(array_map('trim', explode(',', $interface)));
66            } else {
67                $this->addInterface($interface);
68            }
69        }
70
71        $this->setAsAbstract($abstract);
72    }
73
74    /**
75     * Set the class parent
76     *
77     * @param  string $parent
78     * @return ClassGenerator
79     */
80    public function setParent($parent = null)
81    {
82        $this->parent = $parent;
83        return $this;
84    }
85
86    /**
87     * Get the class parent
88     *
89     * @return string
90     */
91    public function getParent()
92    {
93        return $this->parent;
94    }
95
96    /**
97     * Has parent
98     *
99     * @return boolean
100     */
101    public function hasParent()
102    {
103        return (null !== $this->parent);
104    }
105
106    /**
107     * Add a class interface
108     *
109     * @param  string $interface
110     * @return ClassGenerator
111     */
112    public function addInterface($interface)
113    {
114        if (!in_array($interface, $this->interfaces)) {
115            $this->interfaces[] = $interface;
116        }
117
118        return $this;
119    }
120
121    /**
122     * Add a class interface
123     *
124     * @param  array $interfaces
125     * @return ClassGenerator
126     */
127    public function addInterfaces(array $interfaces)
128    {
129        foreach ($interfaces as $interface) {
130            $this->addInterface($interface);
131        }
132
133        return $this;
134    }
135
136    /**
137     * Get the class interfaces
138     *
139     * @return array
140     */
141    public function getInterfaces()
142    {
143        return $this->interfaces;
144    }
145
146    /**
147     * Has class interfaces
148     *
149     * @return boolean
150     */
151    public function hasInterfaces()
152    {
153        return (!empty($this->interfaces));
154    }
155
156    /**
157     * Has class interface
158     *
159     * @param  string $interface
160     * @return boolean
161     */
162    public function hasInterface($interface)
163    {
164        return (in_array($interface, $this->interfaces));
165    }
166
167    /**
168     * Remove class interface
169     *
170     * @param  string $interface
171     * @return ClassGenerator
172     */
173    public function removeInterface($interface)
174    {
175        if (in_array($interface, $this->interfaces)) {
176            $key = array_search($interface, $this->interfaces);
177            unset($this->interfaces[$key]);
178        }
179
180        return $this;
181    }
182
183    /**
184     * Render class
185     *
186     * @return string
187     */
188    public function render()
189    {
190        $classKeyword = null;
191
192        if ($this->isAbstract()) {
193            $classKeyword = 'abstract ';
194        } else if ($this->isFinal()) {
195            $classKeyword = 'final ';
196        }
197
198        $this->output  = (null !== $this->namespace) ? $this->namespace->render() . PHP_EOL : null;
199        $this->output .= (null !== $this->docblock) ? $this->docblock->render() : null;
200        $this->output .= $classKeyword . 'class ' . $this->name;
201
202        if (null !== $this->parent) {
203            $this->output .= ' extends ' . $this->parent;
204        }
205        if (!empty($this->interfaces)) {
206            $this->output .= ' implements ' . implode(', ', $this->interfaces);
207        }
208
209        $this->output .= PHP_EOL . '{' . PHP_EOL;
210
211        if ($this->hasUses()) {
212            $this->output .= PHP_EOL;
213            foreach ($this->uses as $ns => $as) {
214                $this->output .= $this->printIndent() . 'use ';
215                $this->output .= $ns;
216                if (null !== $as) {
217                    $this->output .= ' as ' . $as;
218                }
219                $this->output .= ';' . PHP_EOL;
220            }
221        }
222
223        if ($this->hasConstants()) {
224            $this->output .= $this->formatConstants();
225        }
226        if ($this->hasProperties()) {
227            $this->output .= $this->formatProperties();
228        }
229        if ($this->hasMethods()) {
230            $this->output .= $this->formatMethods();
231        }
232
233        $this->output .= PHP_EOL . '}' . PHP_EOL;
234
235        return $this->output;
236    }
237
238    /**
239     * Format the constants
240     *
241     * @return string
242     */
243    protected function formatConstants()
244    {
245        $constants = null;
246
247        foreach ($this->constants as $constant) {
248            $constants .= $constant->render() . PHP_EOL;
249        }
250
251        return $constants;
252    }
253
254    /**
255     * Format the properties
256     *
257     * @return string
258     */
259    protected function formatProperties()
260    {
261        $props = null;
262
263        foreach ($this->properties as $prop) {
264            $props .= $prop->render() . PHP_EOL;
265        }
266
267        return $props;
268    }
269
270    /**
271     * Format the methods
272     *
273     * @return string
274     */
275    protected function formatMethods()
276    {
277        $methods = null;
278
279        foreach ($this->methods as $method) {
280            $methods .= $method->render() . PHP_EOL;
281        }
282
283        return $methods;
284    }
285
286    /**
287     * Print class
288     *
289     * @return string
290     */
291    public function __toString()
292    {
293        return $this->render();
294    }
295
296}