Skip to content

Commit

Permalink
Improved void and iterable support
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jan 3, 2017
1 parent 3835b04 commit 5925d79
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
17 changes: 11 additions & 6 deletions SlevomatCodingStandard/Helpers/FunctionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,17 @@ public static function getParametersTypeHints(\PHP_CodeSniffer_File $codeSniffer
return $parametersTypeHints;
}

public static function returnsValue(\PHP_CodeSniffer_File $codeSnifferFile, int $functionPointer, bool $requireVoid = false): bool
public static function returnsValue(\PHP_CodeSniffer_File $codeSnifferFile, int $functionPointer): bool
{
return self::innerReturnsValue($codeSnifferFile, $functionPointer, false);
}

public static function returnsVoid(\PHP_CodeSniffer_File $codeSnifferFile, int $functionPointer): bool
{
return self::innerReturnsValue($codeSnifferFile, $functionPointer, true);
}

private static function innerReturnsValue(\PHP_CodeSniffer_File $codeSnifferFile, int $functionPointer, bool $requireVoid): bool
{
$tokens = $codeSnifferFile->getTokens();

Expand All @@ -127,11 +137,6 @@ public static function returnsValue(\PHP_CodeSniffer_File $codeSnifferFile, int
return false;
}

public static function returnsVoid(\PHP_CodeSniffer_File $codeSnifferFile, int $functionPointer): bool
{
return self::returnsValue($codeSnifferFile, $functionPointer, true);
}

/**
* @param \PHP_CodeSniffer_File $codeSnifferFile
* @param int $functionPointer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private function checkReturnTypeHints(\PHP_CodeSniffer_File $phpcsFile, int $fun
return;
}

if ($this->definitionContainsVoidTypeHint($returnTypeHintDefinition) && $returnsVoid) {
if ($returnTypeHintDefinition === 'void' && $returnsVoid) {
$phpcsFile->addError(
sprintf(
'%s %s() does not have return type hint for its return value but it should be possible to add it based on @return annotation "%s".',
Expand Down Expand Up @@ -324,11 +324,6 @@ private function definitionContainsNullTypeHint(string $typeHintDefinition): boo
return preg_match('~(?:^null$)|(?:^null\|)|(?:\|null\|)|(?:\|null$)~i', $typeHintDefinition) !== 0;
}

private function definitionContainsVoidTypeHint(string $typeHintDefinition): bool
{
return preg_match('~(?:^void)|(?:^void\|)|(?:\|void\|)|(?:\|void)~i', $typeHintDefinition) !== 0;
}

private function definitionContainsStaticOrThisTypeHint(string $typeHintDefinition): bool
{
return preg_match('~(?:^static$)|(?:^static\|)|(?:\|static\|)|(?:\|static$)~i', $typeHintDefinition) !== 0
Expand Down
10 changes: 4 additions & 6 deletions tests/Sniffs/Typehints/TypeHintDeclarationSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,20 @@ public function testErrors()
$this->assertSniffError($report, 104, TypeHintDeclarationSniff::CODE_MISSING_PROPERTY_TYPE_HINT);
}

/**
* @requires PHP 7.1
*/
public function test71Typehints()
public function testVoidAndIterable()
{
$report = $this->checkFile(__DIR__ . '/data/typehints71.php', [
$report = $this->checkFile(__DIR__ . '/data/voidAndIterableTypeHintDeclaration.php', [
'enableNullableTypeHints' => false,
'traversableTypeHints' => [
\Traversable::class,
],
]);

$this->assertSame(2, $report->getErrorCount());
$this->assertSame(3, $report->getErrorCount());

$this->assertSniffError($report, 11, TypeHintDeclarationSniff::CODE_MISSING_PARAMETER_TYPE_HINT);
$this->assertSniffError($report, 11, TypeHintDeclarationSniff::CODE_MISSING_RETURN_TYPE_HINT);
$this->assertSniffError($report, 36, TypeHintDeclarationSniff::CODE_USELESS_DOC_COMMENT);
}

public function testEnabledNullableTypeHintsNoErrors()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php // lint >= 7.1

class Typehints71FooClass
class VoidAndIterableClass
{

/**
Expand Down Expand Up @@ -30,4 +30,12 @@ public function returnsIterable(): iterable
return [];
}

/**
* @return void
*/
public function returnVoid(): void
{
return;
}

}

0 comments on commit 5925d79

Please sign in to comment.