Skip to content

Commit 8babba3

Browse files
committed
Fix default trait method parameter mentioning constant from the using class
1 parent 431905c commit 8babba3

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-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.52",
26+
"ondrejmirtes/better-reflection": "4.3.53",
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

+22
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace PHPStan\Analyser;
44

5+
use Bug4288\MyClass;
56
use Bug4713\Service;
67
use PHPStan\Broker\Broker;
78
use PHPStan\File\FileHelper;
89
use PHPStan\Reflection\ParametersAcceptorSelector;
910
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
11+
use PHPStan\Type\Constant\ConstantIntegerType;
1012
use PHPStan\Type\Constant\ConstantStringType;
1113
use const PHP_VERSION_ID;
1214
use function array_reverse;
@@ -352,6 +354,26 @@ public function testBug4713(): void
352354
$this->assertSame(Service::class, $defaultValue->getValue());
353355
}
354356

357+
public function testBug4288(): void
358+
{
359+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-4288.php');
360+
$this->assertCount(0, $errors);
361+
362+
$reflectionProvider = $this->createBroker();
363+
$class = $reflectionProvider->getClass(MyClass::class);
364+
$parameter = ParametersAcceptorSelector::selectSingle($class->getNativeMethod('paginate')->getVariants())->getParameters()[0];
365+
$defaultValue = $parameter->getDefaultValue();
366+
$this->assertInstanceOf(ConstantIntegerType::class, $defaultValue);
367+
$this->assertSame(10, $defaultValue->getValue());
368+
369+
$nativeProperty = $class->getNativeReflection()->getProperty('test');
370+
if (!method_exists($nativeProperty, 'getDefaultValue')) {
371+
return;
372+
}
373+
374+
$this->assertSame(10, $nativeProperty->getDefaultValue());
375+
}
376+
355377
/**
356378
* @param string $file
357379
* @return \PHPStan\Analyser\Error[]
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bug4288;
4+
5+
trait PaginationTrait
6+
{
7+
8+
/** @var int */
9+
private $test = self::DEFAULT_SIZE;
10+
11+
private function paginate(int $size = self::DEFAULT_SIZE): void
12+
{
13+
echo $size;
14+
}
15+
}
16+
17+
class MyClass
18+
{
19+
use PaginationTrait;
20+
21+
const DEFAULT_SIZE = 10;
22+
23+
public function test(): void
24+
{
25+
$this->paginate();
26+
}
27+
}
28+

0 commit comments

Comments
 (0)