Skip to content

Commit 6aae555

Browse files
author
Robin Geuze
committed
Add support for scalar and return type hinting (as available in PHP7) and add the strict type declaration option to CodeFileGenerator
1 parent 0a303ab commit 6aae555

6 files changed

Lines changed: 244 additions & 95 deletions

File tree

src/config/CodeFileGeneratorConfig.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,37 @@ class CodeFileGeneratorConfig extends CodeGeneratorConfig {
66

77
protected function getOptionalOptions() {
88
return array_merge([
9-
'headerComment', 'headerDocblock', 'blankLineAtEnd'
9+
'headerComment', 'headerDocblock', 'blankLineAtEnd', 'declareStrictTypes'
1010
], parent::getOptionalOptions());
1111
}
12-
12+
1313
protected function getDefaultOptions() {
1414
return array_merge([
1515
'headerComment' => '',
1616
'headerDocblock' => null,
17-
'blankLineAtEnd' => true
17+
'blankLineAtEnd' => true,
18+
'declareStrictTypes' => false,
1819
], parent::getDefaultOptions());
1920
}
20-
21+
2122
protected function getAllowedOptionTypes() {
2223
return array_merge([
2324
'headerComment' => 'string',
2425
'headerDocblock' => ['null', 'gossi\\docblock\\Docblock'],
25-
'blankLineAtEnd' => 'bool'
26+
'blankLineAtEnd' => 'bool',
27+
'declareStrictTypes' => 'bool',
2628
], parent::getAllowedOptionTypes());
2729
}
28-
30+
2931
/**
3032
* @return string
3133
*/
3234
public function getHeaderComment() {
3335
return $this->options['headerComment'];
3436
}
35-
37+
3638
/**
37-
*
39+
*
3840
* @param string $comment
3941
* @return $this
4042
*/
@@ -49,9 +51,9 @@ public function setHeaderComment($comment) {
4951
public function getHeaderDocblock() {
5052
return $this->options['headerDocblock'];
5153
}
52-
54+
5355
/**
54-
*
56+
*
5557
* @param Docblock $docblock
5658
* @return $this
5759
*/
@@ -66,9 +68,9 @@ public function setHeaderDocblock(Docblock $docblock) {
6668
public function getBlankLineAtEnd() {
6769
return $this->options['blankLineAtEnd'];
6870
}
69-
71+
7072
/**
71-
*
73+
*
7274
* @param boolean $show
7375
* @return $this
7476
*/
@@ -77,4 +79,21 @@ public function setBlankLineAtEnd($show) {
7779
return $this;
7880
}
7981

82+
/**
83+
* @return boolean
84+
*/
85+
public function getDeclareStrictTypes() {
86+
return $this->options['declareStrictTypes'];
87+
}
88+
89+
/**
90+
*
91+
* @param boolean $show
92+
* @return $this
93+
*/
94+
public function setDeclareStrictTypes($strict) {
95+
$this->options['declareStrictTypes'] = $strict;
96+
return $this;
97+
}
98+
8099
}

src/config/CodeGeneratorConfig.php

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,109 @@
44
use Symfony\Component\OptionsResolver\OptionsResolver;
55

66
class CodeGeneratorConfig {
7-
7+
88
protected $options;
9-
9+
1010
public function __construct(array $options = []) {
1111
$resolver = new OptionsResolver();
1212
$resolver->setOptional($this->getOptionalOptions());
1313
$resolver->setAllowedTypes($this->getAllowedOptionTypes());
1414
$resolver->setDefaults($this->getDefaultOptions());
1515
$this->options = $resolver->resolve($options);
1616
}
17-
17+
1818
protected function getOptionalOptions() {
19-
return ['generateDocblock', 'generateEmptyDocblock'];
19+
return [
20+
'generateDocblock',
21+
'generateEmptyDocblock',
22+
'generateScalarTypeHints',
23+
'generateReturnTypeHints',
24+
];
2025
}
21-
26+
2227
protected function getDefaultOptions() {
2328
return [
2429
'generateDocblock' => true,
25-
'generateEmptyDocblock' => true
30+
'generateEmptyDocblock' => true,
31+
'generateScalarTypeHints' => false,
32+
'generateReturnTypeHints' => false,
2633
];
2734
}
28-
35+
2936
protected function getAllowedOptionTypes() {
3037
return [
3138
'generateDocblock' => 'bool',
3239
'generateEmptyDocblock' => 'bool',
40+
'generateScalarTypeHints' => 'bool',
41+
'generateReturnTypeHints' => 'bool',
3342
];
3443
}
35-
44+
3645
/**
3746
* @return boolean
3847
*/
3948
public function getGenerateDocblock() {
4049
return $this->options['generateDocblock'];
4150
}
42-
51+
4352
/**
44-
*
53+
*
4554
* @param boolean $generate
4655
* @return $this
4756
*/
4857
public function setGenerateDocblock($generate) {
4958
$this->options['generateDocblock'] = $generate;
5059
return $this;
5160
}
52-
61+
5362
/**
5463
* @return boolean
5564
*/
5665
public function getGenerateEmptyDocblock() {
5766
return $this->options['generateEmptyDocblock'];
5867
}
59-
68+
6069
/**
61-
*
70+
*
6271
* @param boolean $generate
6372
* @return $this
6473
*/
6574
public function setGenerateEmptyDocblock($generate) {
6675
$this->options['generateEmptyDocblock'] = $generate;
6776
return $this;
6877
}
78+
79+
/**
80+
* @return boolean
81+
*/
82+
public function getGenerateScalarTypeHints() {
83+
return $this->options['generateScalarTypeHints'];
84+
}
85+
86+
/**
87+
*
88+
* @param boolean $generate
89+
* @return $this
90+
*/
91+
public function setGenerateScalarTypeHints($generate) {
92+
$this->options['generateScalarTypeHints'] = $generate;
93+
return $this;
94+
}
95+
96+
/**
97+
* @return boolean
98+
*/
99+
public function getGenerateReturnTypeHints() {
100+
return $this->options['generateReturnTypeHints'];
101+
}
102+
103+
/**
104+
*
105+
* @param boolean $generate
106+
* @return $this
107+
*/
108+
public function setGenerateReturnTypeHints($generate) {
109+
$this->options['generateReturnTypeHints'] = $generate;
110+
return $this;
111+
}
69112
}

src/generator/CodeFileGenerator.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use gossi\docblock\Docblock;
77

88
class CodeFileGenerator extends CodeGenerator {
9-
9+
1010
/**
1111
* @param CodeFileGeneratorConfig|array $config
1212
*/
@@ -17,38 +17,42 @@ public function __construct($config = null) {
1717
$this->config = $config;
1818
} else {
1919
$this->config = new CodeFileGeneratorConfig();
20-
}
21-
20+
}
21+
2222
$this->init();
2323
}
24-
24+
2525
/**
2626
* @return CodeFileGeneratorConfig
2727
*/
2828
public function getConfig() {
2929
return $this->config;
3030
}
31-
31+
3232
public function generate(GenerateableInterface $model) {
3333
$content = "<?php\n";
34-
34+
3535
$comment = $this->config->getHeaderComment();
3636
if (!empty($comment)) {
3737
$docblock = new Docblock();
3838
$docblock->setLongDescription($comment);
3939
$content .= str_replace('/**', '/*', $docblock->toString()) . "\n";
4040
}
41-
41+
4242
if ($this->config->getHeaderDocblock() instanceof Docblock) {
4343
$content .= $this->config->getHeaderDocblock()->toString() . "\n";
4444
}
45-
45+
46+
if ($this->config->getDeclareStrictTypes()) {
47+
$content .= "declare(strict_types=1);\n";
48+
}
49+
4650
$content .= parent::generate($model);
47-
51+
4852
if ($this->config->getBlankLineAtEnd()) {
4953
$content .= "\n";
5054
}
51-
55+
5256
return $content;
5357
}
5458
}

