Skip to content

Commit b2ee1c6

Browse files
author
codeliner
committed
Fix TypeError if no type given for param or property
1 parent 866f54e commit b2ee1c6

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

src/Code/MethodGenerator.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,13 @@ public function generate(): ClassMethod
185185
$docBlockTypes = [];
186186

187187
foreach ($this->getParameters() as $parameter) {
188-
$type = $parameter->getType()->isNullable()
189-
? $parameter->getType()->type() . '|null'
190-
: $parameter->getType()->type();
188+
if(null === $parameter->getType()) {
189+
$type = 'mixed';
190+
} else {
191+
$type = $parameter->getType()->isNullable()
192+
? $parameter->getType()->type() . '|null'
193+
: $parameter->getType()->type();
194+
}
191195

192196
if ($typeHint = $parameter->getTypeDocBlockHint()) {
193197
$type = $typeHint;

src/Code/ParameterGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function setType($type): self
8888
return $this;
8989
}
9090

91-
public function getType(): TypeGenerator
91+
public function getType(): ?TypeGenerator
9292
{
9393
return $this->type;
9494
}

src/Code/PropertyGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function setType(string $type): self
8282
return $this;
8383
}
8484

85-
public function getType(): TypeGenerator
85+
public function getType(): ?TypeGenerator
8686
{
8787
return $this->type;
8888
}

tests/Code/MethodGeneratorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,33 @@ public function setType(?string $type);
5656
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$method->generate()]));
5757
}
5858

59+
/**
60+
* @test
61+
*/
62+
public function it_generates_method_with_mixed_type_doc_block(): void
63+
{
64+
$method = new MethodGenerator(
65+
'setType',
66+
[
67+
new ParameterGenerator('type'),
68+
]
69+
);
70+
$method->setDocBlockComment('Sets an awesome type');
71+
72+
$expectedOutput = <<<'EOF'
73+
<?php
74+
75+
/**
76+
* Sets an awesome type
77+
*
78+
* @var mixed $type
79+
*/
80+
public function setType($type);
81+
EOF;
82+
83+
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$method->generate()]));
84+
}
85+
5986
/**
6087
* @test
6188
*/

tests/Code/ParameterGeneratorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ public function it_generates_type(string $type): void
6464
<?php
6565
6666
$type \$myParameter
67+
PHP;
68+
69+
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$parameter->generate()]));
70+
}
71+
72+
/**
73+
* @test
74+
*/
75+
public function it_works_without_type()
76+
{
77+
$parameter = new ParameterGenerator('myParameter');
78+
79+
$expectedOutput = <<<PHP
80+
<?php
81+
82+
\$myParameter
6783
PHP;
6884

6985
$this->assertSame($expectedOutput, $this->printer->prettyPrintFile([$parameter->generate()]));

0 commit comments

Comments
 (0)