Skip to content

Commit

Permalink
Fix default trait method parameter mentioning constant from the using…
Browse files Browse the repository at this point in the history
… class
  • Loading branch information
ondrejmirtes committed Mar 17, 2021
1 parent 431905c commit 8babba3
Show file tree
Hide file tree
Showing 4 changed files with 58 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.52",
"ondrejmirtes/better-reflection": "4.3.53",
"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.

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

namespace PHPStan\Analyser;

use Bug4288\MyClass;
use Bug4713\Service;
use PHPStan\Broker\Broker;
use PHPStan\File\FileHelper;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use const PHP_VERSION_ID;
use function array_reverse;
Expand Down Expand Up @@ -352,6 +354,26 @@ public function testBug4713(): void
$this->assertSame(Service::class, $defaultValue->getValue());
}

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

$reflectionProvider = $this->createBroker();
$class = $reflectionProvider->getClass(MyClass::class);
$parameter = ParametersAcceptorSelector::selectSingle($class->getNativeMethod('paginate')->getVariants())->getParameters()[0];
$defaultValue = $parameter->getDefaultValue();
$this->assertInstanceOf(ConstantIntegerType::class, $defaultValue);
$this->assertSame(10, $defaultValue->getValue());

$nativeProperty = $class->getNativeReflection()->getProperty('test');
if (!method_exists($nativeProperty, 'getDefaultValue')) {
return;
}

$this->assertSame(10, $nativeProperty->getDefaultValue());
}

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

namespace Bug4288;

trait PaginationTrait
{

/** @var int */
private $test = self::DEFAULT_SIZE;

private function paginate(int $size = self::DEFAULT_SIZE): void
{
echo $size;
}
}

class MyClass
{
use PaginationTrait;

const DEFAULT_SIZE = 10;

public function test(): void
{
$this->paginate();
}
}

0 comments on commit 8babba3

Please sign in to comment.