Skip to content

Commit 0d0ea45

Browse files
committed
Analyze tests using PHPStan
1 parent 9049757 commit 0d0ea45

File tree

7 files changed

+38
-14
lines changed

7 files changed

+38
-14
lines changed

phpstan.neon

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ parameters:
77
- tests/benchmark/Assets/*
88
paths:
99
- src
10+
- tests
1011
ignoreErrors:
1112
-
1213
message: "#^Parameter \\#1 \\$constExprParser of class PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser constructor expects PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser\\|null, PHPStan\\\\PhpDocParser\\\\ParserConfig given\\.$#"
@@ -24,3 +25,20 @@ parameters:
2425
message: "#^Parameter \\#2 \\$quoteAwareConstExprString of class PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser constructor expects bool, PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser given\\.$#"
2526
count: 1
2627
path: src/TypeResolver.php
28+
29+
# We are intentionally adding invalid parameters here
30+
-
31+
message: "#^Parameter \\#2 \\$typeClassName of method phpDocumentor\\\\Reflection\\\\TypeResolver\\:\\:addKeyword\\(\\) expects class\\-string\\<phpDocumentor\\\\Reflection\\\\Type\\>\\, string given\\.$#"
32+
count: 2
33+
path: tests/unit/TypeResolverTest.php
34+
35+
# We are intentionally adding invalid parameters here
36+
-
37+
message: "#^Parameter \\#1 \\$types of class phpDocumentor\\\\Reflection\\\\Types\\\\Compound constructor expects array\\<phpDocumentor\\\\Reflection\\\\Type\\>\\, array\\<int\\, string\\> given\\.$#"
38+
count: 1
39+
path: tests/unit/Types/CompoundTest.php
40+
41+
-
42+
message: "#^Parameter \\#1 \\$argument of class ReflectionClass constructor expects class\\-string\\<Foo\\\\Bar\\>\\|Foo\\\\Bar\\, string given\\.$#"
43+
count: 1
44+
path: tests/unit/Types/ContextFactoryTest.php

tests/benchmark/ContextFactoryBench.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ final class ContextFactoryBench
1414
{
1515
private string $source;
1616

17-
public function setup()
17+
public function setup(): void
1818
{
19-
$this->source = file_get_contents(__DIR__ . '/Assets/mpdf.php');
19+
$this->source = (string) file_get_contents(__DIR__ . '/Assets/mpdf.php');
2020
}
2121

2222
/**
2323
* @Warmup(1)
2424
*/
25-
public function benchCreateContextForNamespace()
25+
public function benchCreateContextForNamespace(): void
2626
{
2727
$factory = new ContextFactory();
2828
$factory->createForNamespace(

tests/benchmark/TypeResolverBench.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TypeResolverBench
1515
{
1616
private TypeResolver $typeResolver;
1717

18-
public function setup()
18+
public function setup(): void
1919
{
2020
$this->typeResolver = new TypeResolver();
2121
}

tests/benchmark/TypeResolverWithContextBench.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ final class TypeResolverWithContextBench
2424
*/
2525
private $typeResolver;
2626

27-
public function setup()
27+
public function setup(): void
2828
{
2929
$factory = new ContextFactory();
30-
$this->context = $factory->createForNamespace('mpdf', file_get_contents(__DIR__ . '/Assets/mpdf.php'));
30+
$this->context = $factory->createForNamespace('mpdf', (string) file_get_contents(__DIR__ . '/Assets/mpdf.php'));
3131
$this->typeResolver = new TypeResolver();
3232
}
3333

tests/unit/NumericResolverTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use phpDocumentor\Reflection\PseudoTypes\Numeric_;
1717
use phpDocumentor\Reflection\PseudoTypes\NumericString;
18+
use phpDocumentor\Reflection\Types\Compound;
1819
use phpDocumentor\Reflection\Types\Context;
1920
use phpDocumentor\Reflection\Types\String_;
2021
use PHPUnit\Framework\TestCase;
@@ -40,7 +41,10 @@ public function testResolvingIntRange(): void
4041

4142
$this->assertInstanceOf(Numeric_::class, $resolvedType);
4243
$this->assertSame('numeric', (string) $resolvedType);
43-
$this->assertSame(false, $resolvedType->underlyingType()->contains(new String_()));
44-
$this->assertSame(true, $resolvedType->underlyingType()->contains(new NumericString()));
44+
45+
$underlyingType = $resolvedType->underlyingType();
46+
$this->assertInstanceOf(Compound::class, $underlyingType);
47+
$this->assertFalse($underlyingType->contains(new String_()));
48+
$this->assertTrue($underlyingType->contains(new NumericString()));
4549
}
4650
}

tests/unit/TypeResolverTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class TypeResolverTest extends TestCase
101101
* @covers ::<private>
102102
*
103103
* @dataProvider provideKeywords
104+
*
105+
* @param class-string $expectedClass
104106
*/
105107
public function testResolvingKeywords(string $keyword, string $expectedClass): void
106108
{
@@ -291,9 +293,8 @@ public function testResolvingNestedTypedArrays(): void
291293

292294
$resolvedType = $fixture->resolve('string[][]', new Context(''));
293295

294-
$childValueType = $resolvedType->getValueType();
295-
296296
$this->assertInstanceOf(Array_::class, $resolvedType);
297+
$childValueType = $resolvedType->getValueType();
297298

298299
$this->assertSame('string[][]', (string) $resolvedType);
299300
$this->assertInstanceOf(Compound::class, $resolvedType->getKeyType());
@@ -414,11 +415,12 @@ public function testResolvingMixedCompoundTypes(): void
414415

415416
$resolvedType = $firstType->getValueType();
416417

418+
$this->assertInstanceOf(Intersection::class, $resolvedType);
417419
$firstSubType = $resolvedType->get(0);
418420
$secondSubType = $resolvedType->get(1);
419421

420422
$this->assertInstanceOf(Object_::class, $firstSubType);
421-
$this->assertInstanceOf(Fqsen::class, $secondSubType->getFqsen());
423+
$this->assertInstanceOf(Fqsen::class, $firstSubType->getFqsen());
422424
$this->assertInstanceOf(Object_::class, $secondSubType);
423425
$this->assertInstanceOf(Fqsen::class, $secondSubType->getFqsen());
424426
}

tests/unit/Types/ContextFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testReadsAliasesFromClassReflection() : void
6161
public function testReadsNamespaceFromProvidedNamespaceAndContent() : void
6262
{
6363
$fixture = new ContextFactory();
64-
$context = $fixture->createForNamespace(__NAMESPACE__, file_get_contents(__FILE__));
64+
$context = $fixture->createForNamespace(__NAMESPACE__, (string) file_get_contents(__FILE__));
6565

6666
$this->assertSame(__NAMESPACE__, $context->getNamespace());
6767
}
@@ -73,7 +73,7 @@ public function testReadsNamespaceFromProvidedNamespaceAndContent() : void
7373
public function testReadsAliasesFromProvidedNamespaceAndContent() : void
7474
{
7575
$fixture = new ContextFactory();
76-
$context = $fixture->createForNamespace(__NAMESPACE__, file_get_contents(__FILE__));
76+
$context = $fixture->createForNamespace(__NAMESPACE__, (string) file_get_contents(__FILE__));
7777

7878
$this->assertNamespaceAliasesFrom($context);
7979
}
@@ -198,7 +198,7 @@ class Bar
198198
$this->assertSame([], $context->getNamespaceAliases());
199199
}
200200

201-
public function assertNamespaceAliasesFrom(Context $context)
201+
public function assertNamespaceAliasesFrom(Context $context): void
202202
{
203203
$expected = [
204204
'm' => m::class,

0 commit comments

Comments
 (0)