Skip to content

Commit

Permalink
Enhancement: Implement NoNullDefaultValueRule
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Nov 21, 2018
1 parent c887188 commit 035cdc8
Show file tree
Hide file tree
Showing 36 changed files with 571 additions and 1 deletion.
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
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

For a full diff see [`0.2.0...master`](https://github.com/localheinz/phpstan-rules/compare/0.2.0...master).
For a full diff see [`0.3.0...master`](https://github.com/localheinz/phpstan-rules/compare/0.3.0...master).

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

For a full diff see [`0.2.0...0.3.0`](https://github.com/localheinz/phpstan-rules/compare/0.2.0...0.3.0).

### Added

Expand All @@ -16,6 +20,9 @@ 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 `Params\NoNullDefaultValueRule`, which reports an error when a
named function, or 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This package provides the following rules for use with [`phpstan/phpstan`](https
* [`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\Methods\NoNullableReturnTypeDeclarationRule`](https://github.com/localheinz/phpstan-rules#methodsnonullablereturntypedeclarationrule)
* [`Localheinz\PHPStan\Rules\Params\NoNullDefaultValueRule`](https://github.com/localheinz/phpstan-rules#paramsnonulldefaultvaluerule)

### `Classes\AbstractOrFinalRule`

Expand Down Expand Up @@ -96,6 +97,17 @@ rules:
- Localheinz\PHPStan\Rules\Methods\NoNullableReturnTypeDeclarationRule
```

### `Params\NoNullDefaultValueRule`

This rule reports an error when a named function, or 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\Params\NoNullDefaultValueRule
```

## Changelog

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

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

final class NoNullDefaultValueRule implements Rule
{
public function getNodeType(): string
{
return Node\Param::class;
}

/**
* @param Node\Param $node
* @param Scope $scope
*
* @return array
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$node->var instanceof Node\Expr\Variable
|| !\is_string($node->var->name)
|| !$node->default instanceof Node\Expr\ConstFetch
) {
return [];
}

/** @var Node\Expr\ConstFetch $default */
$default = $node->default;

if ('null' !== \mb_strtolower($default->name->toString())) {
return [];
}

return [
\sprintf(
'Parameter "%s" should not have null as default value.',
$node->var->name
),
];
}
}
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\Params\NoNullDefaultValueRule\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\Params\NoNullDefaultValueRule\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\Params\NoNullDefaultValueRule\Failure;

final class MethodInClassWithParameterWithWronglyCapitalizedNullDefaultValue
{
public 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\Params\NoNullDefaultValueRule\Failure;

interface MethodInInterfaceWithParameterWithNullDefaultValue
{
public function foo($bar = null);
}
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\Params\NoNullDefaultValueRule\Failure;

interface MethodInInterfaceWithParameterWithRootNamespaceReferencedNullDefaultValue
{
public function foo($bar = \null);
}
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\Params\NoNullDefaultValueRule\Failure;

interface MethodInInterfaceWithParameterWithWronlgyCapitalizedNullDefaultValue
{
public function foo($bar = NuLl);
}
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\Params\NoNullDefaultValueRule\Failure;

new class() {
public function foo($bar = null)
{
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\Params\NoNullDefaultValueRule\Failure;

new class() {
public function foo($bar = \null)
{
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\Params\NoNullDefaultValueRule\Failure;

new class() {
public 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\Params\NoNullDefaultValueRule\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\Params\NoNullDefaultValueRule\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\Params\NoNullDefaultValueRule\Failure;

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\Params\NoNullDefaultValueRule\Success;

final class MethodInClassWithParameterWithNonNullDefaultValue
{
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\Params\NoNullDefaultValueRule\Success;

final class MethodInClassWithParameterWithoutDefaultValue
{
public function foo($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\Params\NoNullDefaultValueRule\Success;

interface MethodInInterfaceWithParameterWithNonNullDefaultValue
{
public function foo($bar = 'null');
}
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\Params\NoNullDefaultValueRule\Success;

interface MethodInInterfaceWithParameterWithoutDefaultValue
{
public function foo($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\Params\NoNullDefaultValueRule\Success;

trait MethodInTraitWithParameterWithDefaultValue
{
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\Params\NoNullDefaultValueRule\Success;

trait MethodInTraitWithParameterWithNonNullDefaultValue
{
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\Params\NoNullDefaultValueRule\Success;

trait MethodInTraitWithParameterWithRootNamespaceReferencedNullDefaultValue
{
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\Params\NoNullDefaultValueRule\Success;

trait MethodInTraitWithParameterWithWronlgyCapitalizedNullDefaultValue
{
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\Params\NoNullDefaultValueRule\Success;

trait MethodInTraitWithParameterWithoutDefaultValue
{
public 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\Params\NoNullDefaultValueRule\Success;

$foo = function ($bar = 'null') {
return $bar;
};
Loading

0 comments on commit 035cdc8

Please sign in to comment.