-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
148 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\PHPStan\Rule; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Rules\Rule; | ||
use function strlen; | ||
use function substr_compare; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
*/ | ||
class ClassSuffixNamingRule implements Rule | ||
{ | ||
|
||
/** | ||
* @var array<class-string, string> | ||
*/ | ||
private array $superclassToSuffixMapping; | ||
|
||
/** | ||
* @param array<class-string, string> $superclassToSuffixMapping | ||
*/ | ||
public function __construct(array $superclassToSuffixMapping = []) | ||
{ | ||
$this->superclassToSuffixMapping = $superclassToSuffixMapping; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
/** | ||
* @param InClassNode $node | ||
* @return list<string> | ||
*/ | ||
public function processNode( | ||
Node $node, | ||
Scope $scope | ||
): array | ||
{ | ||
$classReflection = $scope->getClassReflection(); | ||
|
||
if ($classReflection === null) { | ||
return []; | ||
} | ||
|
||
if ($classReflection->isAnonymous()) { | ||
return []; | ||
} | ||
|
||
foreach ($this->superclassToSuffixMapping as $superClass => $suffix) { | ||
if (!$classReflection->isSubclassOf($superClass)) { | ||
continue; | ||
} | ||
|
||
$className = $classReflection->getName(); | ||
|
||
if (substr_compare($className, $suffix, -strlen($suffix)) !== 0) { | ||
return ["Class name $className should end with $suffix suffix"]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
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,26 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\PHPStan\Rule; | ||
|
||
use PHPStan\Rules\Rule; | ||
use ShipMonk\PHPStan\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<ClassSuffixNamingRule> | ||
*/ | ||
class ClassSuffixNamingRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
/** @var class-string $superclass */ | ||
$superclass = 'ClassSuffixNamingRule\CheckedParent'; // @phpstan-ignore-line | ||
return new ClassSuffixNamingRule([$superclass => 'Suffix']); | ||
} | ||
|
||
public function testClass(): void | ||
{ | ||
$this->analyseFile(__DIR__ . '/data/ClassSuffixNamingRule/code.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 ClassSuffixNamingRule; | ||
|
||
class CheckedParent {} | ||
class Whatever {} | ||
class BadSuffixClass extends CheckedParent {} // error: Class name ClassSuffixNamingRule\BadSuffixClass should end with Suffix suffix | ||
class GoodNameSuffix extends CheckedParent {} | ||
|
||
new class extends CheckedParent { | ||
|
||
}; |