src/generator/CodeGenerator.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
class CodeGenerator {
1010

1111
protected $config;
12-
12+
1313
/**
1414
* @var DefaultGeneratorStrategy
1515
*/
1616
protected $strategy;
17-
17+
1818
/**
19-
*
19+
*
2020
* @param CodeGeneratorConfig|array $config
2121
*/
2222
public function __construct($config = null) {
@@ -26,20 +26,26 @@ public function __construct($config = null) {
2626
$this->config = $config;
2727
} else {
2828
$this->config = new CodeGeneratorConfig();
29-
}
29+
}
3030

3131
$this->init();
3232
}
33-
33+
3434
protected function init() {
3535
if ($this->config->getGenerateEmptyDocblock()) {
36-
$visitor = new EmptyDocblockVisitor();
36+
$visitor = new EmptyDocblockVisitor(
37+
$this->config->getGenerateScalarTypeHints(),
38+
$this->config->getGenerateReturnTypeHints()
39+
);
3740
} else {
38-
$visitor = new DefaultVisitor();
41+
$visitor = new DefaultVisitor(
42+
$this->config->getGenerateScalarTypeHints(),
43+
$this->config->getGenerateReturnTypeHints()
44+
);
3945
}
4046
$this->strategy = new DefaultGeneratorStrategy($visitor);
4147
}
42-
48+
4349
/**
4450
* @return CodeGeneratorConfig
4551
*/
@@ -58,5 +64,5 @@ public function generate(GenerateableInterface $model) {
5864

5965
return $this->strategy->generate($model);
6066
}
61-
67+
6268
}

0 commit comments

Comments
 (0)