Completed
Push — master ( 713b15...55b21a )
by Thomas
03:46
created

CodeFileGenerator::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 11
ccs 7
cts 8
cp 0.875
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 3.0175
1
<?php
2
namespace gossi\codegen\generator;
3
4
use gossi\codegen\config\CodeFileGeneratorConfig;
5
use gossi\codegen\model\GenerateableInterface;
6
use gossi\docblock\Docblock;
7
8
/**
9
 * Code file generator.
10
 *
11
 * Generates code for a model and puts it into a file with `<?php` statements. Can also
12
 * generate header comments.
13
 *
14
 * @author Thomas Gossmann
15
 */
16
class CodeFileGenerator extends CodeGenerator {
17
18
	/**
19
	 * Creates a new CodeFileGenerator
20
	 *
21
	 * @see https://php-code-generator.readthedocs.org/en/latest/generator.html
22
	 * @param CodeFileGeneratorConfig|array $config
23
	 */
24 6
	public function __construct($config = null) {
25 6
		if (is_array($config)) {
26 4
			$this->config = new CodeFileGeneratorConfig($config);
27 2
		} else if ($config instanceof CodeFileGeneratorConfig) {
28
			$this->config = $config;
29
		} else {
30 2
			$this->config = new CodeFileGeneratorConfig();
31
		}
32
33 6
		$this->init();
34 6
	}
35
36
	/**
37
	 * {@inheritDoc}
38
	 *
39
	 * @return CodeFileGeneratorConfig
40
	 */
41
	public function getConfig() {
42
		return $this->config;
43
	}
44
45
	/**
46
	 * {@inheritDoc}
47
	 */
48 6
	public function generate(GenerateableInterface $model) {
49 6
		$content = "<?php\n";
50
51 6
		$comment = $this->config->getHeaderComment();
52 6
		if (!empty($comment)) {
53
			$docblock = new Docblock();
54
			$docblock->setLongDescription($comment);
55
			$content .= str_replace('/**', '/*', $docblock->toString()) . "\n";
56
		}
57
58 6
		if ($this->config->getHeaderDocblock() instanceof Docblock) {
59
			$content .= $this->config->getHeaderDocblock()->toString() . "\n";
60
		}
61
62 6
		if ($this->config->getDeclareStrictTypes()) {
63 1
			$content .= "declare(strict_types=1);\n\n";
64
		}
65
66 6
		$content .= parent::generate($model);
67
68 6
		if ($this->config->getBlankLineAtEnd()) {
69 6
			$content .= "\n";
70
		}
71
72 6
		return $content;
73
	}
74
}
75