-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement: Implement NoNullDefaultValueRule
- Loading branch information
1 parent
c887188
commit 035cdc8
Showing
36 changed files
with
571 additions
and
1 deletion.
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
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
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 | ||
), | ||
]; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
.../Params/NoNullDefaultValueRule/Failure/MethodInClassWithParameterWithNullDefaultValue.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
final class MethodInClassWithParameterWithNullDefaultValue | ||
{ | ||
public function foo($bar = null) | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...lueRule/Failure/MethodInClassWithParameterWithRootNamespaceReferencedNullDefaultValue.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
final class MethodInClassWithParameterWithRootNamespaceReferencedNullDefaultValue | ||
{ | ||
public function foo($bar = \null) | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ultValueRule/Failure/MethodInClassWithParameterWithWronglyCapitalizedNullDefaultValue.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
final class MethodInClassWithParameterWithWronglyCapitalizedNullDefaultValue | ||
{ | ||
public function foo($bar = NuLl) | ||
{ | ||
return $bar; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ams/NoNullDefaultValueRule/Failure/MethodInInterfaceWithParameterWithNullDefaultValue.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
interface MethodInInterfaceWithParameterWithNullDefaultValue | ||
{ | ||
public function foo($bar = null); | ||
} |
10 changes: 10 additions & 0 deletions
10
...ule/Failure/MethodInInterfaceWithParameterWithRootNamespaceReferencedNullDefaultValue.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
interface MethodInInterfaceWithParameterWithRootNamespaceReferencedNullDefaultValue | ||
{ | ||
public function foo($bar = \null); | ||
} |
10 changes: 10 additions & 0 deletions
10
...alueRule/Failure/MethodInInterfaceWithParameterWithWronlgyCapitalizedNullDefaultValue.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
interface MethodInInterfaceWithParameterWithWronlgyCapitalizedNullDefaultValue | ||
{ | ||
public function foo($bar = NuLl); | ||
} |
12 changes: 12 additions & 0 deletions
12
...ultValueRule/Failure/method-in-anonymous-class-with-parameter-with-null-default-value.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
new class() { | ||
public function foo($bar = null) | ||
{ | ||
return $bar; | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
...d-in-anonymous-class-with-parameter-with-root-namespace-referenced-null-default-value.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
new class() { | ||
public function foo($bar = \null) | ||
{ | ||
return $bar; | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
.../method-in-anonymous-class-with-parameter-with-wrongly-capitalized-null-default-value.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
new class() { | ||
public function foo($bar = NuLl) | ||
{ | ||
return $bar; | ||
} | ||
}; |
10 changes: 10 additions & 0 deletions
10
...arams/NoNullDefaultValueRule/Failure/named-function-parameter-with-null-default-value.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
function foo($bar = null) | ||
{ | ||
return $bar; | ||
} |
10 changes: 10 additions & 0 deletions
10
...le/Failure/named-function-parameter-with-root-namespace-referenced-null-default-value.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
function foo($bar = \null) | ||
{ | ||
return $bar; | ||
} |
10 changes: 10 additions & 0 deletions
10
...alueRule/Failure/named-function-parameter-with-wrongly-capitalized-null-default-value.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\Params\NoNullDefaultValueRule\Failure; | ||
|
||
function foo($bar = nUlL) | ||
{ | ||
return $bar; | ||
} |
13 changes: 13 additions & 0 deletions
13
...rams/NoNullDefaultValueRule/Success/MethodInClassWithParameterWithNonNullDefaultValue.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\Params\NoNullDefaultValueRule\Success; | ||
|
||
final class MethodInClassWithParameterWithNonNullDefaultValue | ||
{ | ||
public function foo($bar = 'null') | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...e/Params/NoNullDefaultValueRule/Success/MethodInClassWithParameterWithoutDefaultValue.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\Params\NoNullDefaultValueRule\Success; | ||
|
||
final class MethodInClassWithParameterWithoutDefaultValue | ||
{ | ||
public function foo($bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
.../NoNullDefaultValueRule/Success/MethodInInterfaceWithParameterWithNonNullDefaultValue.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\Params\NoNullDefaultValueRule\Success; | ||
|
||
interface MethodInInterfaceWithParameterWithNonNullDefaultValue | ||
{ | ||
public function foo($bar = 'null'); | ||
} |
10 changes: 10 additions & 0 deletions
10
...rams/NoNullDefaultValueRule/Success/MethodInInterfaceWithParameterWithoutDefaultValue.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\Params\NoNullDefaultValueRule\Success; | ||
|
||
interface MethodInInterfaceWithParameterWithoutDefaultValue | ||
{ | ||
public function foo($bar); | ||
} |
13 changes: 13 additions & 0 deletions
13
...ture/Params/NoNullDefaultValueRule/Success/MethodInTraitWithParameterWithDefaultValue.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\Params\NoNullDefaultValueRule\Success; | ||
|
||
trait MethodInTraitWithParameterWithDefaultValue | ||
{ | ||
public function foo($bar = 'null') | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...rams/NoNullDefaultValueRule/Success/MethodInTraitWithParameterWithNonNullDefaultValue.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\Params\NoNullDefaultValueRule\Success; | ||
|
||
trait MethodInTraitWithParameterWithNonNullDefaultValue | ||
{ | ||
public function foo($bar = 'null') | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...lueRule/Success/MethodInTraitWithParameterWithRootNamespaceReferencedNullDefaultValue.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\Params\NoNullDefaultValueRule\Success; | ||
|
||
trait MethodInTraitWithParameterWithRootNamespaceReferencedNullDefaultValue | ||
{ | ||
public function foo($bar = \null) | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...ultValueRule/Success/MethodInTraitWithParameterWithWronlgyCapitalizedNullDefaultValue.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\Params\NoNullDefaultValueRule\Success; | ||
|
||
trait MethodInTraitWithParameterWithWronlgyCapitalizedNullDefaultValue | ||
{ | ||
public function foo($bar = NuLl) | ||
{ | ||
return $bar; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...e/Params/NoNullDefaultValueRule/Success/MethodInTraitWithParameterWithoutDefaultValue.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\Params\NoNullDefaultValueRule\Success; | ||
|
||
trait MethodInTraitWithParameterWithoutDefaultValue | ||
{ | ||
public function foo($bar) | ||
{ | ||
return $bar; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...NullDefaultValueRule/Success/anonymous-function-parameter-with-non-null-default-value.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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Params\NoNullDefaultValueRule\Success; | ||
|
||
$foo = function ($bar = 'null') { | ||
return $bar; | ||
}; |
Oops, something went wrong.