Skip to content

Commit 6ba155e

Browse files
committed
PHPCS-267 Add a warning against the mixed return type
Signed-off-by: Björn Lange <[email protected]>
1 parent eaebb0f commit 6ba155e

File tree

14 files changed

+137
-244
lines changed

14 files changed

+137
-244
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,6 @@ The base for the BestIt Standard is [PSR-12](https://github.com/php-fig/fig-stan
135135
| BestIt.DocTags.RequiredMethodTags.TagOccurrenceMin | You MUST provide the required tags. The error is registered for every tag specifically. | no |
136136
| BestIt.DocTags.RequiredPropertyTags.TagOccurrenceMax | You MUST provide only the maximum amount of required tags. For example, only one return per method is allowed. The error is registered for every tag specifically. | no |
137137
| BestIt.DocTags.RequiredPropertyTags.TagOccurrenceMin | You MUST provide the required tags. The error is registered for every tag specifically. | no |
138-
| BestIt.DocTags.ReturnTag.MissingReturnDescription | You SHOULD provide a description your return. | no |
139-
| BestIt.DocTags.ReturnTag.MixedType | You SHOULD provide a native return and prevent "mixed". | no |
140-
| BestIt.DocTags.ReturnTag.TagContentFormatInvalid | You MUST provide a special tag format. | no |
141138
| BestIt.DocTags.TagSorting.MissingNewlineBetweenTags | You SHOULD separate tag groups and the final return with a newline. | yes by class |
142139
| BestIt.DocTags.TagSorting.WrongTagSorting | You SHOULD sort the tags by their occurrence and then alphabetically, but @return SHOULD be the last. | yes by class |
143140
| BestIt.DocTags.ThrowsTag.MissingThrowDescription | You SHOULD provide a description your throw tag. | no |
@@ -187,6 +184,7 @@ The base for the BestIt Standard is [PSR-12](https://github.com/php-fig/fig-stan
187184
| BestIt.TypeHints.PropertyTypeHint.MissingAnyTypeHint | MUST have any type hint if possible. | yes |
188185
| BestIt.TypeHints.PropertyTypeHint.MissingNativeTypeHint | MUST have a native type hint (matching the doc block.) | yes |
189186
| BestIt.TypeHints.ReturnTypeDeclaration.MissingReturnTypeHint | Every function or method MUST have a type hint if the return annotation is valid. | yes |
187+
| BestIt.TypeHints.SuggestExplicitReturnType.MixedType | You SHOULD not use a mixed type but an explicit native return type. | yes |
190188
| Generic.Formatting.SpaceAfterCast | There MUST be a space after cast. |
191189
| Generic.Arrays.DisallowLongArraySyntax | Every array syntax MUST be in short array syntax. |
192190
| SlevomatCodingStandard.Classes.ClassConstantVisibility.MissingConstantVisibility | Constants MUST be marked with a visibility. |

src/Standards/BestIt/Sniffs/DocTags/ReturnTagSniff.php

Lines changed: 0 additions & 123 deletions
This file was deleted.

src/Standards/BestIt/Sniffs/TypeHints/ReturnTypeDeclarationSniff.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
use BestIt\CodeSniffer\CodeWarning;
99
use BestIt\Sniffs\AbstractSniff;
1010
use BestIt\Sniffs\DocPosProviderTrait;
11-
use BestIt\Sniffs\FunctionRegistrationTrait;
1211
use BestIt\Sniffs\SuppressingTrait;
13-
use SlevomatCodingStandard\Helpers\Annotation;
1412
use SlevomatCodingStandard\Helpers\Annotation\ReturnAnnotation;
1513
use SlevomatCodingStandard\Helpers\FunctionHelper;
14+
use SlevomatCodingStandard\Helpers\TokenHelper;
1615
use SlevomatCodingStandard\Helpers\TypeHintHelper;
1716
use function array_filter;
1817
use function array_intersect;
@@ -23,6 +22,7 @@
2322
use function strtolower;
2423
use function substr;
2524
use function version_compare;
25+
use const T_FUNCTION;
2626

2727
/**
2828
* Class ReturnTypeDeclarationSniff
@@ -33,7 +33,6 @@
3333
class ReturnTypeDeclarationSniff extends AbstractSniff
3434
{
3535
use DocPosProviderTrait;
36-
use FunctionRegistrationTrait;
3736
use SuppressingTrait;
3837

3938
/**
@@ -114,13 +113,12 @@ private function addReturnType(): void
114113

115114
/**
116115
* Returns true if this sniff may run.
117-
*
118116
* @return bool
119117
*/
120118
protected function areRequirementsMet(): bool
121119
{
122120
return !$this->isSniffSuppressed(static::CODE_MISSING_RETURN_TYPE) && !$this->hasReturnType() &&
123-
!in_array($this->getFunctionName(), $this->methodsWithoutVoid);
121+
$this->token['code'] === T_FUNCTION && !in_array($this->getFunctionName(), $this->methodsWithoutVoid);
124122
}
125123

126124
/**
@@ -309,7 +307,7 @@ private function loadFunctionName(): string
309307
/**
310308
* Loads the return annotation for this method.
311309
*
312-
* @return null\ReturnAnnotation
310+
* @return null|ReturnAnnotation
313311
*/
314312
protected function loadReturnAnnotation(): ?ReturnAnnotation
315313
{
@@ -337,8 +335,13 @@ protected function processToken(): void
337335
}
338336
}
339337

338+
public function register(): array
339+
{
340+
return TokenHelper::$functionTokenCodes;
341+
}
342+
340343
/**
341-
* Sets up the test.
344+
* Sets up the sniff.
342345
*
343346
* @return void
344347
*/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BestIt\Sniffs\TypeHints;
6+
7+
use BestIt\CodeSniffer\CodeWarning;
8+
use BestIt\Sniffs\AbstractSniff;
9+
use BestIt\Sniffs\SuppressingTrait;
10+
use SlevomatCodingStandard\Helpers\FunctionHelper;
11+
use SlevomatCodingStandard\Helpers\TokenHelper;
12+
use SlevomatCodingStandard\Helpers\TypeHint;
13+
14+
class SuggestExplicitReturnTypeSniff extends AbstractSniff
15+
{
16+
use SuppressingTrait;
17+
18+
public const CODE_MIXED_TYPE = 'MixedType';
19+
20+
private const MESSAGE_MIXED_TYPE = 'We suggest that you avoid the "mixed" type and declare the ' .
21+
'required types in detail.';
22+
23+
private function isMixedTypeHint(): bool
24+
{
25+
$typeHint = FunctionHelper::findReturnTypeHint($this->getFile(), $this->getStackPos());
26+
27+
return ($typeHint instanceof TypeHint) && ($typeHint->getTypeHint() === 'mixed');
28+
}
29+
30+
protected function processToken(): void
31+
{
32+
if ($this->isMixedTypeHint() && !$this->isSniffSuppressed(self::CODE_MIXED_TYPE)) {
33+
throw (new CodeWarning(static::CODE_MIXED_TYPE, self::MESSAGE_MIXED_TYPE, $this->stackPos))
34+
->setToken($this->token);
35+
}
36+
}
37+
38+
public function register(): array
39+
{
40+
return TokenHelper::$functionTokenCodes;
41+
}
42+
}

tests/Sniffs/DocTags/Fixtures/ReturnTagSniff/correct/Function.WithDesc(mixed).php

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/Sniffs/DocTags/Fixtures/ReturnTagSniff/correct/Function.WithDesc.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/Sniffs/DocTags/Fixtures/ReturnTagSniff/correct/Function.WithoutDoc.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/Sniffs/DocTags/Fixtures/ReturnTagSniff/correct/MissingReturnDescription.5.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/Sniffs/DocTags/ReturnTagSniffTest.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

tests/Sniffs/TypeHints/Fixtures/ReturnTypeDeclarationSniff/correct/Correct.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ public function __construct()
1010
// Do nothing.
1111
}
1212

13+
/**
14+
* Returns true if there is a matching tag.
15+
*
16+
* @return bool
17+
*/
18+
protected function areRequirementsMet(): bool
19+
{
20+
return false;
21+
}
22+
1323
/**
1424
* @return bool
1525
*/

0 commit comments

Comments
 (0)