Skip to content

Commit

Permalink
Update dependency phpstan/phpstan to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Nov 17, 2024
1 parent 772e125 commit 7c5fa68
Show file tree
Hide file tree
Showing 45 changed files with 152 additions and 139 deletions.
19 changes: 13 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ parameters:
- phar://%currentWorkingDirectory%/test/unit/Fixture/autoload.phar/vendor/autoload.php

ignoreErrors:
# Impossible to define type hint for anonymous class
-
message: '#Call to an undefined method PhpParser\\NodeVisitorAbstract::(getNode|setConstantName)\(\)#'
path: src/SourceLocator/Type/AutoloadSourceLocator.php
identifier: missingType.generics
-
message: '#Method Roave\\BetterReflection\\Reflection\\ReflectionClass::computeModifiers\(\) never returns \d+ so it can be removed from the return type#'
path: src/Reflection/ReflectionClass.php
identifier: possiblyImpure.functionCall
-
identifier: missingType.generics
identifier: possiblyImpure.methodCall
-
identifier: possiblyImpure.new
-
identifier: property.readOnlyByPhpDocAssignNotInConstructor
-
identifier: staticMethod.alreadyNarrowedType
path: test/unit/
-
identifier: varTag.nativeType
path: test/unit/
4 changes: 2 additions & 2 deletions src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ public function getFileName(): string|false
}

/** @psalm-mutation-free */
public function getStartLine(): int|false
public function getStartLine(): int
{
return $this->betterReflectionClass->getStartLine();
}

/** @psalm-mutation-free */
public function getEndLine(): int|false
public function getEndLine(): int
{
return $this->betterReflectionClass->getEndLine();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Adapter/ReflectionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ public function getFileName(): string|false
return $fileName !== null ? FileHelper::normalizeSystemPath($fileName) : false;
}

public function getStartLine(): int|false
public function getStartLine(): int
{
return $this->betterReflectionEnum->getStartLine();
}

public function getEndLine(): int|false
public function getEndLine(): int
{
return $this->betterReflectionEnum->getEndLine();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionEnumBackedCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function hasType(): bool
return false;
}

public function getType(): ReflectionUnionType|ReflectionNamedType|ReflectionIntersectionType|null
public function getType(): null
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionEnumUnitCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function hasType(): bool
return false;
}

public function getType(): ReflectionUnionType|ReflectionNamedType|ReflectionIntersectionType|null
public function getType(): null
{
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Adapter/ReflectionFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ public function getDocComment(): string|false
}

/** @psalm-mutation-free */
public function getStartLine(): int|false
public function getStartLine(): int
{
return $this->betterReflectionFunction->getStartLine();
}

/** @psalm-mutation-free */
public function getEndLine(): int|false
public function getEndLine(): int
{
return $this->betterReflectionFunction->getEndLine();
}
Expand Down
1 change: 1 addition & 0 deletions src/Reflection/Adapter/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ public function hasPrototype(): bool
* @codeCoverageIgnore
* @infection-ignore-all
* @psalm-pure
* @phpstan-ignore pureMethod.void
*/
public function setAccessible(bool $accessible): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Adapter/ReflectionObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ public function getFileName(): string|false
}

/** @psalm-mutation-free */
public function getStartLine(): int|false
public function getStartLine(): int
{
return $this->betterReflectionObject->getStartLine();
}

/** @psalm-mutation-free */
public function getEndLine(): int|false
public function getEndLine(): int
{
return $this->betterReflectionObject->getEndLine();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Reflection/Adapter/ReflectionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getClass(): CoreReflectionClass|null
}

try {
/** @phpstan-ignore-next-line */
/** @phpstan-ignore variable.undefined */
return new ReflectionClass($classType->getClass());
} catch (LogicException) {
return null;
Expand Down Expand Up @@ -217,6 +217,7 @@ public function hasType(): bool
return $this->betterReflectionParameter->hasType();
}

