Skip to content

Commit

Permalink
Observe @no-named-arguments above class
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 3, 2024
1 parent b98abe0 commit 815270d
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6005,6 +6005,9 @@ public function getPhpDocs(Scope $scope, Node\FunctionLike|Node\Stmt\Property $n
$isPure = $resolvedPhpDoc->isPure();
$isAllowedPrivateMutation = $resolvedPhpDoc->isAllowedPrivateMutation();
$acceptsNamedArguments = $resolvedPhpDoc->acceptsNamedArguments();
if ($acceptsNamedArguments && $scope->isInClass()) {
$acceptsNamedArguments = $scope->getClassReflection()->acceptsNamedArguments();
}
$isReadOnly = $isReadOnly || $resolvedPhpDoc->isReadOnly();
$asserts = Assertions::createFromResolvedPhpDocBlock($resolvedPhpDoc);
$selfOutType = $resolvedPhpDoc->getSelfOutTag() !== null ? $resolvedPhpDoc->getSelfOutTag()->getType() : null;
Expand Down
12 changes: 12 additions & 0 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class ClassReflection

private ?bool $hasConsistentConstructor = null;

private ?bool $acceptsNamedArguments = null;

private ?TemplateTypeMap $templateTypeMap = null;

private ?TemplateTypeMap $activeTemplateTypeMap = null;
Expand Down Expand Up @@ -1243,6 +1245,16 @@ public function hasConsistentConstructor(): bool
return $this->hasConsistentConstructor;
}

public function acceptsNamedArguments(): bool
{
if ($this->acceptsNamedArguments === null) {
$resolvedPhpDoc = $this->getResolvedPhpDoc();
$this->acceptsNamedArguments = $resolvedPhpDoc === null || $resolvedPhpDoc->acceptsNamedArguments();
}

return $this->acceptsNamedArguments;
}

public function isFinalByKeyword(): bool
{
if ($this->isAnonymous()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,4 +614,20 @@ public function testGenericsInCallableInConstructor(): void
$this->analyse([__DIR__ . '/data/generics-in-callable-in-constructor.php'], []);
}

public function testBug11275(): void
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Test requires PHP 7.4.');
}

$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-11275.php'], [
[
'Property Bug11275\D::$b (list<Bug11275\B>) does not accept array<int|string, Bug11275\B>.',
50,
'array<int|string, Bug11275\B> might not be a list.',
],
]);
}

}
52 changes: 52 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-11275.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php // lint >= 7.4

namespace Bug11275;

/**
* @no-named-arguments
*/
final class A
{
/**
* @var list<B>
*/
private array $b;

public function __construct(B ...$b)
{
$this->b = $b;
}
}

final class B
{
}

final class C
{
/**
* @var list<B>
*/
private array $b;

/**
* @no-named-arguments
*/
public function __construct(B ...$b)
{
$this->b = $b;
}
}

final class D
{
/**
* @var list<B>
*/
private array $b;

public function __construct(B ...$b)
{
$this->b = $b;
}
}

0 comments on commit 815270d

Please sign in to comment.