CodeFileGenerator::generate()   B
last analyzed

Complexity

Conditions 8
Paths 16

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8

Importance

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