Skip to content

Commit 2851e0b

Browse files
committed
Merge branch '1.9.x' into 1.10.x
2 parents e4e4240 + 1f608dc commit 2851e0b

13 files changed

+79
-428
lines changed

src/Analyser/MutatingScope.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,26 +2423,13 @@ public function enterTrait(ClassReflection $traitReflection): self
24232423
if (count($traitNameParts) > 1) {
24242424
$namespace = implode('\\', array_slice($traitNameParts, 0, -1));
24252425
}
2426-
2427-
$traitContext = $this->context->enterTrait($traitReflection);
2428-
$classReflection = $traitContext->getClassReflection();
2429-
if ($classReflection === null) {
2430-
throw new ShouldNotHappenException();
2431-
}
2432-
2433-
$thisHolder = ExpressionTypeHolder::createYes(new Variable('this'), new ThisType($classReflection, null, $traitReflection));
2434-
$expressionTypes = $this->expressionTypes;
2435-
$expressionTypes['$this'] = $thisHolder;
2436-
$nativeExpressionTypes = $this->nativeExpressionTypes;
2437-
$nativeExpressionTypes['$this'] = $thisHolder;
2438-
24392426
return $this->scopeFactory->create(
2440-
$traitContext,
2427+
$this->context->enterTrait($traitReflection),
24412428
$this->isDeclareStrictTypes(),
24422429
$this->getFunction(),
24432430
$namespace,
2444-
$expressionTypes,
2445-
$nativeExpressionTypes,
2431+
$this->expressionTypes,
2432+
$this->nativeExpressionTypes,
24462433
[],
24472434
$this->inClosureBindScopeClasses,
24482435
$this->anonymousFunctionReflection,

src/Type/ObjectType.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -304,31 +304,6 @@ public function isSuperTypeOf(Type $type): TrinaryLogic
304304
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createMaybe();
305305
}
306306

307-
if ($type instanceof ThisType && $type->isInTrait()) {
308-
if ($type->getSubtractedType() !== null) {
309-
$isSuperType = $type->getSubtractedType()->isSuperTypeOf($this);
310-
if ($isSuperType->yes()) {
311-
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createNo();
312-
}
313-
}
314-
315-
if ($this->getClassReflection() === null) {
316-
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createMaybe();
317-
}
318-
319-
$thisClassReflection = $this->getClassReflection();
320-
if ($thisClassReflection->isTrait()) {
321-
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createNo();
322-
}
323-
324-
$traitReflection = $type->getTraitReflection();
325-
if ($thisClassReflection->isFinal() && !$thisClassReflection->hasTraitUse($traitReflection->getName())) {
326-
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createNo();
327-
}
328-
329-
return self::$superTypes[$thisDescription][$description] = TrinaryLogic::createMaybe();
330-
}
331-
332307
$transformResult = static fn (TrinaryLogic $result) => $result;
333308
if ($this->subtractedType !== null) {
334309
$isSuperType = $this->subtractedType->isSuperTypeOf($type);

src/Type/ThisType.php

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use PHPStan\Reflection\ClassReflection;
66
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
77
use PHPStan\TrinaryLogic;
8-
use function get_class;
98
use function sprintf;
109

1110
/** @api */
@@ -18,70 +17,24 @@ class ThisType extends StaticType
1817
public function __construct(
1918
ClassReflection $classReflection,
2019
?Type $subtractedType = null,
21-
private ?ClassReflection $traitReflection = null,
2220
)
2321
{
2422
parent::__construct($classReflection, $subtractedType);
2523
}
2624

27-
public function equals(Type $type): bool
28-
{
29-
if (get_class($type) !== static::class) {
30-
return false;
31-
}
32-
33-
/** @var ThisType $type */
34-
$type = $type;
35-
$equals = $this->getStaticObjectType()->equals($type->getStaticObjectType());
36-
if (!$equals) {
37-
return false;
38-
}
39-
40-
if ($this->getTraitReflection() === null) {
41-
if ($type->getTraitReflection() === null) {
42-
return true;
43-
}
44-
45-
return false;
46-
}
47-
if ($type->getTraitReflection() === null) {
48-
return false;
49-
}
50-
51-
return $this->getTraitReflection()->getName() === $type->getTraitReflection()->getName();
52-
}
53-
5425
public function changeBaseClass(ClassReflection $classReflection): StaticType
5526
{
56-
return new self($classReflection, $this->getSubtractedType(), $this->traitReflection);
27+
return new self($classReflection, $this->getSubtractedType());
5728
}
5829

5930
public function describe(VerbosityLevel $level): string
6031
{
61-
$callback = fn () => sprintf('$this(%s)', $this->getStaticObjectType()->describe($level));
62-
return $level->handle(
63-
$callback,
64-
$callback,
65-
$callback,
66-
function () use ($callback): string {
67-
$base = $callback();
68-
$trait = $this->getTraitReflection();
69-
if ($trait === null) {
70-
return $base;
71-
}
72-
73-
return sprintf('%s-trait-%s', $base, $trait->getDisplayName());
74-
},
75-
);
32+
return sprintf('$this(%s)', $this->getStaticObjectType()->describe($level));
7633
}
7734

7835
public function isSuperTypeOf(Type $type): TrinaryLogic
7936
{
8037
if ($type instanceof self) {
81-
if ($this->equals($type)) {
82-
return TrinaryLogic::createYes();
83-
}
84-
8538
return $this->getStaticObjectType()->isSuperTypeOf($type);
8639
}
8740

@@ -98,25 +51,12 @@ public function changeSubtractedType(?Type $subtractedType): Type
9851
{
9952
$type = parent::changeSubtractedType($subtractedType);
10053
if ($type instanceof parent) {
101-
return new self($type->getClassReflection(), $subtractedType, $this->traitReflection);
54+
return new self($type->getClassReflection(), $subtractedType);
10255
}
10356

10457
return $type;
10558
}
10659

107-
/**
108-
* @phpstan-assert-if-true !null $this->getTraitReflection()
109-
*/
110-
public function isInTrait(): bool
111-
{
112-
return $this->traitReflection !== null;
113-
}
114-
115-
public function getTraitReflection(): ?ClassReflection
116-
{
117-
return $this->traitReflection;
118-
}
119-
12060
public function traverse(callable $cb): Type
12161
{
12262
$subtractedType = $this->getSubtractedType() !== null ? $cb($this->getSubtractedType()) : null;
@@ -125,7 +65,6 @@ public function traverse(callable $cb): Type
12565
return new self(
12666
$this->getClassReflection(),
12767
$subtractedType,
128-
$this->traitReflection,
12968
);
13069
}
13170

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,6 @@ public function dataFileAsserts(): iterable
11971197
yield from $this->gatherAssertTypes(__DIR__ . '/data/callsite-cast-narrowing.php');
11981198
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8775.php');
11991199
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8752.php');
1200-
yield from $this->gatherAssertTypes(__DIR__ . '/data/trait-instance-of.php');
12011200
yield from $this->gatherAssertTypes(__DIR__ . '/data/mysql-stmt.php');
12021201
yield from $this->gatherAssertTypes(__DIR__ . '/data/list-shapes.php');
12031202
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7607.php');

tests/PHPStan/Analyser/data/trait-instance-of.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

tests/PHPStan/Rules/Classes/ExistingClassInInstanceOfRuleTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,4 @@ public function testBug7720(): void
7070
]);
7171
}
7272

