Completed
Push — master ( 47b333...d67b8f )
by Thomas
02:24
created

CodeFileGeneratorConfig::getHeaderComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace gossi\codegen\config;
5
6
use gossi\docblock\Docblock;
7
use Symfony\Component\OptionsResolver\Options;
8
use Symfony\Component\OptionsResolver\OptionsResolver;
9
10
/**
11
 * Configuration for code file generation
12
 *
13
 * @author Thomas Gossmann
14
 */
15
class CodeFileGeneratorConfig extends CodeGeneratorConfig {
16
17 11
	protected function configureOptions(OptionsResolver $resolver): void {
18 11
		parent::configureOptions($resolver);
19
20 11
		$resolver->setDefaults([
21 11
			'headerComment' => null,
22
			'headerDocblock' => null,
23
			'blankLineAtEnd' => true,
24
			'declareStrictTypes'  => false,
25
			'generateScalarTypeHints' => function (Options $options) {
26 11
				return $options['declareStrictTypes'];
27 11
			},
28
			'generateReturnTypeHints' => function (Options $options) {
29 11
				return $options['declareStrictTypes'];
30 11
			},
31
		]);
32
33 11
		$resolver->setAllowedTypes('headerComment', ['null', 'string', 'gossi\\docblock\\Docblock']);
34 11
		$resolver->setAllowedTypes('headerDocblock', ['null', 'string', 'gossi\\docblock\\Docblock']);
35 11
		$resolver->setAllowedTypes('blankLineAtEnd', 'bool');
36 11
		$resolver->setAllowedTypes('declareStrictTypes', 'bool');
37
38
		$resolver->setNormalizer('headerComment', function (Options $options, $value) {
39 11
			return $this->toDocblock($value);
40 11
		});
41
		$resolver->setNormalizer('headerDocblock', function (Options $options, $value) {
42 11
			return $this->toDocblock($value);
43 11
		});
44 11
	}
45
46
	/**
47
	 *
48
	 * @param mixed $value
49
	 * @return Docblock|null
50
	 */
51 11
	private function toDocblock($value): ?Docblock {
52 11
		if (is_string($value)) {
53 1
			$value = Docblock::create()->setLongDescription($value);
54
		}
55
56 11
		return $value;
57
	}
58
59
	/**
60
	 * Returns the file header comment
61
	 *
62
	 * @return Docblock the header comment
63
	 */
64 8
	public function getHeaderComment(): ?Docblock {
65 8
		return $this->options['headerComment'];
66
	}
67
68
	/**
69
	 * Sets the file header comment
70
	 *
71
	 * @param Docblock $comment the header comment
72
	 * @return $this
73
	 */
74
	public function setHeaderComment(Docblock $comment) {
75
		$this->options['headerComment'] = $comment;
76
		return $this;
77
	}
78
79
	/**
80
	 * Returns the file header docblock
81
	 *
82
	 * @return Docblock the docblock
83
	 */
84 9
	public function getHeaderDocblock(): ?Docblock {
85 9
		return $this->options['headerDocblock'];
86
	}
87
88
	/**
89
	 * Sets the file header docblock
90
	 *
91
	 * @param Docblock $docblock the docblock
92
	 * @return $this
93
	 */
94 1
	public function setHeaderDocblock(Docblock $docblock) {
95 1
		$this->options['headerDocblock'] = $docblock;
96 1
		return $this;
97
	}
98
99
	/**
100
	 * Returns whether a blank line should be generated at the end of the file
101
	 *
102
	 * @return bool `true` if it will be generated and `false` if not
103
	 */
104 9
	public function getBlankLineAtEnd(): bool {
105 9
		return $this->options['blankLineAtEnd'];
106
	}
107
108
	/**
109
	 * Sets whether a blank line should be generated at the end of the file
110
	 *
111
	 * @param bool $show `true` if it will be generated and `false` if not
112
	 * @return $this
113
	 */
114 1
	public function setBlankLineAtEnd(bool $show) {
115 1
		$this->options['blankLineAtEnd'] = $show;
116 1
		return $this;
117
	}
118
119
	/**
120
	 * Returns whether a `declare(strict_types=1);` statement should be printed
121
	 * below the header comments (PHP 7)
122
	 *
123
	 * @return bool `true` if it will be printed and `false` if not
124
	 */
125 10
	public function getDeclareStrictTypes(): bool {
126 10
		return $this->options['declareStrictTypes'];
127
	}
128
129
	/**
130
	 * Sets whether a `declare(strict_types=1);` statement should be printed
131
	 * below the header comments (PHP 7)
132
	 *
133
	 * @param bool $strict `true` if it will be printed and `false` if not
134
	 * @return $this
135
	 */
136 1
	public function setDeclareStrictTypes(bool $strict) {
137 1
		$this->options['declareStrictTypes'] = $strict;
138 1
		return $this;
139
	}
140
}
141