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