Skip to content

Commit

Permalink
Merge pull request #848 from kukulich/psalm
Browse files Browse the repository at this point in the history
Less ignored errors in Psalm
  • Loading branch information
Ocramius authored Nov 13, 2021
2 parents 629f401 + 9fde8ea commit 5b2a0a7
Show file tree
Hide file tree
Showing 20 changed files with 147 additions and 197 deletions.
29 changes: 0 additions & 29 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,12 @@
</projectFiles>

<issueHandlers>
<MissingImmutableAnnotation errorLevel="suppress" />

<MissingClosureReturnType errorLevel="suppress" />
<MissingReturnType errorLevel="suppress" />

<PropertyNotSetInConstructor errorLevel="suppress" />
<MissingParamType errorLevel="suppress" />
<MissingClosureParamType errorLevel="suppress" />

<NullableReturnStatement errorLevel="suppress" />
<InvalidNullableReturnType errorLevel="suppress" />

<FalsableReturnStatement errorLevel="suppress" />
<InvalidFalsableReturnType errorLevel="suppress" />

<MoreSpecificReturnType errorLevel="suppress" />
<LessSpecificReturnStatement errorLevel="suppress" />

<PossiblyFalseArgument errorLevel="suppress" />
<PossiblyNullArgument errorLevel="suppress" />

<PropertyTypeCoercion errorLevel="suppress" />

<InvalidScalarArgument>
<!-- Not actually an issue, we send scalar where the called method wait for bool|float|int|string. See vimeo/psalm/issues/2622 -->
<errorLevel type="suppress">
<referencedFunction name="PhpParser\BuilderHelpers::normalizeValue"/>
</errorLevel>
</InvalidScalarArgument>

<RedundantConditionGivenDocblockType>
<!-- Assertion used to check that expected object is indeed an object -->
<errorLevel type="suppress">
<file name="src/Reflection/ReflectionProperty.php"/>
</errorLevel>
</RedundantConditionGivenDocblockType>
</issueHandlers>
</psalm>
12 changes: 6 additions & 6 deletions src/BetterReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@

