Skip to content

Commit 5f56d53

Browse files
author
Robin Geuze
committed
Let DefaultVisitor take CodeGeneratorConfig as an argument to prevent having it swallow lots of arguments and make scalar and return type hints on by default when declare strict is on
1 parent bbefc69 commit 5f56d53

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

src/config/CodeFileGeneratorConfig.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
namespace gossi\codegen\config;
33

44
use gossi\docblock\Docblock;
5+
use Symfony\Component\OptionsResolver\Options;
6+
57
class CodeFileGeneratorConfig extends CodeGeneratorConfig {
68

79
protected function getOptionalOptions() {
@@ -11,12 +13,24 @@ protected function getOptionalOptions() {
1113
}
1214

1315
protected function getDefaultOptions() {
14-
return array_merge([
16+
return array_merge(
17+
parent::getDefaultOptions(),
18+
[
1519
'headerComment' => '',
1620
'headerDocblock' => null,
1721
'blankLineAtEnd' => true,
1822
'declareStrictTypes' => false,
19-
], parent::getDefaultOptions());
23+
'generateScalarTypeHints' => function(Options $options) {
24+
if ($options['declareStrictTypes'])
25+
return true;
26+
return false;
27+
},
28+
'generateReturnTypeHints' => function(Options $options) {
29+
if ($options['declareStrictTypes'])
30+
return true;
31+
return false;
32+
},
33+
]);
2034
}
2135

2236
protected function getAllowedOptionTypes() {

src/generator/CodeGenerator.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,7 @@ public function __construct($config = null) {
3232
}
3333

3434
protected function init() {
35-
if ($this->config->getGenerateEmptyDocblock()) {
36-
$visitor = new EmptyDocblockVisitor(
37-
$this->config->getGenerateScalarTypeHints(),
38-
$this->config->getGenerateReturnTypeHints()
39-
);
40-
} else {
41-
$visitor = new DefaultVisitor(
42-
$this->config->getGenerateScalarTypeHints(),
43-
$this->config->getGenerateReturnTypeHints()
44-
);
45-
}
35+
$visitor = new DefaultVisitor($this->config);
4636
$this->strategy = new DefaultGeneratorStrategy($visitor);
4737
}
4838

src/visitor/DefaultVisitor.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
namespace gossi\codegen\visitor;
1818

19+
use gossi\codegen\config\CodeGeneratorConfig;
1920
use gossi\codegen\model\NamespaceInterface;
2021
use gossi\codegen\model\AbstractPhpStruct;
2122
use gossi\codegen\model\DocblockInterface;
@@ -42,6 +43,8 @@ class DefaultVisitor implements GeneratorVisitorInterface {
4243
protected $scalarTypeHints;
4344
protected $returnTypeHints;
4445

46+
protected $config;
47+
4548
protected static $noTypeHints = [
4649
'string',
4750
'int',
@@ -55,9 +58,9 @@ class DefaultVisitor implements GeneratorVisitorInterface {
5558
'resource'
5659
];
5760

58-
public function __construct($scalarTypeHints = false, $returnTypeHints = false) {
59-
$this->scalarTypeHints = $scalarTypeHints;
60-
$this->returnTypeHints = $returnTypeHints;
61+
public function __construct(CodeGeneratorConfig $config = null) {
62+
// Make sure we retain the old default behavior for this class
63+
$this->config = $config?: new CodeGeneratorConfig(['generateEmptyDocblock' => false]);
6164
$this->writer = new Writer();
6265
}
6366

@@ -114,7 +117,7 @@ protected function visitUseStatements(AbstractPhpStruct $struct) {
114117
}
115118

116119
protected function visitDocblock(Docblock $docblock) {
117-
if (!$docblock->isEmpty()) {
120+
if (!$docblock->isEmpty() || $this->config->getGenerateEmptyDocblock()) {
118121
$this->writeDocblock($docblock);
119122
}
120123
}
@@ -323,7 +326,9 @@ protected function writeParameters(array $parameters) {
323326
}
324327
$first = false;
325328

326-
if (false === strpos($parameter->getType(), '|') && ($type = $parameter->getType()) && (!in_array($type, self::$noTypeHints) || $this->scalarTypeHints)) {
329+
if (false === strpos($parameter->getType(), '|') &&
330+
($type = $parameter->getType()) &&
331+
(!in_array($type, self::$noTypeHints) || $this->config->getGenerateScalarTypeHints())) {
327332
$this->writer->write($type . ' ');
328333
}
329334

@@ -353,7 +358,7 @@ protected function writeParameters(array $parameters) {
353358
}
354359

355360
protected function writeFunctionReturnType($type) {
356-
if ($this->returnTypeHints && false === strpos($type, '|')) {
361+
if ($this->config->getGenerateReturnTypeHints() && $type != NULL && false === strpos($type, '|')) {
357362
$this->writer->write(': ')->write($type);
358363
}
359364
}

tests/generator/CodeGeneratorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ public function testMethodReturnTypeHinting() {
147147

148148
public function testStrictTypesDeclaration() {
149149
$expected = "<?php\ndeclare(strict_types=1);\n\nfunction fn(\$a) {\n}\n";
150-
$fn = PhpFunction::create('fn')->addParameter(PhpParameter::create('a')->setType('int'));
151-
$fn->setType('int');
150+
$fn = PhpFunction::create('fn')->addParameter(PhpParameter::create('a'));
152151

153152
$codegen = new CodeFileGenerator(['generateDocblock' => false, 'generateEmptyDocblock' => false, 'declareStrictTypes' => true]);
154153
$code = $codegen->generate($fn);

0 commit comments

Comments
 (0)