Skip to content

Commit d98c3e3

Browse files
committed
makes phpstan happy
1 parent 78f2f0e commit d98c3e3

File tree

7 files changed

+36
-10
lines changed

7 files changed

+36
-10
lines changed

dev/phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ includes:
22
parameters:
33
level: max
44
inferPrivatePropertyTypeFromConstructor: true
5+
checkGenericClassInNonGenericObjectType: false
56
paths:
67
- %currentWorkingDirectory%/src/
78
- %currentWorkingDirectory%/test/

docker/Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ export PROJECT_NAME = phpstan-rules
22
export SHELL = bash
33

44
DOCKER_COMPOSE = docker-compose
5-
CLI = $(DOCKER_COMPOSE) exec -T ${PROJECT_NAME}
6-
CLI_COMPOSER = $(DOCKER_COMPOSE) exec -T ${PROJECT_NAME} php -d memory_limit=-1 /usr/bin/composer
5+
CLI = $(DOCKER_COMPOSE) run -T ${PROJECT_NAME}
6+
CLI_COMPOSER = $(DOCKER_COMPOSE) run -T ${PROJECT_NAME} php -d memory_limit=-1 /usr/bin/composer
77

88
start:
99
$(DOCKER_COMPOSE) up -d
@@ -47,4 +47,7 @@ php-cs-fixer: ##@development run php-cs-fixer
4747

4848
tests:
4949
$(CLI) ./vendor/bin/phpunit --configuration=test/Integration/phpunit.xml
50-
.PHONY: tests
50+
.PHONY: tests
51+
52+
check: php-cs-fixer phpstan tests
53+
.PHONY: check

src/Helper/Converter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ class Converter
1616
public static function propertyStringNames(array $properties)
1717
{
1818
return array_map(static function (Node\Stmt\Property $property): string {
19-
return (string)reset($property->props)->name;
19+
$firstProp = reset($property->props);
20+
if ($firstProp === false) {
21+
return '';
22+
}
23+
24+
return (string)$firstProp->name;
2025
}, $properties);
2126
}
2227
}

src/Helper/NodeParser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public static function getClassProperties(Node\Stmt\Class_ $classNode): array
5959
return $properties;
6060
}
6161

62+
/**
63+
* @param Node\Stmt\Class_ $classNode
64+
*
65+
* @return array<Node\Stmt\Property>
66+
*/
6267
public static function getNonPrivateProperties(Node\Stmt\Class_ $classNode): array
6368
{
6469
$properties = self::getClassProperties($classNode);

src/Rules/ImmutableObjectRule.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function processNode(Node $node, Scope $scope): array
4848
return [];
4949
}
5050

51-
[$immutableProperties, $hasImmutableParent] = $this->getInheritedImmutableProperties($scope);
51+
['properties' => $immutableProperties, 'hasImmutableParent' => $hasImmutableParent] = $this->getInheritedImmutableProperties($scope);
5252

5353
$nodes = $this->parser->parseFile($scope->getFile());
5454
$hasImmutableClassAnnotation = AnnotationParser::classHasAnnotation(self::WHITELISTED_ANNOTATIONS, $nodes);
@@ -124,12 +124,12 @@ public function processNode(Node $node, Scope $scope): array
124124
/**
125125
* @param Scope $scope
126126
*
127-
* @return array<string[]>
127+
* @return array{properties: string[], hasImmutableParent: bool}
128128
*/
129129
private function getInheritedImmutableProperties(Scope $scope): array
130130
{
131131
if ($scope->getClassReflection() === null) {
132-
return [];
132+
return ['properties' => [], 'hasImmutableParent' => false];
133133
}
134134

135135
$immutableParentProperties = [];
@@ -158,6 +158,6 @@ private function getInheritedImmutableProperties(Scope $scope): array
158158
// @TODO: detect non private parent properties annotated as immutable (instead of whole class)
159159
}
160160

161-
return [$immutableParentProperties, $hasImmutableParent];
161+
return ['properties' => $immutableParentProperties, 'hasImmutableParent' => $hasImmutableParent];
162162
}
163163
}

test/Integration/AbstractTestCase.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ final public function testAnalysisSucceeds(string $path): void
2626
/**
2727
* @dataProvider provideCasesWhereAnalysisShouldFail
2828
*
29-
* @param string $path
30-
* @param array $error
29+
* @param string $path
30+
* @param array<string|int> $error
3131
*/
3232
final public function testAnalysisFails(string $path, array $error): void
3333
{
@@ -41,7 +41,13 @@ final public function testAnalysisFails(string $path, array $error): void
4141
);
4242
}
4343

44+
/**
45+
* @return iterable<string,string[]>
46+
*/
4447
abstract public function provideCasesWhereAnalysisShouldSucceed(): iterable;
4548

49+
/**
50+
* @return iterable<string,array>
51+
*/
4652
abstract public function provideCasesWhereAnalysisShouldFail(): iterable;
4753
}

test/Integration/Rules/ImmutableObjectRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public function provideCasesWhereAnalysisShouldSucceed(): iterable
4040
}
4141
}
4242

43+
/**
44+
* @return iterable<string,array>
45+
*/
4346
public function provideCasesWhereAnalysisShouldFail(): iterable
4447
{
4548
$paths = [
@@ -95,6 +98,9 @@ public function provideCasesWhereAnalysisShouldFail(): iterable
9598
}
9699
}
97100

101+
/**
102+
* @return Rule<\PhpParser\Node>
103+
*/
98104
protected function getRule(): Rule
99105
{
100106
$lexer = new Lexer();

0 commit comments

Comments
 (0)