-
Notifications
You must be signed in to change notification settings - Fork 41
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
27 changed files
with
248 additions
and
49 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
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
15 changes: 15 additions & 0 deletions
15
src/Rule/Assertion/Declaration/ShouldBeNamed/ClassnameRule.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,15 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Rule\Assertion\Declaration\ShouldBeNamed; | ||
|
||
use PHPat\Rule\Extractor\Declaration\ClassnameExtractor; | ||
use PHPStan\Node\InClassNode; | ||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @implements Rule<InClassNode> | ||
*/ | ||
final class ClassnameRule extends ShouldBeNamed implements Rule | ||
{ | ||
use ClassnameExtractor; | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Rule/Assertion/Declaration/ShouldBeNamed/ShouldBeNamed.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,45 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Rule\Assertion\Declaration\ShouldBeNamed; | ||
|
||
use PHPat\Configuration; | ||
use PHPat\Rule\Assertion\Declaration\DeclarationAssertion; | ||
use PHPat\Rule\Assertion\Declaration\ValidationTrait; | ||
use PHPat\Statement\Builder\StatementBuilderFactory; | ||
use PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Type\FileTypeMapper; | ||
|
||
abstract class ShouldBeNamed extends DeclarationAssertion | ||
{ | ||
use ValidationTrait; | ||
|
||
public function __construct( | ||
StatementBuilderFactory $statementBuilderFactory, | ||
Configuration $configuration, | ||
ReflectionProvider $reflectionProvider, | ||
FileTypeMapper $fileTypeMapper | ||
) { | ||
parent::__construct( | ||
__CLASS__, | ||
$statementBuilderFactory, | ||
$configuration, | ||
$reflectionProvider, | ||
$fileTypeMapper | ||
); | ||
} | ||
|
||
protected function applyValidation(string $ruleName, ClassReflection $subject, bool $meetsDeclaration, array $tips, array $params = []): array | ||
{ | ||
return $this->applyShould($ruleName, $subject, $meetsDeclaration, $tips, $params); | ||
} | ||
|
||
protected function getMessage(string $ruleName, string $subject, array $params = []): string | ||
{ | ||
$message = $params['isRegex'] === true | ||
? sprintf('%s should be named matching the regex %s', $subject, $params['classname']) | ||
: sprintf('%s should be named %s', $subject, $params['classname']); | ||
|
||
return $this->prepareMessage($ruleName, $message); | ||
} | ||
} |
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
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,36 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPat\Rule\Extractor\Declaration; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassNode; | ||
|
||
trait ClassnameExtractor | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return InClassNode::class; | ||
} | ||
|
||
/** | ||
* @param InClassNode $node | ||
*/ | ||
protected function meetsDeclaration(Node $node, Scope $scope, array $params = []): bool | ||
{ | ||
if (!isset($params['isRegex'], $params['classname'])) { | ||
return false; | ||
} | ||
|
||
$pos = mb_strrpos($node->getClassReflection()->getName(), '\\'); | ||
$classname = $pos === false | ||
? $node->getClassReflection()->getName() | ||
: mb_substr($node->getClassReflection()->getName(), $pos + 1); | ||
|
||
if ($params['isRegex'] === true) { | ||
return preg_match($params['classname'], $classname) === 1; | ||
} | ||
|
||
return $classname === $params['classname']; | ||
} | ||
} |
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
Oops, something went wrong.