From 0d0ea452ec3ee8456ff858349014d2aabefd3216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Sat, 29 Nov 2025 20:26:23 +0400 Subject: [PATCH 1/2] Analyze tests using PHPStan --- phpstan.neon | 18 ++++++++++++++++++ tests/benchmark/ContextFactoryBench.php | 6 +++--- tests/benchmark/TypeResolverBench.php | 2 +- .../benchmark/TypeResolverWithContextBench.php | 4 ++-- tests/unit/NumericResolverTest.php | 8 ++++++-- tests/unit/TypeResolverTest.php | 8 +++++--- tests/unit/Types/ContextFactoryTest.php | 6 +++--- 7 files changed, 38 insertions(+), 14 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index e53acac5..1808cd5b 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -7,6 +7,7 @@ parameters: - tests/benchmark/Assets/* paths: - src + - tests ignoreErrors: - 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: message: "#^Parameter \\#2 \\$quoteAwareConstExprString of class PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser constructor expects bool, PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser given\\.$#" count: 1 path: src/TypeResolver.php + + # We are intentionally adding invalid parameters here + - + message: "#^Parameter \\#2 \\$typeClassName of method phpDocumentor\\\\Reflection\\\\TypeResolver\\:\\:addKeyword\\(\\) expects class\\-string\\\\, string given\\.$#" + count: 2 + path: tests/unit/TypeResolverTest.php + + # We are intentionally adding invalid parameters here + - + message: "#^Parameter \\#1 \\$types of class phpDocumentor\\\\Reflection\\\\Types\\\\Compound constructor expects array\\\\, array\\ given\\.$#" + count: 1 + path: tests/unit/Types/CompoundTest.php + + - + message: "#^Parameter \\#1 \\$argument of class ReflectionClass constructor expects class\\-string\\\\|Foo\\\\Bar\\, string given\\.$#" + count: 1 + path: tests/unit/Types/ContextFactoryTest.php diff --git a/tests/benchmark/ContextFactoryBench.php b/tests/benchmark/ContextFactoryBench.php index b19cb8e3..3ab4af1f 100644 --- a/tests/benchmark/ContextFactoryBench.php +++ b/tests/benchmark/ContextFactoryBench.php @@ -14,15 +14,15 @@ final class ContextFactoryBench { private string $source; - public function setup() + public function setup(): void { - $this->source = file_get_contents(__DIR__ . '/Assets/mpdf.php'); + $this->source = (string) file_get_contents(__DIR__ . '/Assets/mpdf.php'); } /** * @Warmup(1) */ - public function benchCreateContextForNamespace() + public function benchCreateContextForNamespace(): void { $factory = new ContextFactory(); $factory->createForNamespace( diff --git a/tests/benchmark/TypeResolverBench.php b/tests/benchmark/TypeResolverBench.php index 5f823d5d..a8258332 100644 --- a/tests/benchmark/TypeResolverBench.php +++ b/tests/benchmark/TypeResolverBench.php @@ -15,7 +15,7 @@ class TypeResolverBench { private TypeResolver $typeResolver; - public function setup() + public function setup(): void { $this->typeResolver = new TypeResolver(); } diff --git a/tests/benchmark/TypeResolverWithContextBench.php b/tests/benchmark/TypeResolverWithContextBench.php index ee1163f7..4ce7d755 100644 --- a/tests/benchmark/TypeResolverWithContextBench.php +++ b/tests/benchmark/TypeResolverWithContextBench.php @@ -24,10 +24,10 @@ final class TypeResolverWithContextBench */ private $typeResolver; - public function setup() + public function setup(): void { $factory = new ContextFactory(); - $this->context = $factory->createForNamespace('mpdf', file_get_contents(__DIR__ . '/Assets/mpdf.php')); + $this->context = $factory->createForNamespace('mpdf', (string) file_get_contents(__DIR__ . '/Assets/mpdf.php')); $this->typeResolver = new TypeResolver(); } diff --git a/tests/unit/NumericResolverTest.php b/tests/unit/NumericResolverTest.php index 6a8c935e..df4ee0a7 100644 --- a/tests/unit/NumericResolverTest.php +++ b/tests/unit/NumericResolverTest.php @@ -15,6 +15,7 @@ use phpDocumentor\Reflection\PseudoTypes\Numeric_; use phpDocumentor\Reflection\PseudoTypes\NumericString; +use phpDocumentor\Reflection\Types\Compound; use phpDocumentor\Reflection\Types\Context; use phpDocumentor\Reflection\Types\String_; use PHPUnit\Framework\TestCase; @@ -40,7 +41,10 @@ public function testResolvingIntRange(): void $this->assertInstanceOf(Numeric_::class, $resolvedType); $this->assertSame('numeric', (string) $resolvedType); - $this->assertSame(false, $resolvedType->underlyingType()->contains(new String_())); - $this->assertSame(true, $resolvedType->underlyingType()->contains(new NumericString())); + + $underlyingType = $resolvedType->underlyingType(); + $this->assertInstanceOf(Compound::class, $underlyingType); + $this->assertFalse($underlyingType->contains(new String_())); + $this->assertTrue($underlyingType->contains(new NumericString())); } } diff --git a/tests/unit/TypeResolverTest.php b/tests/unit/TypeResolverTest.php index 4cff6369..4e523ab0 100644 --- a/tests/unit/TypeResolverTest.php +++ b/tests/unit/TypeResolverTest.php @@ -101,6 +101,8 @@ class TypeResolverTest extends TestCase * @covers :: * * @dataProvider provideKeywords + * + * @param class-string $expectedClass */ public function testResolvingKeywords(string $keyword, string $expectedClass): void { @@ -291,9 +293,8 @@ public function testResolvingNestedTypedArrays(): void $resolvedType = $fixture->resolve('string[][]', new Context('')); - $childValueType = $resolvedType->getValueType(); - $this->assertInstanceOf(Array_::class, $resolvedType); + $childValueType = $resolvedType->getValueType(); $this->assertSame('string[][]', (string) $resolvedType); $this->assertInstanceOf(Compound::class, $resolvedType->getKeyType()); @@ -414,11 +415,12 @@ public function testResolvingMixedCompoundTypes(): void $resolvedType = $firstType->getValueType(); + $this->assertInstanceOf(Intersection::class, $resolvedType); $firstSubType = $resolvedType->get(0); $secondSubType = $resolvedType->get(1); $this->assertInstanceOf(Object_::class, $firstSubType); - $this->assertInstanceOf(Fqsen::class, $secondSubType->getFqsen()); + $this->assertInstanceOf(Fqsen::class, $firstSubType->getFqsen()); $this->assertInstanceOf(Object_::class, $secondSubType); $this->assertInstanceOf(Fqsen::class, $secondSubType->getFqsen()); } diff --git a/tests/unit/Types/ContextFactoryTest.php b/tests/unit/Types/ContextFactoryTest.php index 9aa97aeb..b822f44e 100644 --- a/tests/unit/Types/ContextFactoryTest.php +++ b/tests/unit/Types/ContextFactoryTest.php @@ -61,7 +61,7 @@ public function testReadsAliasesFromClassReflection() : void public function testReadsNamespaceFromProvidedNamespaceAndContent() : void { $fixture = new ContextFactory(); - $context = $fixture->createForNamespace(__NAMESPACE__, file_get_contents(__FILE__)); + $context = $fixture->createForNamespace(__NAMESPACE__, (string) file_get_contents(__FILE__)); $this->assertSame(__NAMESPACE__, $context->getNamespace()); } @@ -73,7 +73,7 @@ public function testReadsNamespaceFromProvidedNamespaceAndContent() : void public function testReadsAliasesFromProvidedNamespaceAndContent() : void { $fixture = new ContextFactory(); - $context = $fixture->createForNamespace(__NAMESPACE__, file_get_contents(__FILE__)); + $context = $fixture->createForNamespace(__NAMESPACE__, (string) file_get_contents(__FILE__)); $this->assertNamespaceAliasesFrom($context); } @@ -198,7 +198,7 @@ class Bar $this->assertSame([], $context->getNamespaceAliases()); } - public function assertNamespaceAliasesFrom(Context $context) + public function assertNamespaceAliasesFrom(Context $context): void { $expected = [ 'm' => m::class, From 588e1026c6fd28484265d5e76397560d1b216588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D1=80=D0=BA=D0=BE=D0=B2?= Date: Sat, 29 Nov 2025 20:29:49 +0400 Subject: [PATCH 2/2] Fix CS --- tests/unit/TypeResolverTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/TypeResolverTest.php b/tests/unit/TypeResolverTest.php index 4e523ab0..f2dc92c4 100644 --- a/tests/unit/TypeResolverTest.php +++ b/tests/unit/TypeResolverTest.php @@ -95,14 +95,14 @@ class TypeResolverTest extends TestCase * @uses \phpDocumentor\Reflection\Types\Array_ * @uses \phpDocumentor\Reflection\Types\Object_ * + * @param class-string $expectedClass + * * @covers ::__construct * @covers ::resolve * @covers ::createType * @covers :: * * @dataProvider provideKeywords - * - * @param class-string $expectedClass */ public function testResolvingKeywords(string $keyword, string $expectedClass): void {