-
-
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.
Merge pull request #45 from localheinz/feature/no-constructor-paramet…
…er-with-default-value Enhancement: Implement NoConstructorParameterWithDefaultValueRule
- Loading branch information
Showing
21 changed files
with
373 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
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
includes: | ||
- vendor/phpstan/phpstan-strict-rules/rules.neon | ||
- rules.neon | ||
|
||
parameters: | ||
ignoreErrors: | ||
- '#Constructor in Localheinz\\PHPStan\\Rules\\Classes\\AbstractOrFinalRule has parameter \$excludedClassNames with default value.#' | ||
- '#Constructor in Localheinz\\PHPStan\\Rules\\Classes\\FinalRule has parameter \$excludedClassNames with default value.#' |
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/NoConstructorParameterWithDefaultValueRule.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 NoConstructorParameterWithDefaultValueRule 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 ('__construct' !== $node->name->toLowerString()) { | ||
return []; | ||
} | ||
|
||
if (0 === \count($node->params)) { | ||
return []; | ||
} | ||
|
||
$params = \array_filter($node->params, static function (Node\Param $node): bool { | ||
return null !== $node->default; | ||
}); | ||
|
||
if (0 === \count($params)) { | ||
return []; | ||
} | ||
|
||
/** @var Reflection\ClassReflection $classReflection */ | ||
$classReflection = $scope->getClassReflection(); | ||
|
||
if ($classReflection->isAnonymous()) { | ||
return \array_map(static function (Node\Param $node): string { | ||
/** @var Node\Expr\Variable $variable */ | ||
$variable = $node->var; | ||
|
||
/** @var string $parameterName */ | ||
$parameterName = $variable->name; | ||
|
||
return \sprintf( | ||
'Constructor in anonymous class has parameter $%s with default value.', | ||
$parameterName | ||
); | ||
}, $params); | ||
} | ||
|
||
$className = $classReflection->getName(); | ||
|
||
return \array_map(static function (Node\Param $node) use ($className): string { | ||
/** @var Node\Expr\Variable $variable */ | ||
$variable = $node->var; | ||
|
||
/** @var string $parameterName */ | ||
$parameterName = $variable->name; | ||
|
||
return \sprintf( | ||
'Constructor in %s has parameter $%s with default value.', | ||
$className, | ||
$parameterName | ||
); | ||
}, $params); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ParameterWithDefaultValueRule/Failure/ConstructorInClassWithParameterWithDefaultValue.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\NoConstructorParameterWithDefaultValueRule\Failure; | ||
|
||
final class ConstructorInClassWithParameterWithDefaultValue | ||
{ | ||
public function __construct($bar = 9000) | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ueRule/Failure/ConstructorWithWrongCapitalizationInClassWithParameterWithDefaultValue.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\NoConstructorParameterWithDefaultValueRule\Failure; | ||
|
||
final class ConstructorWithWrongCapitalizationInClassWithParameterWithDefaultValue | ||
{ | ||
public function __CoNsTrUcT($bar = 9000) | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ultValueRule/Failure/constructor-in-anonymous-class-with-parameter-with-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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
new class() { | ||
public function __construct($bar = 9000) | ||
{ | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
11
...ructor-with-wrong-capitalization-in-anonymous-class-with-parameter-with-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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
new class() { | ||
public function __CoNsTrUcT($bar = 9000) | ||
{ | ||
} | ||
}; |
12 changes: 12 additions & 0 deletions
12
...ameterWithDefaultValueRule/Success/ConstructorInClassWithParameterWithoutDefaultValue.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\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
final class ConstructorInClassWithParameterWithoutDefaultValue | ||
{ | ||
public function __construct($bar) | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...oConstructorParameterWithDefaultValueRule/Success/ConstructorInClassWithoutParameters.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\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
final class ConstructorInClassWithoutParameters | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ParameterWithDefaultValueRule/Success/ConstructorInTraitWithParameterWithDefaultValue.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\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
trait ConstructorInTraitWithParameterWithDefaultValue | ||
{ | ||
public function __construct($bar = 9000) | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ameterWithDefaultValueRule/Success/ConstructorInTraitWithParameterWithoutDefaultValue.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\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
trait ConstructorInTraitWithParameterWithoutDefaultValue | ||
{ | ||
public function __construct($bar) | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...oConstructorParameterWithDefaultValueRule/Success/ConstructorInTraitWithoutParameters.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\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
trait ConstructorInTraitWithoutParameters | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...uctorParameterWithDefaultValueRule/Success/MethodInClassWithParameterWithDefaultValue.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\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
final class MethodInClassWithParameterWithDefaultValue | ||
{ | ||
public function foo($bar = 9000) | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...uctorParameterWithDefaultValueRule/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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
trait MethodInTraitWithParameterWithDefaultValue | ||
{ | ||
public function foo($bar = 9000) | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ValueRule/Success/constructor-in-anonymous-class-with-parameter-without-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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
new class() { | ||
public function __construct($bar) | ||
{ | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
11
...rameterWithDefaultValueRule/Success/constructor-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\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
new class() { | ||
public function __construct() | ||
{ | ||
} | ||
}; |
11 changes: 11 additions & 0 deletions
11
...hDefaultValueRule/Success/method-in-anonymous-class-with-parameter-with-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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Localheinz\PHPStan\Rules\Test\Fixture\Methods\NoConstructorParameterWithDefaultValueRule\Success; | ||
|
||
new class() { | ||
public function foo($bar = null) | ||
{ | ||
} | ||
}; |
Oops, something went wrong.