Skip to content

Commit

Permalink
Enhancement: Implement Methods\NoParameterWithNullableTypeDeclaration…
Browse files Browse the repository at this point in the history
…Rule
  • Loading branch information
localheinz committed Nov 25, 2018
1 parent 63c0944 commit 0ee4e8c
Show file tree
Hide file tree
Showing 21 changed files with 386 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ For a full diff see [`0.2.0...master`](https://github.com/localheinz/phpstan-rul
error when a closure has a parameter with a nullable type declaration ([#33](https://github.com/localheinz/phpstan-rules/pull/33)), by [@localheinz](https://github.com/localheinz)
* added `Functions\NoParameterWithNullableTypeDeclarationRule`, which reports an
error when a function has a parameter with a nullable type declaration ([#34](https://github.com/localheinz/phpstan-rules/pull/34)), by [@localheinz](https://github.com/localheinz)
* added `Methods\NoParameterWithNullableTypeDeclarationRule`, which reports an
error when a method declared on an anonymous class, a class, or an interface
has a parameter with a nullable type declaration ([#35](https://github.com/localheinz/phpstan-rules/pull/35)), by [@localheinz](https://github.com/localheinz)

## [`0.2.0`](https://github.com/localheinz/phpstan-rules/releases/tag/0.2.0)

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This package provides the following rules for use with [`phpstan/phpstan`](https
* [`Localheinz\PHPStan\Rules\Functions\NoParameterWithNullDefaultValueRule`](https://github.com/localheinz/phpstan-rules#functionsnoparameterwithnulldefaultvaluerule)
* [`Localheinz\PHPStan\Rules\Functions\NoParameterWithNullableTypeDeclaration`](https://github.com/localheinz/phpstan-rules#functionsnoparameterwithnullabletypedeclarationrule)
* [`Localheinz\PHPStan\Rules\Methods\NoNullableReturnTypeDeclarationRule`](https://github.com/localheinz/phpstan-rules#methodsnonullablereturntypedeclarationrule)
* [`Localheinz\PHPStan\Rules\Methods\NoParameterWithNullableTypeDeclarationRule`](https://github.com/localheinz/phpstan-rules#methodsnoparameterwithnullabletypedeclarationrule)
* [`Localheinz\PHPStan\Rules\Methods\NoParameterWithNullDefaultValueRule`](https://github.com/localheinz/phpstan-rules#methodsnoparameterwithnulldefaultvaluerule)

### `Classes\AbstractOrFinalRule`
Expand Down Expand Up @@ -157,6 +158,17 @@ rules:
- Localheinz\PHPStan\Rules\Methods\NoNullableReturnTypeDeclarationRule
```

### `Methods\NoParameterWithNullableTypeDeclarationRule`

This rule reports an error when a method declared in an anonymous class, a class, or an interface has a parameter with a nullable type declaration.

If you want to use this rule, add it to your `phpstan.neon`:

```neon
rules:
- Localheinz\PHPStan\Rules\Methods\NoParameterWithNullableTypeDeclarationRule
```

### `Methods\NoParameterWithNullDefaultValueRule`

This rule reports an error when a method declared in an anonymous class, a class, or an interface has a parameter with `null` as default value.
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ rules:
- Localheinz\PHPStan\Rules\Functions\NoParameterWithNullableTypeDeclarationRule
- Localheinz\PHPStan\Rules\Methods\NoNullableReturnTypeDeclarationRule
- Localheinz\PHPStan\Rules\Methods\NoParameterWithNullDefaultValueRule
- Localheinz\PHPStan\Rules\Methods\NoParameterWithNullableTypeDeclarationRule
86 changes: 86 additions & 0 deletions src/Methods/NoParameterWithNullableTypeDeclarationRule.php
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);
}
}
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;
}
}
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);
}
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;
}
};
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;
}
}
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;
}
}
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()
{
}
}
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);
}
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);
}
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();
}
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;
}
}
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;
}
}
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;
}
}
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()
{
}
}
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;
}
};
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;
}
};
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()
{
}
};
Loading

0 comments on commit 0ee4e8c

Please sign in to comment.