Skip to content

Commit df75bef

Browse files
authored
Merge pull request #1508 from kukulich/phpstan7
PHPStan level 7
2 parents 30fb5d8 + 571b0f2 commit df75bef

File tree

54 files changed

+330
-132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+330
-132
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ includes:
33
- tools/vendor/phpstan/phpstan-phpunit/rules.neon
44

55
parameters:
6-
level: 6
6+
level: 7
77

88
paths:
99
- src

src/Reflection/Adapter/ReflectionEnum.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Roave\BetterReflection\Reflection\ReflectionMethod as BetterReflectionMethod;
1919
use Roave\BetterReflection\Reflection\ReflectionProperty as BetterReflectionProperty;
2020
use Roave\BetterReflection\Util\FileHelper;
21+
use UnitEnum;
2122
use ValueError;
2223

2324
use function array_combine;
@@ -56,6 +57,7 @@ public function __get(string $name): mixed
5657

5758
public function getName(): string
5859
{
60+
/** @phpstan-var class-string<UnitEnum> */
5961
return $this->betterReflectionEnum->getName();
6062
}
6163

src/Reflection/ReflectionAttribute.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/** @psalm-immutable */
1717
class ReflectionAttribute
1818
{
19-
/** @var non-empty-string */
19+
/** @var class-string */
2020
private string $name;
2121

2222
/** @var array<int|string, Node\Expr> */
@@ -29,7 +29,10 @@ public function __construct(
2929
private ReflectionClass|ReflectionMethod|ReflectionFunction|ReflectionClassConstant|ReflectionEnumCase|ReflectionProperty|ReflectionParameter $owner,
3030
private bool $isRepeated,
3131
) {
32-
$this->name = $node->name->toString();
32+
/** @var class-string $name */
33+
$name = $node->name->toString();
34+
35+
$this->name = $name;
3336

3437
$arguments = [];
3538
foreach ($node->args as $argNo => $arg) {
@@ -48,7 +51,7 @@ public function withOwner(ReflectionClass|ReflectionMethod|ReflectionFunction|Re
4851
return $clone;
4952
}
5053

51-
/** @return non-empty-string */
54+
/** @return class-string */
5255
public function getName(): string
5356
{
5457
return $this->name;

src/Reflection/ReflectionEnum.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,16 @@ private function __construct(
4747
/**
4848
* @internal
4949
*
50-
* @param EnumNode $node
5150
* @param non-empty-string|null $namespace
52-
*
53-
* @psalm-suppress MoreSpecificImplementedParamType
5451
*/
5552
public static function createFromNode(
5653
Reflector $reflector,
5754
ClassNode|InterfaceNode|TraitNode|EnumNode $node,
5855
LocatedSource $locatedSource,
5956
string|null $namespace = null,
6057
): self {
58+
assert($node instanceof EnumNode);
59+
6160
return new self($reflector, $node, $locatedSource, $namespace);
6261
}
6362

src/Reflection/ReflectionFunction.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ private function assertIsNoClosure(): void
170170
}
171171
}
172172

173-
/** @throws FunctionDoesNotExist */
173+
/**
174+
* @throws FunctionDoesNotExist
175+
*
176+
* @phpstan-assert callable $functionName
177+
*/
174178
private function assertFunctionExist(string $functionName): void
175179
{
176180
if (! function_exists($functionName)) {

src/Reflection/ReflectionIntersectionType.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ public function withOwner(ReflectionParameter|ReflectionMethod|ReflectionFunctio
4040
{
4141
$clone = clone $this;
4242

43-
foreach ($clone->types as $typeNo => $innerType) {
44-
$clone->types[$typeNo] = $innerType->withOwner($owner);
45-
}
43+
$clone->types = array_map(static fn (ReflectionNamedType $type): ReflectionNamedType => $type->withOwner($owner), $clone->types);
4644

4745
return $clone;
4846
}

src/Reflection/ReflectionProperty.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -667,13 +667,13 @@ private function assertObject(mixed $object): object
667667
/** @return int-mask-of<ReflectionPropertyAdapter::IS_*> */
668668
private function computeModifiers(PropertyNode $node): int
669669
{
670-
$modifiers = $node->isReadonly() ? CoreReflectionProperty::IS_READONLY : 0;
671-
$modifiers += $node->isStatic() ? CoreReflectionProperty::IS_STATIC : 0;
672-
$modifiers += $node->isPrivate() ? CoreReflectionProperty::IS_PRIVATE : 0;
670+
$modifiers = $node->isReadonly() ? ReflectionPropertyAdapter::IS_READONLY : 0;
671+
$modifiers += $node->isStatic() ? ReflectionPropertyAdapter::IS_STATIC : 0;
672+
$modifiers += $node->isPrivate() ? ReflectionPropertyAdapter::IS_PRIVATE : 0;
673673
$modifiers += ! $node->isPrivate() && $node->isPrivateSet() ? ReflectionPropertyAdapter::IS_PRIVATE_SET_COMPATIBILITY : 0;
674-
$modifiers += $node->isProtected() ? CoreReflectionProperty::IS_PROTECTED : 0;
674+
$modifiers += $node->isProtected() ? ReflectionPropertyAdapter::IS_PROTECTED : 0;
675675
$modifiers += ! $node->isProtected() && $node->isProtectedSet() ? ReflectionPropertyAdapter::IS_PROTECTED_SET_COMPATIBILITY : 0;
676-
$modifiers += $node->isPublic() ? CoreReflectionProperty::IS_PUBLIC : 0;
676+
$modifiers += $node->isPublic() ? ReflectionPropertyAdapter::IS_PUBLIC : 0;
677677
$modifiers += $node->isFinal() ? ReflectionPropertyAdapter::IS_FINAL_COMPATIBILITY : 0;
678678
$modifiers += $node->isAbstract() ? ReflectionPropertyAdapter::IS_ABSTRACT_COMPATIBILITY : 0;
679679

@@ -688,11 +688,12 @@ private function computeModifiers(PropertyNode $node): int
688688
! ($modifiers & (ReflectionPropertyAdapter::IS_PROTECTED_SET_COMPATIBILITY | ReflectionPropertyAdapter::IS_PRIVATE_SET_COMPATIBILITY))
689689
&& ! $node->isPublicSet()
690690
&& $node->isPublic()
691-
&& ($modifiers & CoreReflectionProperty::IS_READONLY)
691+
&& ($modifiers & ReflectionPropertyAdapter::IS_READONLY)
692692
) {
693693
$modifiers += ReflectionPropertyAdapter::IS_PROTECTED_SET_COMPATIBILITY;
694694
}
695695

696+
/** @phpstan-ignore return.type */
696697
return $modifiers;
697698
}
698699

src/Reflection/ReflectionUnionType.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public function withOwner(ReflectionParameter|ReflectionMethod|ReflectionFunctio
4343
{
4444
$clone = clone $this;
4545

46-
foreach ($clone->types as $typeNo => $innerType) {
47-
$clone->types[$typeNo] = $innerType->withOwner($owner);
48-
}
46+
$clone->types = array_map(static fn (ReflectionNamedType|ReflectionIntersectionType $type): ReflectionNamedType|ReflectionIntersectionType => $type->withOwner($owner), $clone->types);
4947

5048
return $clone;
5149
}

src/SourceLocator/SourceStubber/PhpStormStubs/CachingVisitor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private function updateConstantValue(Node\Expr\FuncCall|Node\Const_ $node, strin
201201
// prevent autoloading while discovering class constants
202202
$parts = explode('::', $constantName, 2);
203203
if (count($parts) === 2) {
204-
[$className, $classConstName] = $parts;
204+
[$className] = $parts;
205205

206206
if (! class_exists($className, false)) {
207207
return;
@@ -217,6 +217,7 @@ private function updateConstantValue(Node\Expr\FuncCall|Node\Const_ $node, strin
217217
$constantValue = @constant($constantName);
218218
$normalizedConstantValue = is_resource($constantValue)
219219
? $this->builderFactory->funcCall('constant', [$constantName])
220+
/** @phpstan-ignore argument.type */
220221
: $this->builderFactory->val($constantValue);
221222

222223
if ($node instanceof Node\Expr\FuncCall) {

src/SourceLocator/SourceStubber/ReflectionSourceStubber.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,15 +598,21 @@ private function setParameterDefaultValue(ReflectionParameter $parameterReflecti
598598
private function formatType(CoreReflectionType $type): Name|NullableType|UnionType|IntersectionType
599599
{
600600
if ($type instanceof CoreReflectionIntersectionType) {
601+
/** @phpstan-var list<CoreReflectionNamedType> $intersectionTypes */
602+
$intersectionTypes = $type->getTypes();
603+
601604
/** @var list<Name> $types */
602-
$types = $this->formatTypes($type->getTypes());
605+
$types = $this->formatTypes($intersectionTypes);
603606

604607
return new IntersectionType($types);
605608
}
606609

607610
if ($type instanceof CoreReflectionUnionType) {
611+
/** @phpstan-var list<CoreReflectionIntersectionType|CoreReflectionNamedType> $unionTypes */
612+
$unionTypes = $type->getTypes();
613+
608614
/** @var list<Name|IntersectionType> $types */
609-
$types = $this->formatTypes($type->getTypes());
615+
$types = $this->formatTypes($unionTypes);
610616

611617
return new UnionType($types);
612618
}

0 commit comments

Comments
 (0)