-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from localheinz/feature/methods-no-parameter-w…
…ith-nullable-type-declaration Enhancement: Implement Methods\NoParameterWithNullableTypeDeclarationRule
- Loading branch information
Showing
21 changed files
with
386 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/Methods/NoParameterWithNullableTypeDeclarationRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* Copyright (c) 2018 Andreas Möller. | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
* | ||
* @see https://github.com/localheinz/phpstan-rules | ||
*/ | ||
|
||
namespace Localheinz\PHPStan\Rules\Methods; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection; | ||
use PHPStan\Rules\Rule; | ||
|
||
final class NoParameterWithNullableTypeDeclarationRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\ClassMethod::class; | ||
} | ||
|
||
/** | ||
* @param Node\Stmt\ClassMethod $node | ||
* @param Scope $scope | ||
* | ||
* @return array | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (0 === \count($node->params)) { | ||
return []; | ||
} | ||
|
||
$params = \array_filter($node->params, static function (Node\Param $node): bool { | ||
return $node->type instanceof Node\NullableType; | ||
}); | ||
|
||
if (0 === \count($params)) { | ||
return []; | ||
} | ||
|
||
$methodName = $node->name->toString(); | ||
|
||
/** @var Reflection\ClassReflection $classReflection */ | ||
$classReflection = $scope->getClassReflection(); | ||
|
||
if ($classReflection->isAnonymous()) { | ||
return \array_map(static function (Node\Param $node) use ($methodName): string { | ||
/** @var Node\Expr\Variable $variable */ | ||
$variable = $node->var; | ||
|
||
/** @var string $parameterName */ | ||
$parameterName = $variable->name; | ||
|
||
return \sprintf( | ||
'Parameter "$%s" of method "%s()" in anonymous class should not have a nullable type declaration.', | ||
$parameterName, | ||
$methodName | ||
); | ||
}, $params); | ||
} | ||
|
||
$className = $classReflection->getName(); | ||
|
||
return \array_map(static function (Node\Param $node) use ($className, $methodName): string { | ||
/** @var Node\Expr\Variable $variable */ | ||
$variable = $node->var; | ||
|
||
/** @var string $parameterName */ | ||
$parameterName = $variable->name; | ||
|
||
return \sprintf( | ||
'Parameter "$%s" of method "%s::%s()" should not have a nullable type declaration.', | ||
$parameterName, | ||
$className, | ||
$methodName | ||
); | ||
}, $params); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ableTypeDeclarationRule/Failure/MethodInClassWithParameterWithNullableTypeDeclaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Failure; | ||
|
||
final class MethodInClassWithParameterWithNullableTypeDeclaration | ||
{ | ||
public function foo(?string $bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...TypeDeclarationRule/Failure/MethodInInterfaceWithParameterWithNullableTypeDeclaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Failure; | ||
|
||
interface MethodInInterfaceWithParameterWithNullableTypeDeclaration | ||
{ | ||
public function foo(?string $bar); | ||
} |
12 changes: 12 additions & 0 deletions
12
...nRule/Failure/method-in-anonymous-class-with-parameter-with-nullable-type-declaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
new class() { | ||
public function foo(?string $bar) | ||
{ | ||
return $bar; | ||
} | ||
}; |
13 changes: 13 additions & 0 deletions
13
...WithNullableTypeDeclarationRule/Success/MethodInClassWithParameterWithTypeDeclaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
final class MethodInClassWithParameterWithTypeDeclaration | ||
{ | ||
public function foo(string $bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...hNullableTypeDeclarationRule/Success/MethodInClassWithParameterWithoutTypeDeclaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
final class MethodInClassWithParameterWithoutTypeDeclaration | ||
{ | ||
public function foo($bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ods/NoParameterWithNullableTypeDeclarationRule/Success/MethodInClassWithoutParameters.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
final class MethodInClassWithoutParameters | ||
{ | ||
public function foo() | ||
{ | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...NullableTypeDeclarationRule/Success/MethodInInterfaceWithParameterWithTypeDeclaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
interface MethodInInterfaceWithParameterWithTypeDeclaration | ||
{ | ||
public function foo(string $bar); | ||
} |
10 changes: 10 additions & 0 deletions
10
...lableTypeDeclarationRule/Success/MethodInInterfaceWithParameterWithoutTypeDeclaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
interface MethodInInterfaceWithParameterWithoutTypeDeclaration | ||
{ | ||
public function foo($bar); | ||
} |
10 changes: 10 additions & 0 deletions
10
...NoParameterWithNullableTypeDeclarationRule/Success/MethodInInterfaceWithoutParameters.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
interface MethodInInterfaceWithoutParameters | ||
{ | ||
public function foo(); | ||
} |
13 changes: 13 additions & 0 deletions
13
...ableTypeDeclarationRule/Success/MethodInTraitWithParameterWithNullableTypeDeclaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
trait MethodInTraitWithParameterWithNullableTypeDeclaration | ||
{ | ||
public function foo(?string $bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...WithNullableTypeDeclarationRule/Success/MethodInTraitWithParameterWithTypeDeclaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
trait MethodInTraitWithParameterWithTypeDeclaration | ||
{ | ||
public function foo(string $bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...hNullableTypeDeclarationRule/Success/MethodInTraitWithParameterWithoutTypeDeclaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
trait MethodInTraitWithParameterWithoutTypeDeclaration | ||
{ | ||
public function foo($bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ods/NoParameterWithNullableTypeDeclarationRule/Success/MethodInTraitWithoutParameters.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
trait MethodInTraitWithoutParameters | ||
{ | ||
public function foo() | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...eclarationRule/Success/method-in-anonymous-class-with-parameter-with-type-declaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
new class() { | ||
public function foo(string $bar) | ||
{ | ||
return $bar; | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
...arationRule/Success/method-in-anonymous-class-with-parameter-without-type-declaration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
new class() { | ||
public function foo($bar) | ||
{ | ||
return $bar; | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
11
...rWithNullableTypeDeclarationRule/Success/method-in-anonymous-class-without-parameters.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoParameterWithNullableTypeDeclarationRule\Success; | ||
|
||
new class() { | ||
public function foo() | ||
{ | ||
} | ||
}; |
Oops, something went wrong.