Skip to content

Commit 431905c

Browse files
committed
Fix default method parameter value mentioning ::class constant
1 parent 2b30830 commit 431905c

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"nette/utils": "^3.1.3",
2424
"nikic/php-parser": "4.10.4",
2525
"ondram/ci-detector": "^3.4.0",
26-
"ondrejmirtes/better-reflection": "4.3.51",
26+
"ondrejmirtes/better-reflection": "4.3.52",
2727
"phpdocumentor/reflection-docblock": "4.3.4",
2828
"phpstan/php-8-stubs": "^0.1.13",
2929
"phpstan/phpdoc-parser": "^0.4.9",

composer.lock

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace PHPStan\Analyser;
44

5+
use Bug4713\Service;
56
use PHPStan\Broker\Broker;
67
use PHPStan\File\FileHelper;
8+
use PHPStan\Reflection\ParametersAcceptorSelector;
79
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
10+
use PHPStan\Type\Constant\ConstantStringType;
811
use const PHP_VERSION_ID;
912
use function array_reverse;
1013

@@ -336,6 +339,19 @@ public function testBug1843(): void
336339
$this->assertCount(0, $errors);
337340
}
338341

342+
public function testBug4713(): void
343+
{
344+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-4713.php');
345+
$this->assertCount(0, $errors);
346+
347+
$reflectionProvider = $this->createBroker();
348+
$class = $reflectionProvider->getClass(Service::class);
349+
$parameter = ParametersAcceptorSelector::selectSingle($class->getNativeMethod('createInstance')->getVariants())->getParameters()[0];
350+
$defaultValue = $parameter->getDefaultValue();
351+
$this->assertInstanceOf(ConstantStringType::class, $defaultValue);
352+
$this->assertSame(Service::class, $defaultValue->getValue());
353+
}
354+
339355
/**
340356
* @param string $file
341357
* @return \PHPStan\Analyser\Error[]
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Bug4713;
4+
5+
class Service
6+
{
7+
public static function createInstance(string $class = self::class): Service
8+
{
9+
return new $class();
10+
}
11+
}
12+
13+
function (): void {
14+
$service = Service::createInstance();
15+
};

0 commit comments

Comments
 (0)