Skip to content

Commit

Permalink
Fix default method parameter value mentioning ::class constant
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 17, 2021
1 parent 2b30830 commit 431905c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"nette/utils": "^3.1.3",
"nikic/php-parser": "4.10.4",
"ondram/ci-detector": "^3.4.0",
"ondrejmirtes/better-reflection": "4.3.51",
"ondrejmirtes/better-reflection": "4.3.52",
"phpdocumentor/reflection-docblock": "4.3.4",
"phpstan/php-8-stubs": "^0.1.13",
"phpstan/phpdoc-parser": "^0.4.9",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace PHPStan\Analyser;

use Bug4713\Service;
use PHPStan\Broker\Broker;
use PHPStan\File\FileHelper;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
use PHPStan\Type\Constant\ConstantStringType;
use const PHP_VERSION_ID;
use function array_reverse;

Expand Down Expand Up @@ -336,6 +339,19 @@ public function testBug1843(): void
$this->assertCount(0, $errors);
}

public function testBug4713(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-4713.php');
$this->assertCount(0, $errors);

$reflectionProvider = $this->createBroker();
$class = $reflectionProvider->getClass(Service::class);
$parameter = ParametersAcceptorSelector::selectSingle($class->getNativeMethod('createInstance')->getVariants())->getParameters()[0];
$defaultValue = $parameter->getDefaultValue();
$this->assertInstanceOf(ConstantStringType::class, $defaultValue);
$this->assertSame(Service::class, $defaultValue->getValue());
}

/**
* @param string $file
* @return \PHPStan\Analyser\Error[]
Expand Down
15 changes: 15 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4713.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Bug4713;

class Service
{
public static function createInstance(string $class = self::class): Service
{
return new $class();
}
}

function (): void {
$service = Service::createInstance();
};

0 comments on commit 431905c

Please sign in to comment.