Skip to content

Commit

Permalink
Enhancement: Implement NoParameterWithNullDefaultValueRule
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Nov 23, 2018
1 parent c887188 commit ac220a0
Show file tree
Hide file tree
Showing 44 changed files with 823 additions and 0 deletions.
1 change: 1 addition & 0 deletions .php_cs.fixture
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Localheinz\PhpCsFixer\Config;

$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php71(''), [
'lowercase_constants' => false,
'static_lambda' => false,
]);

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ For a full diff see [`0.2.0...master`](https://github.com/localheinz/phpstan-rul
`Methods\NoNullableReturnTypeDeclarationRule`, which reports an error
when a method declared on an anonymous class, a class, or an interface has a
nullable return type declaration ([#16](https://github.com/localheinz/phpstan-rules/pull/16)), by [@localheinz](https://github.com/localheinz)
* added `Functions\NoNullDefaultValueRule`, which reports an error when a
named function has a parameter with `null` as default value, and
`Methods\NoParameterWithNullDefaultValueRule`, which reports an error when a
method declared on an anonymous class, a class, or an interface has a
parameter with `null` as default value ([#26](https://github.com/localheinz/phpstan-rules/pull/26)), by [@localheinz](https://github.com/localheinz)

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

Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ This package provides the following rules for use with [`phpstan/phpstan`](https
* [`Localheinz\PHPStan\Rules\Classes\AbstractOrFinalRule`](https://github.com/localheinz/phpstan-rules#classesabstractorfinalrule)
* [`Localheinz\PHPStan\Rules\Classes\FinalRule`](https://github.com/localheinz/phpstan-rules#classesfinalrule)
* [`Localheinz\PHPStan\Rules\Functions\NoNullableReturnTypeDeclarationRule`](https://github.com/localheinz/phpstan-rules#functionsnonullablereturntypedeclarationrule)
* [`Localheinz\PHPStan\Rules\Functions\NoParameterWithNullDefaultValueRule`](https://github.com/localheinz/phpstan-rules#functionsnoparameterwithnulldefaultvaluerule)
* [`Localheinz\PHPStan\Rules\Methods\NoNullableReturnTypeDeclarationRule`](https://github.com/localheinz/phpstan-rules#methodsnonullablereturntypedeclarationrule)
* [`Localheinz\PHPStan\Rules\Methods\NoParameterWithNullDefaultValueRule`](https://github.com/localheinz/phpstan-rules#methodsnoparameterwithnulldefaultvaluerule)

### `Classes\AbstractOrFinalRule`

Expand Down Expand Up @@ -85,6 +87,17 @@ rules:
- Localheinz\PHPStan\Rules\Functions\NoNullableReturnTypeDeclarationRule
```

### `Functions\NoParameterWithNullDefaultValueRule`

This rule reports an error when a named function has a parameter with `null` as default value.

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

```neon
rules:
- Localheinz\PHPStan\Rules\Functions\NoParameterWithNullDefaultValueRule
```

### `Methods\NoNullableReturnTypeDeclarationRule`

This rule reports an error when a method declared on an anonymous class, a class, or an interface uses a nullable return type declaration.
Expand All @@ -96,6 +109,17 @@ rules:
- Localheinz\PHPStan\Rules\Methods\NoNullableReturnTypeDeclarationRule
```

### `Methods\NoParameterWithNullDefaultValueRule`

This rule reports an error when a method declared on an anonymous class, a class, or an interface has a parameter with `null` as default value.

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

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

## Changelog

Please have a look at [`CHANGELOG.md`](CHANGELOG.md).
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ includes:
rules:
- Localheinz\PHPStan\Rules\Classes\AbstractOrFinalRule
- Localheinz\PHPStan\Rules\Functions\NoNullableReturnTypeDeclarationRule
- Localheinz\PHPStan\Rules\Functions\NoParameterWithNullDefaultValueRule
- Localheinz\PHPStan\Rules\Methods\NoNullableReturnTypeDeclarationRule
- Localheinz\PHPStan\Rules\Methods\NoParameterWithNullDefaultValueRule
67 changes: 67 additions & 0 deletions src/Functions/NoParameterWithNullDefaultValueRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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\Functions;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;

final class NoParameterWithNullDefaultValueRule implements Rule
{
public function getNodeType(): string
{
return Node\Stmt\Function_::class;
}

/**
* @param Node\Stmt\Function_ $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 {
if (!$node->default instanceof Node\Expr\ConstFetch) {
return false;
}

return 'null' === $node->default->name->toLowerString();
});

if (0 === \count($params)) {
return [];
}

$functionName = $node->namespacedName;

return \array_map(static function (Node\Param $node) use ($functionName): string {
/** @var Node\Expr\Variable $variable */
$variable = $node->var;

/** @var string $parameterName */
$parameterName = $variable->name;

return \sprintf(
'Parameter "$%s" of function "%s()" should not have null as default value.',
$parameterName,
$functionName
);
}, $params);
}
}
90 changes: 90 additions & 0 deletions src/Methods/NoParameterWithNullDefaultValueRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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 NoParameterWithNullDefaultValueRule 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 {
if (!$node->default instanceof Node\Expr\ConstFetch) {
return false;
}

return 'null' === $node->default->name->toLowerString();
});

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 null as default value.',
$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 null as default value.',
$parameterName,
$className,
$methodName
);
}, $params);
}
}
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\Functions\NoParameterWithNullDefaultValueRule\Failure;

function foo($bar = null)
{
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\Functions\NoParameterWithNullDefaultValueRule\Failure;

function foo($bar = \null)
{
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\Functions\NoParameterWithNullDefaultValueRule\Failure;

function foo($bar = nUlL)
{
return $bar;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Success;

$foo = function ($bar = true) {
return $bar;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Success;

$foo = function ($bar = null) {
return $bar;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Success;

$foo = function ($bar = null) {
return $bar;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Success;

$foo = function ($bar = null) {
return $bar;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Success;

$foo = function ($bar) {
return $bar;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Success;

$foo = function () {
};
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\Functions\NoParameterWithNullDefaultValueRule\Success;

function foo($bar = true)
{
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\Functions\NoParameterWithNullDefaultValueRule\Success;

function foo($bar)
{
return $bar;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Localheinz\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullDefaultValueRule\Success;

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\NoParameterWithNullDefaultValueRule\Failure;

final class MethodInClassWithParameterWithNullDefaultValue
{
public function foo($bar = null)
{
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\NoParameterWithNullDefaultValueRule\Failure;

final class MethodInClassWithParameterWithRootNamespaceReferencedNullDefaultValue
{
public function foo($bar = \null)
{
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\NoParameterWithNullDefaultValueRule\Failure;

final class MethodInClassWithParameterWithWronglyCapitalizedNullDefaultValue
{
public function foo($bar = NuLl)
{
return $bar;
}
}
Loading

0 comments on commit ac220a0

Please sign in to comment.