Skip to content

Commit

Permalink
Update build-cs
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 4, 2024
1 parent 953195d commit 3faa605
Show file tree
Hide file tree
Showing 23 changed files with 153 additions and 228 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
with:
repository: "phpstan/build-cs"
path: "build-cs"
ref: "1.x"
ref: "2.x"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ lint:
.PHONY: cs-install
cs-install:
git clone https://github.com/phpstan/build-cs.git || true
git -C build-cs fetch origin && git -C build-cs reset --hard origin/1.x
git -C build-cs fetch origin && git -C build-cs reset --hard origin/2.x
composer install --working-dir build-cs

.PHONY: cs
Expand Down
3 changes: 1 addition & 2 deletions src/PhpDoc/PHPUnit/MockObjectTypeNodeResolverExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
class MockObjectTypeNodeResolverExtension implements TypeNodeResolverExtension, TypeNodeResolverAwareExtension
{

/** @var TypeNodeResolver */
private $typeNodeResolver;
private TypeNodeResolver $typeNodeResolver;

public function setTypeNodeResolver(TypeNodeResolver $typeNodeResolver): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/PHPUnit/AnnotationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function processDocComment(Doc $docComment): array
}

$errors[] = RuleErrorBuilder::message(
'Annotation "' . $matches['annotation'] . '" is invalid, "@' . $matches['property'] . '" should be followed by a space and a value.'
'Annotation "' . $matches['annotation'] . '" is invalid, "@' . $matches['property'] . '" should be followed by a space and a value.',
)->identifier('phpunit.invalidPhpDoc')->build();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/PHPUnit/AssertRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function isMethodOrStaticCallOnAssert(Node $node, Scope $scope): b
'static',
'parent',
],
true
true,
)
) {
$calledOnType = new ObjectType($scope->getClassReflection()->getName());
Expand Down
12 changes: 5 additions & 7 deletions src/Rules/PHPUnit/ClassCoversExistsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@ class ClassCoversExistsRule implements Rule
/**
* Covers helper.
*
* @var CoversHelper
*/
private $coversHelper;
private CoversHelper $coversHelper;

/**
* Reflection provider.
*
* @var ReflectionProvider
*/
private $reflectionProvider;
private ReflectionProvider $reflectionProvider;

public function __construct(
CoversHelper $coversHelper,
Expand Down Expand Up @@ -62,7 +60,7 @@ public function processNode(Node $node, Scope $scope): array
if (count($classCoversDefaultClasses) >= 2) {
return [
RuleErrorBuilder::message(sprintf(
'@coversDefaultClass is defined multiple times.'
'@coversDefaultClass is defined multiple times.',
))->identifier('phpunit.coversDuplicate')->build(),
];
}
Expand All @@ -75,15 +73,15 @@ public function processNode(Node $node, Scope $scope): array
if (!$this->reflectionProvider->hasClass($className)) {
$errors[] = RuleErrorBuilder::message(sprintf(
'@coversDefaultClass references an invalid class %s.',
$className
$className,
))->identifier('phpunit.coversClass')->build();
}
}

foreach ($classCovers as $covers) {
$errors = array_merge(
$errors,
$this->coversHelper->processCovers($node, $covers, null)
$this->coversHelper->processCovers($node, $covers, null),
);
}

Expand Down
18 changes: 7 additions & 11 deletions src/Rules/PHPUnit/ClassMethodCoversExistsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ class ClassMethodCoversExistsRule implements Rule
/**
* Covers helper.
*
* @var CoversHelper
*/
private $coversHelper;
private CoversHelper $coversHelper;

/**
* The file type mapper.
*
* @var FileTypeMapper
*/
private $fileTypeMapper;
private FileTypeMapper $fileTypeMapper;

public function __construct(
CoversHelper $coversHelper,
Expand Down Expand Up @@ -65,9 +63,7 @@ public function processNode(Node $node, Scope $scope): array
$classPhpDoc = $classReflection->getResolvedPhpDoc();
[$classCovers, $classCoversDefaultClasses] = $this->coversHelper->getCoverAnnotations($classPhpDoc);

$classCoversStrings = array_map(static function (PhpDocTagNode $covers): string {
return (string) $covers->value;
}, $classCovers);
$classCoversStrings = array_map(static fn (PhpDocTagNode $covers): string => (string) $covers->value, $classCovers);

$docComment = $node->getDocComment();
if ($docComment === null) {
Expand All @@ -83,7 +79,7 @@ public function processNode(Node $node, Scope $scope): array
$classReflection->getName(),
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
$node->name->toString(),
$docComment->getText()
$docComment->getText(),
);

[$methodCovers, $methodCoversDefaultClasses] = $this->coversHelper->getCoverAnnotations($methodPhpDoc);
Expand All @@ -93,21 +89,21 @@ public function processNode(Node $node, Scope $scope): array
if (count($methodCoversDefaultClasses) > 0) {
$errors[] = RuleErrorBuilder::message(sprintf(
'@coversDefaultClass defined on class method %s.',
$node->name
$node->name,
))->identifier('phpunit.covers')->build();
}

foreach ($methodCovers as $covers) {
if (in_array((string) $covers->value, $classCoversStrings, true)) {
$errors[] = RuleErrorBuilder::message(sprintf(
'Class already @covers %s so the method @covers is redundant.',
$covers->value
$covers->value,
))->identifier('phpunit.coversDuplicate')->build();
}

$errors = array_merge(
$errors,
$this->coversHelper->processCovers($node, $covers, $coversDefaultClass)
$this->coversHelper->processCovers($node, $covers, $coversDefaultClass),
);
}

Expand Down
13 changes: 6 additions & 7 deletions src/Rules/PHPUnit/CoversHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ class CoversHelper
/**
* Reflection provider.
*
* @var ReflectionProvider
*/
private $reflectionProvider;
private ReflectionProvider $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
{
Expand All @@ -48,12 +47,12 @@ public function getCoverAnnotations(?ResolvedPhpDocBlock $phpDoc): array
foreach ($phpDocNodes as $docNode) {
$covers = array_merge(
$covers,
$docNode->getTagsByName('@covers')
$docNode->getTagsByName('@covers'),
);

$coversDefaultClasses = array_merge(
$coversDefaultClasses,
$docNode->getTagsByName('@coversDefaultClass')
$docNode->getTagsByName('@coversDefaultClass'),
);
}

Expand Down Expand Up @@ -100,14 +99,14 @@ public function processCovers(
if ($class->isInterface()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'@covers value %s references an interface.',
$fullName
$fullName,
))->identifier('phpunit.coversInterface')->build();
}

if (isset($method) && $method !== '' && !$class->hasMethod($method)) {
$errors[] = RuleErrorBuilder::message(sprintf(
'@covers value %s references an invalid method.',
$fullName
$fullName,
))->identifier('phpunit.coversMethod')->build();
}
} elseif (isset($method) && $this->reflectionProvider->hasFunction(new Name($method, []), null)) {
Expand All @@ -118,7 +117,7 @@ public function processCovers(
$error = RuleErrorBuilder::message(sprintf(
'@covers value %s references an invalid %s.',
$fullName,
$isMethod ? 'method' : 'class or function'
$isMethod ? 'method' : 'class or function',
))->identifier(sprintf('phpunit.covers%s', $isMethod ? 'Method' : ''));

if (strpos($className, '\\') === false) {
Expand Down
13 changes: 5 additions & 8 deletions src/Rules/PHPUnit/DataProviderDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,20 @@ class DataProviderDeclarationRule implements Rule
/**
* Data provider helper.
*
* @var DataProviderHelper
*/
private $dataProviderHelper;
private DataProviderHelper $dataProviderHelper;

/**
* When set to true, it reports data provider method with incorrect name case.
*
* @var bool
*/
private $checkFunctionNameCase;
private bool $checkFunctionNameCase;

/**
* When phpstan-deprecation-rules is installed, it reports deprecated usages.
*
* @var bool
*/
private $deprecationRulesInstalled;
private bool $deprecationRulesInstalled;

public function __construct(
DataProviderHelper $dataProviderHelper,
Expand Down Expand Up @@ -70,8 +67,8 @@ public function processNode(Node $node, Scope $scope): array
$dataProviderMethodName,
$lineNumber,
$this->checkFunctionNameCase,
$this->deprecationRulesInstalled
)
$this->deprecationRulesInstalled,
),
);
}

Expand Down
23 changes: 10 additions & 13 deletions src/Rules/PHPUnit/DataProviderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@ class DataProviderHelper
/**
* Reflection provider.
*
* @var ReflectionProvider
*/
private $reflectionProvider;
private ReflectionProvider $reflectionProvider;

/**
* The file type mapper.
*
* @var FileTypeMapper
*/
private $fileTypeMapper;
private FileTypeMapper $fileTypeMapper;

/** @var bool */
private $phpunit10OrNewer;
private bool $phpunit10OrNewer;

public function __construct(
ReflectionProvider $reflectionProvider,
Expand Down Expand Up @@ -69,7 +66,7 @@ public function getDataProviderMethods(
$classReflection->getName(),
$scope->isInTrait() ? $scope->getTraitReflection()->getName() : null,
$node->name->toString(),
$docComment->getText()
$docComment->getText(),
);
foreach ($this->getDataProviderAnnotations($methodPhpDoc) as $annotation) {
$dataProviderValue = $this->getDataProviderAnnotationValue($annotation);
Expand Down Expand Up @@ -122,7 +119,7 @@ private function getDataProviderAnnotations(?ResolvedPhpDocBlock $phpDoc): array
foreach ($phpDocNodes as $docNode) {
$annotations = array_merge(
$annotations,
$docNode->getTagsByName('@dataProvider')
$docNode->getTagsByName('@dataProvider'),
);
}

Expand All @@ -145,7 +142,7 @@ public function processDataProvider(
return [
RuleErrorBuilder::message(sprintf(
'@dataProvider %s related class not found.',
$dataProviderValue
$dataProviderValue,
))
->line($lineNumber)
->identifier('phpunit.dataProviderClass')
Expand All @@ -159,7 +156,7 @@ public function processDataProvider(
return [
RuleErrorBuilder::message(sprintf(
'@dataProvider %s related method not found.',
$dataProviderValue
$dataProviderValue,
))
->line($lineNumber)
->identifier('phpunit.dataProviderMethod')
Expand All @@ -173,7 +170,7 @@ public function processDataProvider(
$errors[] = RuleErrorBuilder::message(sprintf(
'@dataProvider %s related method is used with incorrect case: %s.',
$dataProviderValue,
$dataProviderMethodReflection->getName()
$dataProviderMethodReflection->getName(),
))
->line($lineNumber)
->identifier('method.nameCase')
Expand All @@ -183,7 +180,7 @@ public function processDataProvider(
if (!$dataProviderMethodReflection->isPublic()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'@dataProvider %s related method must be public.',
$dataProviderValue
$dataProviderValue,
))
->line($lineNumber)
->identifier('phpunit.dataProviderPublic')
Expand All @@ -193,7 +190,7 @@ public function processDataProvider(
if ($deprecationRulesInstalled && $this->phpunit10OrNewer && !$dataProviderMethodReflection->isStatic()) {
$errors[] = RuleErrorBuilder::message(sprintf(
'@dataProvider %s related method must be static in PHPUnit 10 and newer.',
$dataProviderValue
$dataProviderValue,
))
->line($lineNumber)
->identifier('phpunit.dataProviderStatic')
Expand Down
6 changes: 2 additions & 4 deletions src/Rules/PHPUnit/DataProviderHelperFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
class DataProviderHelperFactory
{

/** @var ReflectionProvider */
private $reflectionProvider;
private ReflectionProvider $reflectionProvider;

/** @var FileTypeMapper */
private $fileTypeMapper;
private FileTypeMapper $fileTypeMapper;

public function __construct(ReflectionProvider $reflectionProvider, FileTypeMapper $fileTypeMapper)
{
Expand Down
8 changes: 3 additions & 5 deletions src/Rules/PHPUnit/MockMethodCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,15 @@ public function processNode(Node $node, Scope $scope): array
)
&& !$type->hasMethod($method)->yes()
) {
$mockClasses = array_filter($type->getObjectClassNames(), static function (string $class): bool {
return $class !== MockObject::class && $class !== Stub::class;
});
$mockClasses = array_filter($type->getObjectClassNames(), static fn (string $class): bool => $class !== MockObject::class && $class !== Stub::class);
if (count($mockClasses) === 0) {
continue;
}

$errors[] = RuleErrorBuilder::message(sprintf(
'Trying to mock an undefined method %s() on class %s.',
$method,
implode('&', $mockClasses)
implode('&', $mockClasses),
))->identifier('phpunit.mockMethod')->build();
continue;
}
Expand All @@ -82,7 +80,7 @@ public function processNode(Node $node, Scope $scope): array
$errors[] = RuleErrorBuilder::message(sprintf(
'Trying to mock an undefined method %s() on class %s.',
$method,
implode('|', $classNames)
implode('|', $classNames),
))->identifier('phpunit.mockMethod')->build();
}

Expand Down
3 changes: 1 addition & 2 deletions src/Rules/PHPUnit/NoMissingSpaceInClassAnnotationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class NoMissingSpaceInClassAnnotationRule implements Rule
/**
* Covers helper.
*
* @var AnnotationHelper
*/
private $annotationHelper;
private AnnotationHelper $annotationHelper;

public function __construct(AnnotationHelper $annotationHelper)
{
Expand Down
3 changes: 1 addition & 2 deletions src/Rules/PHPUnit/NoMissingSpaceInMethodAnnotationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class NoMissingSpaceInMethodAnnotationRule implements Rule
/**
* Covers helper.
*
* @var AnnotationHelper
*/
private $annotationHelper;
private AnnotationHelper $annotationHelper;

public function __construct(AnnotationHelper $annotationHelper)
{
Expand Down
Loading

0 comments on commit 3faa605

Please sign in to comment.