Skip to content

Commit 3863033

Browse files
committed
Merge remote-tracking branch 'origin/1.12.x' into 2.0.x
2 parents 4edc329 + f158d5b commit 3863033

File tree

6 files changed

+87
-5
lines changed

6 files changed

+87
-5
lines changed

src/Rules/Classes/ClassAttributesRule.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
use Attribute;
66
use PhpParser\Node;
77
use PHPStan\Analyser\Scope;
8+
use PHPStan\Node\InClassNode;
89
use PHPStan\Rules\AttributesCheck;
910
use PHPStan\Rules\Rule;
1011

1112
/**
12-
* @implements Rule<Node\Stmt\ClassLike>
13+
* @implements Rule<InClassNode>
1314
*/
1415
final class ClassAttributesRule implements Rule
1516
{
@@ -20,14 +21,16 @@ public function __construct(private AttributesCheck $attributesCheck)
2021

2122
public function getNodeType(): string
2223
{
23-
return Node\Stmt\ClassLike::class;
24+
return InClassNode::class;
2425
}
2526

2627
public function processNode(Node $node, Scope $scope): array
2728
{
29+
$classLikeNode = $node->getOriginalNode();
30+
2831
return $this->attributesCheck->check(
2932
$scope,
30-
$node->attrGroups,
33+
$classLikeNode->attrGroups,
3134
Attribute::TARGET_CLASS,
3235
'class',
3336
);

src/Rules/FunctionCallParametersCheck.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,10 @@ public function check(
325325
if ($this->checkArgumentTypes) {
326326
$parameterType = TypeUtils::resolveLateResolvableTypes($parameter->getType());
327327

328-
if (!$parameter->passedByReference()->createsNewVariable() || !$isBuiltin) {
328+
if (
329+
!$parameter->passedByReference()->createsNewVariable()
330+
|| (!$isBuiltin && !$argumentValueType instanceof ErrorType)
331+
) {
329332
$accepts = $this->ruleLevelHelper->accepts($parameterType, $argumentValueType, $scope->isDeclareStrictTypes());
330333

331334
if (!$accepts->result) {

tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@
2222
class ClassAttributesRuleTest extends RuleTestCase
2323
{
2424

25+
private bool $checkExplicitMixed = false;
26+
27+
private bool $checkImplicitMixed = false;
28+
2529
protected function getRule(): Rule
2630
{
2731
$reflectionProvider = $this->createReflectionProvider();
2832
return new ClassAttributesRule(
2933
new AttributesCheck(
3034
$reflectionProvider,
3135
new FunctionCallParametersCheck(
32-
new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false),
36+
new RuleLevelHelper($reflectionProvider, true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false),
3337
new NullsafeCheck(),
3438
new PhpVersion(80000),
3539
new UnresolvableTypeHelper(),
@@ -147,4 +151,20 @@ public function testAllowDynamicPropertiesAttribute(): void
147151
$this->analyse([__DIR__ . '/data/allow-dynamic-properties-attribute.php'], []);
148152
}
149153

154+
public function testBug12011(): void
155+
{
156+
if (PHP_VERSION_ID < 80300) {
157+
$this->markTestSkipped('Test requires PHP 8.3.');
158+
}
159+
160+
$this->checkExplicitMixed = true;
161+
$this->checkImplicitMixed = true;
162+
$this->analyse([__DIR__ . '/data/bug-12011.php'], [
163+
[
164+
'Parameter #1 $name of attribute class Bug12011\Table constructor expects string|null, int given.',
165+
23,
166+
],
167+
]);
168+
}
169+
150170
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Bug12011;
4+
5+
use Attribute;
6+
7+
#[Table(self::TABLE_NAME)]
8+
class HelloWorld
9+
{
10+
private const string TABLE_NAME = 'table';
11+
}
12+
13+
#[Attribute(Attribute::TARGET_CLASS)]
14+
final class Table
15+
{
16+
public function __construct(
17+
public readonly string|null $name = null,
18+
public readonly string|null $schema = null,
19+
) {
20+
}
21+
}
22+
23+
#[Table(self::TABLE_NAME)]
24+
class HelloWorld2
25+
{
26+
private const int TABLE_NAME = 1;
27+
}

tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,4 +852,12 @@ public function testBug10872(): void
852852
$this->analyse([__DIR__ . '/data/bug-10872.php'], []);
853853
}
854854

855+
public function testBug12015(): void
856+
{
857+
$this->checkThisOnly = false;
858+
$this->checkExplicitMixed = true;
859+
$this->checkImplicitMixed = true;
860+
$this->analyse([__DIR__ . '/data/bug-12015.php'], []);
861+
}
862+
855863
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Bug12015;
4+
5+
class HelloWorld
6+
{
7+
8+
/**
9+
* @param-out int $ref
10+
*/
11+
public static function passRef(?int &$ref): void
12+
{
13+
$ref = 1;
14+
}
15+
}
16+
17+
function test(): void
18+
{
19+
HelloWorld::passRef($storeHere);
20+
echo $storeHere;
21+
}

0 commit comments

Comments
 (0)