73-
public function testTraitInstanceOf(): void
74-
{
75-
$this->analyse([__DIR__ . '/../../Analyser/data/trait-instance-of.php'], [
76-
[
77-
'Instanceof between $this(TraitInstanceOf\ATrait1Class) and trait TraitInstanceOf\Trait2 will always evaluate to false.',
78-
21,
79-
],
80-
]);
81-
}
82-
8373
}

tests/PHPStan/Rules/Classes/data/bug-3632.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,4 +2860,13 @@ public function testCannotCallOnGenericClassString(): void
28602860
]);
28612861
}
28622862

2863+
public function testBug8888(): void
2864+
{
2865+
$this->checkThisOnly = false;
2866+
$this->checkNullables = true;
2867+
$this->checkUnionTypes = true;
2868+
$this->checkExplicitMixed = true;
2869+
$this->analyse([__DIR__ . '/data/bug-8888.php'], []);
2870+
}
2871+
28632872
}

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,4 +839,9 @@ public function testBug8573(): void
839839
$this->analyse([__DIR__ . '/data/bug-8573.php'], []);
840840
}
841841

842+
public function testBug8879(): void
843+
{
844+
$this->analyse([__DIR__ . '/data/bug-8879.php'], []);
845+
}
846+
842847
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Bug8879;
4+
5+
trait GetThisTrait {
6+
/** @return $this */
7+
function getThis() {
8+
return $this;
9+
}
10+
}
11+
12+
/** @template T */
13+
final class A {
14+
use GetThisTrait;
15+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Bug8888;
4+
5+
trait SomeTrait
6+
{
7+
public function test(): void
8+
{
9+
if (method_exists($this, 'someTest')) {
10+
if ($this instanceof A) {
11+
return;
12+
}
13+
$this->someTest('foo');
14+
}
15+
return;
16+
}
17+
}
18+
19+
class A
20+
{
21+
use SomeTrait;
22+
23+
public function someTest(string $foo, string $bar): void {}
24+
}
25+
26+
class B
27+
{
28+
use SomeTrait;
29+
30+
public function someTest(string $foo): void {}
31+
}
32+
33+
class Test
34+
{
35+
public function test(): void
36+
{
37+
$a = new A();
38+
$a->test();
39+
$b = new B();
40+
$b->test();
41+
}
42+
}

0 commit comments

Comments
 (0)