Skip to content
This repository was archived by the owner on Dec 29, 2020. It is now read-only.

Commit 22258b6

Browse files
committed
Fix a bug when a return type already exists.
1 parent ca3cff9 commit 22258b6

File tree

3 files changed

+58
-29
lines changed

3 files changed

+58
-29
lines changed

src/Converter.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function convert(Project $project, File $file): string
5555
$objectType = self::OBJECT_FUNCTION;
5656
$outputTypeHint = true;
5757
$outputDefaultValue = false;
58+
$outputReturn = true;
5859
$namespace = null;
5960
$object = null;
6061
$function = null;
@@ -70,26 +71,19 @@ public function convert(Project $project, File $file): string
7071
}
7172
break;
7273

74+
case ':':
75+
if (null !== $function) {
76+
$outputReturn = false;
77+
}
78+
break;
79+
7380
case ')':
7481
if ($insideFunctionSignature) {
7582
if ($outputDefaultValue) {
7683
$output .= ' = null';
7784
}
7885

7986
$insideFunctionSignature = false;
80-
81-
if (null !== $function) {
82-
$return = $this->getReturn($project, $objectType, $namespace, $object, $function);
83-
if ($return && $return[0] && !$return[1]) {
84-
$output .= sprintf('): %s', $return[0]);
85-
} else {
86-
$output .= ')';
87-
}
88-
89-
$function = null;
90-
91-
continue 2;
92-
}
9387
}
9488
break;
9589

@@ -105,9 +99,27 @@ public function convert(Project $project, File $file): string
10599
}
106100
break;
107101

102+
case ';':
108103
case '{':
109-
++$level;
110-
break;
104+
if (null !== $function && $outputReturn) {
105+
$return = $this->getReturn($project, $objectType, $namespace, $object, $function);
106+
107+
if ($return && $return[0] && !$return[1]) {
108+
$output .= sprintf(': %s', $return[0]);
109+
}
110+
111+
$function = null;
112+
$outputReturn = false;
113+
}
114+
115+
if ('{' === $token) {
116+
++$level;
117+
}
118+
119+
if (';' === $token && $insideNamespace) {
120+
$insideNamespace = false;
121+
}
122+
break;
111123

112124
case '}':
113125
--$level;
@@ -116,12 +128,6 @@ public function convert(Project $project, File $file): string
116128
$objectType = self::OBJECT_FUNCTION;
117129
}
118130
break;
119-
120-
case ';':
121-
if ($insideNamespace) {
122-
$insideNamespace = false;
123-
}
124-
break;
125131
}
126132

127133
$output .= $token;
@@ -183,6 +189,7 @@ public function convert(Project $project, File $file): string
183189
case T_FUNCTION:
184190
$outsideFunctionSignature = true;
185191
$outputDefaultValue = false;
192+
$outputReturn = true;
186193
break;
187194

188195
case T_VARIABLE:

tests/Fixtures/functions1.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ function bar(\DateTime $a = null, array $b, $c, $d, bool $e, callable $f = null)
3333
function baz($a)
3434
{
3535
}
36+
37+
/**
38+
* Must not be converted (already using type hints).
39+
*
40+
* @param int $a
41+
*
42+
* @return string
43+
*/
44+
function bat(int $a): string
45+
{
46+
}

tests/test.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ function foo()
5757
*
5858
* @return float
5959
*/
60-
function bar(\DateTime $a = null, array $b, int $c = null, string $d, bool $e, callable $f = null): float
61-
{
60+
function bar(\DateTime $a = null, array $b, int $c = null, string $d, bool $e, callable $f = null)
61+
: float{
6262
return 0.0;
6363
}
6464
@@ -69,6 +69,17 @@ function baz($a)
6969
{
7070
}
7171
72+
/**
73+
* Must not be converted (already using type hints).
74+
*
75+
* @param int $a
76+
*
77+
* @return string
78+
*/
79+
function bat(int $a): string
80+
{
81+
}
82+
7283
PHP
7384
, $converter->convert($project, $file));
7485
}
@@ -87,8 +98,8 @@ function baz($a)
8798
*
8899
* @return int
89100
*/
90-
function test(string $a = null): int
91-
{
101+
function test(string $a = null)
102+
: int{
92103
}
93104

94105
PHP
@@ -163,8 +174,8 @@ trait BazTrait
163174
*
164175
* @return \DateTime
165176
*/
166-
protected function inTrait(int $a): \DateTime
167-
{
177+
protected function inTrait(int $a)
178+
: \DateTime{
168179
}
169180
}
170181

@@ -195,8 +206,8 @@ public function test(float $a)
195206
/**
196207
* {@inheritdoc}
197208
*/
198-
public function baz(array $a, int $b): float
199-
{
209+
public function baz(array $a, int $b)
210+
: float{
200211
}
201212
}
202213

0 commit comments

Comments
 (0)