-
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
159 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,28 @@ | ||
<?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 | ||
{ | ||
return new ClassSuffixNamingRule([ // @phpstan-ignore-line ignore non existing class not being class-string | ||
'ClassSuffixNamingRule\CheckedParent' => 'Suffix', | ||
'ClassSuffixNamingRule\CheckedInterface' => 'Suffix2', | ||
'NotExistingClass' => 'Foo', | ||
]); | ||
} | ||
|
||
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,21 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ClassSuffixNamingRule; | ||
|
||
class CheckedParent {} | ||
interface CheckedInterface {} | ||
|
||
interface BadSuffixInterface extends CheckedInterface {} // error: Class name ClassSuffixNamingRule\BadSuffixInterface should end with Suffix2 suffix | ||
interface GoodNameSuffix2 extends CheckedInterface {} | ||
|
||
class Whatever {} | ||
class BadSuffixClass extends CheckedParent {} // error: Class name ClassSuffixNamingRule\BadSuffixClass should end with Suffix suffix | ||
class GoodNameSuffix extends CheckedParent {} | ||
|
||
class InvalidConfigurationAlwaysGeneratesSomeError extends CheckedParent implements CheckedInterface {} // error: Class name ClassSuffixNamingRule\InvalidConfigurationAlwaysGeneratesSomeError should end with Suffix suffix | ||
class InvalidConfigurationAlwaysGeneratesSomeErrorSuffix extends CheckedParent implements CheckedInterface {} // error: Class name ClassSuffixNamingRule\InvalidConfigurationAlwaysGeneratesSomeErrorSuffix should end with Suffix2 suffix | ||
class InvalidConfigurationAlwaysGeneratesSomeErrorSuffix2 extends CheckedParent implements CheckedInterface {} // error: Class name ClassSuffixNamingRule\InvalidConfigurationAlwaysGeneratesSomeErrorSuffix2 should end with Suffix suffix | ||
|
||
new class extends CheckedParent { | ||
|
||
}; |