Skip to content

Commit 11a579c

Browse files
committed
minor #4580 Add suport for true/false return type hints. (SpacePossum)
This PR was merged into the 2.15 branch. Discussion ---------- Add suport for true/false return type hints. fixes first issues listed here #4511 Commits ------- 20969b5 Add suport for true/false return type hints.
2 parents 1bb9417 + 20969b5 commit 11a579c

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed

src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
final class PhpdocToReturnTypeFixer extends AbstractFixer implements ConfigurationDefinitionFixerInterface
3333
{
3434
/**
35-
* @var array
35+
* @var array<array<int, string>>
3636
*/
3737
private $blacklistFuncNames = [
3838
[T_STRING, '__construct'],
@@ -41,7 +41,7 @@ final class PhpdocToReturnTypeFixer extends AbstractFixer implements Configurati
4141
];
4242

4343
/**
44-
* @var array
44+
* @var array<string, int>
4545
*/
4646
private $versionSpecificTypes = [
4747
'void' => 70100,
@@ -50,17 +50,19 @@ final class PhpdocToReturnTypeFixer extends AbstractFixer implements Configurati
5050
];
5151

5252
/**
53-
* @var array
53+
* @var array<string, string>
5454
*/
5555
private $scalarTypes = [
56-
'bool' => true,
57-
'float' => true,
58-
'int' => true,
59-
'string' => true,
56+
'bool' => 'bool',
57+
'true' => 'bool',
58+
'false' => 'bool',
59+
'float' => 'float',
60+
'int' => 'int',
61+
'string' => 'string',
6062
];
6163

6264
/**
63-
* @var array
65+
* @var array<string, bool>
6466
*/
6567
private $skippedTypes = [
6668
'mixed' => true,
@@ -127,9 +129,9 @@ public function isCandidate(Tokens $tokens)
127129
*/
128130
public function getPriority()
129131
{
130-
// should be run after PhpdocScalarFixer.
132+
// should be run after PhpdocScalarFixer and PhpdocTypes.
131133
// should be run before ReturnTypeDeclarationFixer, FullyQualifiedStrictTypesFixer, NoSuperfluousPhpdocTagsFixer.
132-
return 8;
134+
return 13;
133135
}
134136

135137
/**
@@ -172,15 +174,18 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
172174
if (1 !== \count($returnTypeAnnotation)) {
173175
continue;
174176
}
177+
175178
$returnTypeAnnotation = current($returnTypeAnnotation);
176179
$types = array_values($returnTypeAnnotation->getTypes());
177180
$typesCount = \count($types);
181+
178182
if (1 > $typesCount || 2 < $typesCount) {
179183
continue;
180184
}
181185

182186
$isNullable = false;
183187
$returnType = current($types);
188+
184189
if (2 === $typesCount) {
185190
$null = $types[0];
186191
$returnType = $types[1];
@@ -216,16 +221,20 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
216221
continue;
217222
}
218223

219-
if (isset($this->scalarTypes[$returnType]) && false === $this->configuration['scalar_types']) {
220-
continue;
221-
}
224+
if (isset($this->scalarTypes[$returnType])) {
225+
if (false === $this->configuration['scalar_types']) {
226+
continue;
227+
}
222228

223-
if (1 !== Preg::match($this->classRegex, $returnType, $matches)) {
224-
continue;
225-
}
229+
$returnType = $this->scalarTypes[$returnType];
230+
} else {
231+
if (1 !== Preg::match($this->classRegex, $returnType, $matches)) {
232+
continue;
233+
}
226234

227-
if (isset($matches['array'])) {
228-
$returnType = 'array';
235+
if (isset($matches['array'])) {
236+
$returnType = 'array';
237+
}
229238
}
230239

231240
$startIndex = $tokens->getNextTokenOfKind($index, ['{', ';']);

tests/AutoReview/FixerFactoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ public function provideFixersPriorityCases()
196196
[$fixers['phpdoc_to_return_type'], $fixers['fully_qualified_strict_types']],
197197
[$fixers['phpdoc_to_return_type'], $fixers['no_superfluous_phpdoc_tags']],
198198
[$fixers['phpdoc_to_return_type'], $fixers['return_type_declaration']],
199+
[$fixers['phpdoc_types'], $fixers['phpdoc_to_return_type']],
199200
[$fixers['pow_to_exponentiation'], $fixers['binary_operator_spaces']],
200201
[$fixers['pow_to_exponentiation'], $fixers['method_argument_space']],
201202
[$fixers['pow_to_exponentiation'], $fixers['native_function_casing']],

tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,31 @@ function xyz() {}
119119
'<?php /** @return object */ function my_foo() {}',
120120
70200,
121121
],
122-
'fix scalar types by default' => [
122+
'fix scalar types by default, int' => [
123123
'<?php /** @return int */ function my_foo(): int {}',
124124
'<?php /** @return int */ function my_foo() {}',
125125
],
126-
'fix scalar types when configured' => [
126+
'fix scalar types by default, float' => [
127+
'<?php /** @return float */ function my_foo(): float {}',
128+
'<?php /** @return float */ function my_foo() {}',
129+
],
130+
'fix scalar types by default, string' => [
131+
'<?php /** @return string */ function my_foo(): string {}',
132+
'<?php /** @return string */ function my_foo() {}',
133+
],
134+
'fix scalar types by default, bool' => [
135+
'<?php /** @return bool */ function my_foo(): bool {}',
136+
'<?php /** @return bool */ function my_foo() {}',
137+
],
138+
'fix scalar types by default, false' => [
139+
'<?php /** @return false */ function my_foo(): bool {}',
140+
'<?php /** @return false */ function my_foo() {}',
141+
],
142+
'fix scalar types by default, true' => [
143+
'<?php /** @return true */ function my_foo(): bool {}',
144+
'<?php /** @return true */ function my_foo() {}',
145+
],
146+
'do not fix scalar types when configured as such' => [
127147
'<?php /** @return int */ function my_foo() {}',
128148
null,
129149
null,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Integration of fixers: phpdoc_types,phpdoc_to_return_type.
3+
--RULESET--
4+
{"phpdoc_types": true, "phpdoc_to_return_type": true}
5+
--REQUIREMENTS--
6+
{"php": 70000}
7+
--EXPECT--
8+
<?php
9+
/**
10+
* @return int
11+
*/
12+
function my_foo(): int
13+
{
14+
return 1;
15+
}
16+
17+
--INPUT--
18+
<?php
19+
/**
20+
* @return INT
21+
*/
22+
function my_foo()
23+
{
24+
return 1;
25+
}

0 commit comments

Comments
 (0)