final class BetterReflection
{
private ?SourceLocator $sourceLocator;
private ?SourceLocator $sourceLocator = null;

private ?Reflector $reflector;
private ?Reflector $reflector = null;

private ?Parser $phpParser;
private ?Parser $phpParser = null;

private ?AstLocator $astLocator;
private ?AstLocator $astLocator = null;

private ?FindReflectionOnLine $findReflectionOnLine;
private ?FindReflectionOnLine $findReflectionOnLine = null;

private ?SourceStubber $sourceStubber;
private ?SourceStubber $sourceStubber = null;

public function sourceLocator(): SourceLocator
{
Expand Down
3 changes: 3 additions & 0 deletions src/Reflection/Adapter/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
use function sprintf;
use function strtolower;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ReflectionClass extends CoreReflectionClass
{
public function __construct(private BetterReflectionClass|BetterReflectionEnum $betterReflectionClass)
Expand Down
3 changes: 3 additions & 0 deletions src/Reflection/Adapter/ReflectionEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
use function sprintf;
use function strtolower;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ReflectionEnum extends CoreReflectionEnum
{
public function __construct(private BetterReflectionEnum $betterReflectionEnum)
Expand Down
3 changes: 3 additions & 0 deletions src/Reflection/Adapter/ReflectionNamedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use ReflectionNamedType as CoreReflectionNamedType;
use Roave\BetterReflection\Reflection\ReflectionNamedType as BetterReflectionNamedType;

/**
* @psalm-suppress MissingImmutableAnnotation
*/
final class ReflectionNamedType extends CoreReflectionNamedType
{
public function __construct(private BetterReflectionNamedType $betterReflectionType)
Expand Down
3 changes: 3 additions & 0 deletions src/Reflection/Adapter/ReflectionParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

use function array_map;

/**
* @psalm-suppress MissingImmutableAnnotation
*/
final class ReflectionParameter extends CoreReflectionParameter
{
public function __construct(private BetterReflectionParameter $betterReflectionParameter)
Expand Down
4 changes: 4 additions & 0 deletions src/Reflection/Adapter/ReflectionProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ public function hasType(): bool
return $this->betterReflectionProperty->hasType();
}

/**
* @psalm-mutation-free
*/
public function getType(): ReflectionUnionType|ReflectionNamedType|ReflectionIntersectionType|null
{
/** @psalm-suppress ImpureMethodCall */
return ReflectionType::fromTypeOrNull($this->betterReflectionProperty->getType());
}

Expand Down
4 changes: 0 additions & 4 deletions src/Reflection/ReflectionClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,6 @@ public function getImmediateProperties(?int $filter = null): array
$this->reflector,
$propertiesNode,
$propertyPositionInNode,
$this->declaringNamespace,
$this,
$this,
false,
Expand Down Expand Up @@ -765,7 +764,6 @@ public function getImmediateProperties(?int $filter = null): array
$this->reflector,
$propertyNode,
0,
$this->declaringNamespace,
$this,
$this,
true,
Expand Down Expand Up @@ -812,7 +810,6 @@ private function addEnumProperties(array $properties): array
$this->reflector,
$propertyNode,
0,
$this->declaringNamespace,
$this,
$this,
false,
Expand Down Expand Up @@ -864,7 +861,6 @@ function (ReflectionClass $trait) use ($filter) {
$this->reflector,
$property->getAst(),
$property->getPositionInAst(),
$trait->declaringNamespace,
$property->getDeclaringClass(),
$this,
$property->isPromoted(),
Expand Down
32 changes: 12 additions & 20 deletions src/Reflection/ReflectionClassConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,31 @@ class ReflectionClassConstant

private ?CompiledValue $compiledValue = null;

private Reflector $reflector;

/** @var ReflectionClass Constant owner */
private ReflectionClass $owner;

private ClassConst $node;

private int $positionInNode;

private function __construct()
{
private function __construct(
private Reflector $reflector,
private ClassConst $node,
private ReflectionClass $owner,
private int $positionInNode,
) {
}

/**
* Create a reflection of a class's constant by Const Node
*
* @internal
*
* @param ClassConst $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
*/
public static function createFromNode(
Reflector $reflector,
ClassConst $node,
int $positionInNode,
ReflectionClass $owner,
): self {
$ref = new self();
$ref->node = $node;
$ref->positionInNode = $positionInNode;
$ref->owner = $owner;
$ref->reflector = $reflector;

return $ref;
return new self(
$reflector,
$node,
$owner,
$positionInNode,
);
}

/**
Expand Down
45 changes: 19 additions & 26 deletions src/Reflection/ReflectionConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,15 @@

class ReflectionConstant implements Reflection
{
private Reflector $reflector;

private Node\Stmt\Const_|Node\Expr\FuncCall $node;

private LocatedSource $locatedSource;

private ?NamespaceNode $declaringNamespace;

private ?int $positionInNode;

private ?CompiledValue $compiledValue = null;

private function __construct()
{
private function __construct(
private Reflector $reflector,
private Node\Stmt\Const_|Node\Expr\FuncCall $node,
private LocatedSource $locatedSource,
private ?NamespaceNode $declaringNamespace = null,
private ?int $positionInNode = null,
) {
}

/**
Expand Down Expand Up @@ -81,14 +76,13 @@ private static function createFromConstKeyword(
?NamespaceNode $namespace,
int $positionInNode,
): self {
$constant = new self();
$constant->reflector = $reflector;
$constant->node = $node;
$constant->locatedSource = $locatedSource;
$constant->declaringNamespace = $namespace;
$constant->positionInNode = $positionInNode;

return $constant;
return new self(
$reflector,
$node,
$locatedSource,
$namespace,
$positionInNode,
);
}

/**
Expand All @@ -101,12 +95,11 @@ private static function createFromDefineFunctionCall(
): self {
ConstantNodeChecker::assertValidDefineFunctionCall($node);

$constant = new self();
$constant->reflector = $reflector;
$constant->node = $node;
$constant->locatedSource = $locatedSource;

return $constant;
return new self(
$reflector,
$node,
$locatedSource,
);
}

/**
Expand Down
20 changes: 6 additions & 14 deletions src/Reflection/ReflectionEnumCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@

class ReflectionEnumCase
{
private Reflector $reflector;

private EnumCase $node;

private ReflectionEnum $enum;

private ?CompiledValue $compiledValue = null;

private function __construct()
{
private function __construct(
private Reflector $reflector,
private EnumCase $node,
private ReflectionEnum $enum,
) {
}

/**
Expand All @@ -42,12 +39,7 @@ public static function createFromNode(
EnumCase $node,
ReflectionEnum $enum,
): self {
$reflection = new self();
$reflection->reflector = $reflector;
$reflection->node = $node;
$reflection->enum = $enum;

return $reflection;
return new self($reflector, $node, $enum);
}

public function getName(): string
Expand Down
19 changes: 14 additions & 5 deletions src/Reflection/ReflectionFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
use Roave\BetterReflection\SourceLocator\Type\ClosureSourceLocator;

use function assert;
use function function_exists;

class ReflectionFunction implements Reflection
Expand All @@ -28,6 +29,17 @@ class ReflectionFunction implements Reflection

private Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $functionNode;

private function __construct(
private Reflector $reflector,
private Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $node,
private LocatedSource $locatedSource,
private ?NamespaceNode $declaringNamespace = null,
) {
assert($node instanceof Node\Stmt\Function_ || $node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction);

$this->functionNode = $node;
}

/**
* @throws IdentifierNotFound
*/
Expand Down Expand Up @@ -63,10 +75,7 @@ public static function createFromNode(
LocatedSource $locatedSource,
?NamespaceNode $namespaceNode = null,
): self {
$function = new self($reflector, $node, $locatedSource, $namespaceNode);
$function->functionNode = $node;

return $function;
return new self($reflector, $node, $locatedSource, $namespaceNode);
}

public function getAst(): Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction
Expand Down Expand Up @@ -123,7 +132,7 @@ public function getClosure(): Closure

$this->assertFunctionExist($functionName);

return static fn (...$args) => $functionName(...$args);
return static fn (mixed ...$args): mixed => $functionName(...$args);
}

/**
Expand Down
10 changes: 0 additions & 10 deletions src/Reflection/ReflectionFunctionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpParser\Node\Expr\Yield_ as YieldNode;
use PhpParser\Node\Expr\YieldFrom as YieldFromNode;
use PhpParser\Node\Param as ParamNode;
use PhpParser\Node\Stmt\Namespace_ as NamespaceNode;
use PhpParser\NodeTraverser;
use PhpParser\Parser;
use PhpParser\PrettyPrinter\Standard as StandardPrettyPrinter;
Expand All @@ -24,7 +23,6 @@
use Roave\BetterReflection\Reflection\Attribute\ReflectionAttributeHelper;
use Roave\BetterReflection\Reflection\Exception\InvalidArrowFunctionBodyNode;
use Roave\BetterReflection\Reflection\Exception\Uncloneable;
use Roave\BetterReflection\Reflector\Reflector;
use Roave\BetterReflection\SourceLocator\Ast\Exception\ParseToAstFailure;
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;
use Roave\BetterReflection\SourceLocator\Type\ClosureSourceLocator;
Expand All @@ -44,14 +42,6 @@ trait ReflectionFunctionAbstract
{
private static ?Parser $parser;

private function __construct(
private Reflector $reflector,
private Node\Stmt\ClassMethod|Node\Stmt\Function_|Node\Expr\Closure|Node\Expr\ArrowFunction $node,
private LocatedSource $locatedSource,
private ?NamespaceNode $declaringNamespace = null,
) {
}

abstract public function __toString(): string;

abstract public function getShortName(): string;
Expand Down
Loading

0 comments on commit 5b2a0a7

Please sign in to comment.