Skip to content

Commit f59d525

Browse files
Fix issues identified by PHPStan
1 parent 984a0b8 commit f59d525

34 files changed

+334
-80
lines changed

src/ReflectionMapper.php

-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ private function mapUnionType(ReflectionUnionType $type, ReflectionFunction|Refl
129129
$types = [];
130130

131131
foreach ($type->getTypes() as $_type) {
132-
assert($_type instanceof ReflectionNamedType || $_type instanceof ReflectionIntersectionType);
133-
134132
if ($_type instanceof ReflectionNamedType) {
135133
$types[] = $this->mapNamedType($_type, $functionOrMethod);
136134

src/TypeName.php

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public static function fromQualifiedName(string $fullClassName): self
4444
return new self($namespaceName, $simpleName);
4545
}
4646

47+
/**
48+
* @phpstan-ignore missingType.generics
49+
*/
4750
public static function fromReflection(ReflectionClass $type): self
4851
{
4952
return new self(

src/type/CallableType.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,6 @@ public function allowsNull(): bool
8181
return $this->allowsNull;
8282
}
8383

84-
/**
85-
* @phpstan-assert-if-true CallableType $this
86-
*/
8784
public function isCallable(): bool
8885
{
8986
return true;
@@ -165,8 +162,11 @@ private function isClassCallback(SimpleType $type): bool
165162
[$className, $methodName] = $type->value();
166163
}
167164

168-
assert(isset($className) && is_string($className));
169-
assert(isset($methodName) && is_string($methodName));
165+
/** @phpstan-ignore isset.variable */
166+
assert(isset($className));
167+
168+
/** @phpstan-ignore isset.variable */
169+
assert(isset($methodName));
170170

171171
if (!class_exists($className)) {
172172
return false;

src/type/FalseType.php

-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ public function allowsNull(): bool
3535
return false;
3636
}
3737

38-
/**
39-
* @phpstan-assert-if-true FalseType $this
40-
*/
4138
public function isFalse(): bool
4239
{
4340
return true;

src/type/GenericObjectType.php

-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ public function allowsNull(): bool
4444
return $this->allowsNull;
4545
}
4646

47-
/**
48-
* @phpstan-assert-if-true GenericObjectType $this
49-
*/
5047
public function isGenericObject(): bool
5148
{
5249
return true;

src/type/IntersectionType.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\Type;
1111

12+
use function array_is_list;
1213
use function assert;
1314
use function count;
1415
use function implode;
@@ -31,6 +32,8 @@ public function __construct(Type ...$types)
3132
$this->ensureOnlyValidTypes(...$types);
3233
$this->ensureNoDuplicateTypes(...$types);
3334

35+
assert(array_is_list($types) && !empty($types));
36+
3437
$this->types = $types;
3538
}
3639

@@ -68,9 +71,6 @@ public function allowsNull(): bool
6871
return false;
6972
}
7073

71-
/**
72-
* @phpstan-assert-if-true IntersectionType $this
73-
*/
7474
public function isIntersection(): bool
7575
{
7676
return true;

src/type/IterableType.php

-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ public function allowsNull(): bool
6464
return $this->allowsNull;
6565
}
6666

67-
/**
68-
* @phpstan-assert-if-true IterableType $this
69-
*/
7067
public function isIterable(): bool
7168
{
7269
return true;

src/type/MixedType.php

-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ public function allowsNull(): bool
3737
return true;
3838
}
3939

40-
/**
41-
* @phpstan-assert-if-true MixedType $this
42-
*/
4340
public function isMixed(): bool
4441
{
4542
return true;

src/type/NeverType.php

-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ public function allowsNull(): bool
2929
return false;
3030
}
3131

32-
/**
33-
* @phpstan-assert-if-true NeverType $this
34-
*/
3532
public function isNever(): bool
3633
{
3734
return true;

src/type/NullType.php

-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ public function allowsNull(): bool
3737
return true;
3838
}
3939

40-
/**
41-
* @phpstan-assert-if-true NullType $this
42-
*/
4340
public function isNull(): bool
4441
{
4542
return true;

src/type/ObjectType.php

-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ public function className(): TypeName
6060
return $this->className;
6161
}
6262

63-
/**
64-
* @phpstan-assert-if-true ObjectType $this
65-
*/
6663
public function isObject(): bool
6764
{
6865
return true;

src/type/SimpleType.php

-3
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ public function value(): mixed
6969
return $this->value;
7070
}
7171

72-
/**
73-
* @phpstan-assert-if-true SimpleType $this
74-
*/
7572
public function isSimple(): bool
7673
{
7774
return true;

src/type/StaticType.php

-3
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ public function allowsNull(): bool
5757
return $this->allowsNull;
5858
}
5959

60-
/**
61-
* @phpstan-assert-if-true StaticType $this
62-
*/
6360
public function isStatic(): bool
6461
{
6562
return true;

src/type/TrueType.php

-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ public function allowsNull(): bool
3535
return false;
3636
}
3737

38-
/**
39-
* @phpstan-assert-if-true TrueType $this
40-
*/
4138
public function isTrue(): bool
4239
{
4340
return true;

src/type/UnionType.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace SebastianBergmann\Type;
1111

12+
use function array_is_list;
1213
use function assert;
1314
use function count;
1415
use function implode;
@@ -29,6 +30,8 @@ public function __construct(Type ...$types)
2930
$this->ensureMinimumOfTwoTypes(...$types);
3031
$this->ensureOnlyValidTypes(...$types);
3132

33+
assert(array_is_list($types) && !empty($types));
34+
3235
$this->types = $types;
3336
}
3437

@@ -72,6 +75,7 @@ public function name(): string
7275

7376
$name = implode('|', $types);
7477

78+
/** @phpstan-ignore empty.variable */
7579
assert(!empty($name));
7680

7781
return $name;
@@ -88,9 +92,6 @@ public function allowsNull(): bool
8892
return false;
8993
}
9094

91-
/**
92-
* @phpstan-assert-if-true UnionType $this
93-
*/
9495
public function isUnion(): bool
9596
{
9697
return true;

src/type/UnknownType.php

-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ public function allowsNull(): bool
3737
return true;
3838
}
3939

40-
/**
41-
* @phpstan-assert-if-true UnknownType $this
42-
*/
4340
public function isUnknown(): bool
4441
{
4542
return true;

src/type/VoidType.php

-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ public function allowsNull(): bool
2929
return false;
3030
}
3131

32-
/**
33-
* @phpstan-assert-if-true VoidType $this
34-
*/
3532
public function isVoid(): bool
3633
{
3734
return true;

tests/unit/ReflectionMapperTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
#[Small]
5656
final class ReflectionMapperTest extends TestCase
5757
{
58+
/**
59+
* @return non-empty-list<array{0: non-empty-string, 1: ReflectionFunction|ReflectionMethod}>
60+
*/
5861
public static function typeProvider(): array
5962
{
6063
return [

tests/unit/TypeNameTest.php

+12-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPUnit\Framework\Attributes\Small;
1414
use PHPUnit\Framework\TestCase;
1515
use ReflectionClass;
16+
use stdClass;
1617

1718
#[CoversClass(TypeName::class)]
1819
#[Small]
@@ -31,31 +32,31 @@ public function testFromReflection(): void
3132

3233
public function testFromQualifiedName(): void
3334
{
34-
$typeName = TypeName::fromQualifiedName('PHPUnit\\Framework\\MockObject\\TypeName');
35+
$typeName = TypeName::fromQualifiedName(self::class);
3536

3637
$this->assertTrue($typeName->isNamespaced());
37-
$this->assertSame('PHPUnit\\Framework\\MockObject', $typeName->namespaceName());
38-
$this->assertSame('PHPUnit\\Framework\\MockObject\\TypeName', $typeName->qualifiedName());
39-
$this->assertSame('TypeName', $typeName->simpleName());
38+
$this->assertSame('SebastianBergmann\\Type', $typeName->namespaceName());
39+
$this->assertSame('SebastianBergmann\\Type\\TypeNameTest', $typeName->qualifiedName());
40+
$this->assertSame('TypeNameTest', $typeName->simpleName());
4041
}
4142

4243
public function testFromQualifiedNameWithLeadingSeparator(): void
4344
{
44-
$typeName = TypeName::fromQualifiedName('\\Foo\\Bar');
45+
$typeName = TypeName::fromQualifiedName('\\' . self::class);
4546

4647
$this->assertTrue($typeName->isNamespaced());
47-
$this->assertSame('Foo', $typeName->namespaceName());
48-
$this->assertSame('Foo\\Bar', $typeName->qualifiedName());
49-
$this->assertSame('Bar', $typeName->simpleName());
48+
$this->assertSame('SebastianBergmann\\Type', $typeName->namespaceName());
49+
$this->assertSame('SebastianBergmann\\Type\\TypeNameTest', $typeName->qualifiedName());
50+
$this->assertSame('TypeNameTest', $typeName->simpleName());
5051
}
5152

5253
public function testFromQualifiedNameWithoutNamespace(): void
5354
{
54-
$typeName = TypeName::fromQualifiedName('Bar');
55+
$typeName = TypeName::fromQualifiedName(stdClass::class);
5556

5657
$this->assertFalse($typeName->isNamespaced());
5758
$this->assertNull($typeName->namespaceName());
58-
$this->assertSame('Bar', $typeName->qualifiedName());
59-
$this->assertSame('Bar', $typeName->simpleName());
59+
$this->assertSame(stdClass::class, $typeName->qualifiedName());
60+
$this->assertSame(stdClass::class, $typeName->simpleName());
6061
}
6162
}

tests/unit/type/FalseTypeTest.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#[Small]
2222
final class FalseTypeTest extends TestCase
2323
{
24+
/**
25+
* @return non-empty-list<array{0: Type}>
26+
*/
2427
public static function assignableTypes(): array
2528
{
2629
return [
@@ -29,6 +32,9 @@ public static function assignableTypes(): array
2932
];
3033
}
3134

35+
/**
36+
* @return non-empty-list<array{0: Type}>
37+
*/
3238
public static function notAssignableTypes(): array
3339
{
3440
return [
@@ -73,19 +79,45 @@ public function testCanBeQueriedForType(): void
7379
{
7480
$type = new FalseType;
7581

76-
$this->assertFalse($type->isCallable());
7782
$this->assertTrue($type->isFalse());
83+
84+
/** @phpstan-ignore method.impossibleType */
85+
$this->assertFalse($type->isCallable());
86+
87+
/** @phpstan-ignore method.impossibleType */
7888
$this->assertFalse($type->isGenericObject());
89+
90+
/** @phpstan-ignore method.impossibleType */
7991
$this->assertFalse($type->isIntersection());
92+
93+
/** @phpstan-ignore method.impossibleType */
8094
$this->assertFalse($type->isIterable());
95+
96+
/** @phpstan-ignore method.impossibleType */
8197
$this->assertFalse($type->isMixed());
98+
99+
/** @phpstan-ignore method.impossibleType */
82100
$this->assertFalse($type->isNever());
101+
102+
/** @phpstan-ignore method.impossibleType */
83103
$this->assertFalse($type->isNull());
104+
105+
/** @phpstan-ignore method.impossibleType */
84106
$this->assertFalse($type->isObject());
107+
108+
/** @phpstan-ignore method.impossibleType */
85109
$this->assertFalse($type->isSimple());
110+
111+
/** @phpstan-ignore method.impossibleType */
86112
$this->assertFalse($type->isStatic());
113+
114+
/** @phpstan-ignore method.impossibleType */
87115
$this->assertFalse($type->isUnion());
116+
117+
/** @phpstan-ignore method.impossibleType */
88118
$this->assertFalse($type->isUnknown());
119+
120+
/** @phpstan-ignore method.impossibleType */
89121
$this->assertFalse($type->isVoid());
90122
}
91123
}

tests/unit/type/GenericObjectTypeTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ public function testNonObjectCannotBeAssignedToGenericObject(): void
7878

7979
public function testCanBeQueriedForType(): void
8080
{
81+
$this->assertTrue($this->type->isGenericObject());
8182
$this->assertFalse($this->type->isCallable());
8283
$this->assertFalse($this->type->isFalse());
83-
$this->assertTrue($this->type->isGenericObject());
8484
$this->assertFalse($this->type->isIntersection());
8585
$this->assertFalse($this->type->isIterable());
8686
$this->assertFalse($this->type->isMixed());

tests/unit/type/IntersectionTypeTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ final class IntersectionTypeTest extends TestCase
2828
{
2929
private IntersectionType $type;
3030

31+
/**
32+
* @return non-empty-list<array{0: bool, 1: Type, 2: IntersectionType}>
33+
*/
3134
public static function assignableTypes(): array
3235
{
3336
return [
@@ -70,10 +73,10 @@ public function testTypesCanBeQueried(): void
7073

7174
public function testCanBeQueriedForType(): void
7275
{
76+
$this->assertTrue($this->type->isIntersection());
7377
$this->assertFalse($this->type->isCallable());
7478
$this->assertFalse($this->type->isFalse());
7579
$this->assertFalse($this->type->isGenericObject());
76-
$this->assertTrue($this->type->isIntersection());
7780
$this->assertFalse($this->type->isIterable());
7881
$this->assertFalse($this->type->isMixed());
7982
$this->assertFalse($this->type->isNever());

tests/unit/type/IterableTypeTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ public function testSomethingThatIsNotIterableCannotBeAssignedToIterable(): void
8888

8989
public function testCanBeQueriedForType(): void
9090
{
91+
$this->assertTrue($this->type->isIterable());
9192
$this->assertFalse($this->type->isCallable());
9293
$this->assertFalse($this->type->isFalse());
9394
$this->assertFalse($this->type->isGenericObject());
9495
$this->assertFalse($this->type->isIntersection());
95-
$this->assertTrue($this->type->isIterable());
9696
$this->assertFalse($this->type->isMixed());
9797
$this->assertFalse($this->type->isNever());
9898
$this->assertFalse($this->type->isNull());

0 commit comments

Comments
 (0)