Completed
Pull Request — master (#30)
by
unknown
03:31
created

CodeFileGenerator::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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