-
-
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 NoConstructorParameterWithDefaultValueRule
- Loading branch information
1 parent
687bada
commit c93ce4d
Showing
18 changed files
with
334 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 = null) | ||
{ | ||
} | ||
}; |
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 = null) | ||
{ | ||
} | ||
}; |
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() | ||
{ | ||
} | ||
} |
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() | ||
{ | ||
} | ||
}; |
97 changes: 97 additions & 0 deletions
97
test/Integration/Methods/NoConstructorParameterWithDefaultValueRuleTest.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,97 @@ | ||
<?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\Test\Integration\Methods; | ||
|
||
use Localheinz\PHPStan\Rules\Methods\NoConstructorParameterWithDefaultValueRule; | ||
use Localheinz\PHPStan\Rules\Test\Fixture; | ||
use Localheinz\PHPStan\Rules\Test\Integration\AbstractTestCase; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class NoConstructorParameterWithDefaultValueRuleTest extends AbstractTestCase | ||
{ | ||
public function providerAnalysisSucceeds(): \Generator | ||
{ | ||
$paths = [ | ||
'constructor-in-anonymous-class-with-parameter-without-default-value' => __DIR__ . '/../../Fixture/Methods/NoConstructorParameterWithDefaultValueRule/Success/constructor-in-anonymous-class-with-parameter-without-default-value.php', | ||
'constructor-in-anonymous-class-without-parameters' => __DIR__ . '/../../Fixture/Methods/NoConstructorParameterWithDefaultValueRule/Success/constructor-in-anonymous-class-without-parameters.php', | ||
'constructor-in-class-with-parameter-without-default-value' => __DIR__ . '/../../Fixture/Methods/NoConstructorParameterWithDefaultValueRule/Success/ConstructorInClassWithParameterWithoutDefaultValue.php', | ||
'constructor-in-class-without-parameters' => __DIR__ . '/../../Fixture/Methods/NoConstructorParameterWithDefaultValueRule/Success/ConstructorInClassWithoutParameters.php', | ||
// traits are not supported | ||
'constructor-in-trait-with-parameter-with-default-value' => __DIR__ . '/../../Fixture/Methods/NoConstructorParameterWithDefaultValueRule/Success/ConstructorInTraitWithParameterWithDefaultValue.php', | ||
'constructor-in-trait-with-parameter-without-default-value' => __DIR__ . '/../../Fixture/Methods/NoConstructorParameterWithDefaultValueRule/Success/ConstructorInTraitWithParameterWithoutDefaultValue.php', | ||
'constructor-in-trait-without-parameters' => __DIR__ . '/../../Fixture/Methods/NoConstructorParameterWithDefaultValueRule/Success/ConstructorInTraitWithoutParameters.php', | ||
]; | ||
|
||
foreach ($paths as $description => $path) { | ||
yield $description => [ | ||
$path, | ||
]; | ||
} | ||
} | ||
|
||
public function providerAnalysisFails(): \Generator | ||
{ | ||
$paths = [ | ||
'constructor-in-anonymous-class-with-parameter-with-default-value' => [ | ||
__DIR__ . '/../../Fixture/Methods/NoConstructorParameterWithDefaultValueRule/Failure/constructor-in-anonymous-class-with-parameter-with-default-value.php', | ||
[ | ||
'Constructor in anonymous class has parameter $bar with default value.', | ||
8, | ||
], | ||
], | ||
'constructor-with-wrong-capitalization-in-anonymous-class-with-parameter-with-default-value' => [ | ||
__DIR__ . '/../../Fixture/Methods/NoConstructorParameterWithDefaultValueRule/Failure/constructor-with-wrong-capitalization-in-anonymous-class-with-parameter-with-default-value.php', | ||
[ | ||
'Constructor in anonymous class has parameter $bar with default value.', | ||
8, | ||
], | ||
], | ||
'constructor-in-class-with-parameter-with-default-value' => [ | ||
__DIR__ . '/../../Fixture/Methods/NoConstructorParameterWithDefaultValueRule/Failure/ConstructorInClassWithParameterWithDefaultValue.php', | ||
[ | ||
\sprintf( | ||
'Constructor in %s has parameter $bar with default value.', | ||
Fixture\Methods\NoConstructorParameterWithDefaultValueRule\Failure\ConstructorInClassWithParameterWithDefaultValue::class | ||
), | ||
9, | ||
], | ||
], | ||
'constructor-with-wrong-capitalization-in-class-with-parameter-with-default-value' => [ | ||
__DIR__ . '/../../Fixture/Methods/NoConstructorParameterWithDefaultValueRule/Failure/ConstructorWithWrongCapitalizationInClassWithParameterWithDefaultValue.php', | ||
[ | ||
\sprintf( | ||
'Constructor in %s has parameter $bar with default value.', | ||
Fixture\Methods\NoConstructorParameterWithDefaultValueRule\Failure\ConstructorWithWrongCapitalizationInClassWithParameterWithDefaultValue::class | ||
), | ||
9, | ||
], | ||
], | ||
]; | ||
|
||
foreach ($paths as $description => [$path, $error]) { | ||
yield $description => [ | ||
$path, | ||
$error, | ||
]; | ||
} | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new NoConstructorParameterWithDefaultValueRule(); | ||
} | ||
} |