/** @phpstan-ignore return.unusedType */
public function getType(): ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|ReflectionType|null
{
return ReflectionType::fromTypeOrNull($this->betterReflectionParameter->getType());
Expand Down
7 changes: 5 additions & 2 deletions src/Reflection/ReflectionAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ReflectionAttribute
private string $name;

/** @var array<int|string, Node\Expr> */
private array $arguments = [];
private array $arguments;

/** @internal */
public function __construct(
Expand All @@ -31,9 +31,12 @@ public function __construct(
) {
$this->name = $node->name->toString();

$arguments = [];
foreach ($node->args as $argNo => $arg) {
$this->arguments[$arg->name?->toString() ?? $argNo] = $arg->value;
$arguments[$arg->name?->toString() ?? $argNo] = $arg->value;
}

$this->arguments = $arguments;
}

/** @internal */
Expand Down
18 changes: 11 additions & 7 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,19 @@ protected function __construct(
private LocatedSource $locatedSource,
private string|null $namespace = null,
) {
$this->name = null;
$this->shortName = null;
$name = null;
$shortName = null;

if ($node->name instanceof Node\Identifier) {
$namespacedName = $node->namespacedName;
assert($namespacedName instanceof Node\Name);
/** @psalm-var class-string|trait-string */
$name = $namespacedName->toString();
$shortName = $node->name->name;

$this->name = $name;
$this->shortName = $shortName;
}

$this->name = $name;
$this->shortName = $shortName;
$this->isInterface = $node instanceof InterfaceNode;
$this->isTrait = $node instanceof TraitNode;
$this->isEnum = $node instanceof EnumNode;
Expand Down Expand Up @@ -380,7 +380,7 @@ private function createMethodsFromTrait(ReflectionMethod $method): array
$createMethod = function (string|null $aliasMethodName) use ($method, $methodModifiers): ReflectionMethod {
assert($aliasMethodName === null || $aliasMethodName !== '');

/** @phpstan-ignore-next-line */
/** @phpstan-ignore argument.type */
return $method->withImplementingClass($this, $aliasMethodName, $methodModifiers);
};

Expand Down Expand Up @@ -1229,7 +1229,11 @@ public function getModifiers(): int
return $this->modifiers;
}

/** @return int-mask-of<ReflectionClassAdapter::IS_*> */
/**
* @return int-mask-of<ReflectionClassAdapter::IS_*>
*
* @phpstan-ignore-next-line return.unusedType
*/
private function computeModifiers(ClassNode|InterfaceNode|TraitNode|EnumNode $node): int
{
if (! $node instanceof ClassNode) {
Expand Down
4 changes: 3 additions & 1 deletion src/Reflection/ReflectionConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,9 @@ private function setNamesFromNode(Node\Stmt\Const_|Node\Expr\FuncCall $node, int
$shortName = $constNode->name->name;
}

$this->name = $name;
/** @phpstan-ignore assign.readOnlyPropertyByPhpDoc */
$this->name = $name;
/** @phpstan-ignore assign.readOnlyPropertyByPhpDoc */
$this->shortName = $shortName;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Reflection/ReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public function withImplementingClass(ReflectionClass $implementingClass, string
/** @internal */
public function withCurrentClass(ReflectionClass $currentClass): self
{
$clone = clone $this;
$clone = clone $this;
/** @phpstan-ignore property.readOnlyByPhpDocAssignNotInConstructor */
$clone->currentClass = $currentClass;

if ($clone->returnType !== null) {
Expand Down Expand Up @@ -430,6 +431,7 @@ private function callStaticMethod(array $args): mixed
/** @psalm-suppress InvalidStringClass */
$closure = Closure::bind(fn (string $implementingClassName, string $_methodName, array $methodArgs): mixed => $implementingClassName::{$_methodName}(...$methodArgs), null, $implementingClassName);

/** @phpstan-ignore function.alreadyNarrowedType, instanceof.alwaysTrue */
assert($closure instanceof Closure);

return $closure->__invoke($implementingClassName, $this->getName(), $args);
Expand All @@ -441,6 +443,7 @@ private function callObjectMethod(object $object, array $args): mixed
/** @psalm-suppress MixedMethodCall */
$closure = Closure::bind(fn (object $object, string $methodName, array $methodArgs): mixed => $object->{$methodName}(...$methodArgs), $object, $this->getImplementingClass()->getName());

/** @phpstan-ignore function.alreadyNarrowedType, instanceof.alwaysTrue */
assert($closure instanceof Closure);

return $closure->__invoke($object, $this->getName(), $args);
Expand Down
6 changes: 5 additions & 1 deletion src/Reflection/ReflectionProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public function isInitialized(object|null $object = null): bool

return true;

// @phpstan-ignore-next-line
/** @phpstan-ignore catch.neverThrown */
} catch (Error $e) {
if (str_contains($e->getMessage(), 'must not be accessed before initialization')) {
return false;
Expand Down Expand Up @@ -452,6 +452,7 @@ public function getValue(object|null $object = null): mixed

$closure = Closure::bind(fn (string $implementingClassName, string $propertyName): mixed => $implementingClassName::${$propertyName}, null, $implementingClassName);

/** @phpstan-ignore function.alreadyNarrowedType, instanceof.alwaysTrue */
assert($closure instanceof Closure);

return $closure->__invoke($implementingClassName, $this->getName());
Expand All @@ -461,6 +462,7 @@ public function getValue(object|null $object = null): mixed

$closure = Closure::bind(fn (object $instance, string $propertyName): mixed => $instance->{$propertyName}, $instance, $implementingClassName);

/** @phpstan-ignore function.alreadyNarrowedType, instanceof.alwaysTrue */
assert($closure instanceof Closure);

return $closure->__invoke($instance, $this->getName());
Expand All @@ -484,6 +486,7 @@ public function setValue(mixed $object, mixed $value = null): void
$_implementingClassName::${$_propertyName} = $value;
}, null, $implementingClassName);

/** @phpstan-ignore function.alreadyNarrowedType, instanceof.alwaysTrue */
assert($closure instanceof Closure);

$closure->__invoke($implementingClassName, $this->getName(), func_num_args() === 2 ? $value : $object);
Expand All @@ -497,6 +500,7 @@ public function setValue(mixed $object, mixed $value = null): void
$instance->{$propertyName} = $value;
}, $instance, $implementingClassName);

/** @phpstan-ignore function.alreadyNarrowedType, instanceof.alwaysTrue */
assert($closure instanceof Closure);

$closure->__invoke($instance, $this->getName(), $value);
Expand Down
20 changes: 8 additions & 12 deletions src/SourceLocator/SourceStubber/ReflectionSourceStubber.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function __construct()
/** @param class-string|trait-string $className */
public function generateClassStub(string $className): StubData|null
{
/** @phpstan-ignore staticMethod.alreadyNarrowedType */
if (! ClassExistenceChecker::exists($className)) {
return null;
}
Expand Down Expand Up @@ -139,10 +140,7 @@ public function generateFunctionStub(string $functionName): StubData|null
$this->addDocComment($functionNode, $functionReflection);
$this->addParameters($functionNode, $functionReflection);

$returnType = $functionReflection->getReturnType();
if ($returnType === null && method_exists($functionReflection, 'getTentativeReturnType')) {
$returnType = $functionReflection->getTentativeReturnType();
}
$returnType = $functionReflection->getReturnType() ?? $functionReflection->getTentativeReturnType();

if ($returnType !== null) {
assert($returnType instanceof CoreReflectionNamedType || $returnType instanceof CoreReflectionUnionType || $returnType instanceof CoreReflectionIntersectionType);
Expand Down Expand Up @@ -228,7 +226,7 @@ private function addDocComment(
$annotations[] = '@deprecated';
}

if (method_exists($reflection, 'hasTentativeReturnType') && $reflection->hasTentativeReturnType()) {
if ($reflection->hasTentativeReturnType()) {
$annotations[] = sprintf('@%s', AnnotationHelper::TENTATIVE_RETURN_TYPE_ANNOTATION);
}
}
Expand All @@ -253,6 +251,7 @@ private function addEnumBackingType(Enum_ $enumNode, CoreReflectionEnum $enumRef
}

$backingType = $enumReflection->getBackingType();
/** @phpstan-ignore function.alreadyNarrowedType, instanceof.alwaysTrue */
assert($backingType instanceof CoreReflectionNamedType);

$enumNode->setScalarType($backingType->getName());
Expand Down Expand Up @@ -378,7 +377,7 @@ private function isPropertyDeclaredInClass(CoreReflectionProperty $propertyRefle

private function addPropertyModifiers(Property $propertyNode, CoreReflectionProperty $propertyReflection): void
{
if (method_exists($propertyReflection, 'isReadOnly') && $propertyReflection->isReadOnly()) {
if ($propertyReflection->isReadOnly()) {
$propertyNode->makeReadonly();
}

Expand Down Expand Up @@ -411,7 +410,7 @@ private function addEnumCases(Enum_ $enumNode, CoreReflectionEnum $enumReflectio
private function addClassConstants(Class_|Interface_|Trait_|Enum_ $classNode, CoreReflectionClass $classReflection): void
{
foreach ($classReflection->getReflectionConstants() as $constantReflection) {
if (method_exists($constantReflection, 'isEnumCase') && $constantReflection->isEnumCase()) {
if ($constantReflection->isEnumCase()) {
continue;
}

Expand Down Expand Up @@ -442,7 +441,7 @@ private function addClassConstants(Class_|Interface_|Trait_|Enum_ $classNode, Co

private function addClassConstantModifiers(ClassConst $classConstantNode, CoreReflectionClassConstant $classConstantReflection): void
{
if (method_exists($classConstantReflection, 'isFinal') && $classConstantReflection->isFinal()) {
if ($classConstantReflection->isFinal()) {
$classConstantNode->makeFinal();
}

Expand All @@ -468,10 +467,7 @@ private function addMethods(Class_|Interface_|Trait_|Enum_ $classNode, CoreRefle
$this->addDocComment($methodNode, $methodReflection);
$this->addParameters($methodNode, $methodReflection);

$returnType = $methodReflection->getReturnType();
if ($returnType === null && method_exists($methodReflection, 'getTentativeReturnType')) {
$returnType = $methodReflection->getTentativeReturnType();
}
$returnType = $methodReflection->getReturnType() ?? $methodReflection->getTentativeReturnType();

if ($returnType !== null) {
assert($returnType instanceof CoreReflectionNamedType || $returnType instanceof CoreReflectionUnionType || $returnType instanceof CoreReflectionIntersectionType);
Expand Down
2 changes: 0 additions & 2 deletions src/SourceLocator/Type/AnonymousClassObjectSourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpParser\NodeVisitorAbstract;
use PhpParser\Parser;
use ReflectionClass as CoreReflectionClass;
use ReflectionException;
use Roave\BetterReflection\Identifier\Identifier;
use Roave\BetterReflection\Identifier\IdentifierType;
use Roave\BetterReflection\Reflection\Reflection;
Expand All @@ -36,7 +35,6 @@ final class AnonymousClassObjectSourceLocator implements SourceLocator
{
private CoreReflectionClass $coreClassReflection;

/** @throws ReflectionException */
public function __construct(object $anonymousClassObject, private Parser $parser)
{
$this->coreClassReflection = new CoreReflectionClass($anonymousClassObject);
Expand Down
10 changes: 8 additions & 2 deletions src/SourceLocator/Type/AutoloadSourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ private function locateConstantByName(string $constantName): array|null
return null;
}

/** @psalm-suppress UndefinedMethod */
/**
* @psalm-suppress UndefinedMethod
* @phpstan-ignore method.notFound
*/
$this->constantVisitor->setConstantName($constantName);

$constantFileName = null;
Expand All @@ -268,7 +271,10 @@ private function locateConstantByName(string $constantName): array|null

$this->nodeTraverser->traverse($ast);

/** @psalm-suppress UndefinedMethod */
/**
* @psalm-suppress UndefinedMethod
* @phpstan-ignore method.notFound
*/
if ($this->constantVisitor->getNode() !== null) {
$constantFileName = $includedFileName;
break;
Expand Down
Loading

0 comments on commit 7c5fa68

Please sign in to comment.