Skip to content

Commit 1a102fe

Browse files
committed
Support for new in initializers
1 parent 9488d34 commit 1a102fe

File tree

10 files changed

+109
-9
lines changed

10 files changed

+109
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"nette/utils": "^3.1.3",
2424
"nikic/php-parser": "4.13.1",
2525
"ondram/ci-detector": "^3.4.0",
26-
"ondrejmirtes/better-reflection": "4.3.79",
26+
"ondrejmirtes/better-reflection": "4.3.80",
2727
"phpstan/php-8-stubs": "^0.1.23",
2828
"phpstan/phpdoc-parser": "^1.2.0",
2929
"react/child-process": "^0.6.4",

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Type/ConstantTypeHelper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public static function getTypeFromValue($value): Type
3939
$arrayBuilder->setOffsetValueType(self::getTypeFromValue($k), self::getTypeFromValue($v));
4040
}
4141
return $arrayBuilder->getArray();
42+
} elseif (is_object($value)) {
43+
return new ObjectType(get_class($value));
4244
}
4345

4446
return new MixedType();

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,15 @@ public function dataFileAsserts(): iterable
542542

543543
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2760.php');
544544

545+
if (PHP_VERSION_ID >= 80100 || self::$useStaticReflectionProvider) {
546+
yield from $this->gatherAssertTypes(__DIR__ . '/data/new-in-initializers.php');
547+
548+
if (PHP_VERSION_ID >= 80100) {
549+
define('TEST_OBJECT_CONSTANT', new \stdClass());
550+
yield from $this->gatherAssertTypes(__DIR__ . '/data/new-in-initializers-runtime.php');
551+
}
552+
}
553+
545554
yield from $this->gatherAssertTypes(__DIR__ . '/data/filesystem-functions.php');
546555
}
547556

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php // lint >= 8.1
2+
3+
namespace NewInInitializers;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
assertType('stdClass', TEST_OBJECT_CONSTANT);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php // lint >= 8.1
2+
3+
namespace NewInInitializers;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
10+
/**
11+
* @template T of object
12+
* @param T $test
13+
* @return T
14+
*/
15+
public function doFoo(
16+
object $test = new \stdClass()
17+
): object
18+
{
19+
return $test;
20+
}
21+
22+
#[\Test(new \stdClass())]
23+
public function doBar()
24+
{
25+
assertType(\stdClass::class, $this->doFoo());
26+
assertType('$this(NewInInitializers\Foo)', $this->doFoo($this));
27+
assertType(Bar::class, $this->doFoo(new Bar()));
28+
}
29+
30+
}
31+
32+
class Bar extends Foo
33+
{
34+
35+
public function doBar()
36+
{
37+
38+
}
39+
40+
public function doBaz()
41+
{
42+
static $o = new \stdClass();
43+
assertType('mixed', $o);
44+
}
45+
46+
}

tests/PHPStan/Reflection/BetterReflection/SourceLocator/OptimizedSingleFileSourceLocatorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ public function dataConst(): array
114114
'const_with_dir_const',
115115
str_replace('\\', '/', __DIR__ . '/data'),
116116
],
117+
[
118+
'OPTIMIZED_SFSL_OBJECT_CONSTANT',
119+
new \stdClass(),
120+
],
117121
];
118122
}
119123

@@ -130,7 +134,7 @@ public function testConst(string $constantName, $value): void
130134
$constantReflector = new ConstantReflector($locator, $classReflector);
131135
$constant = $constantReflector->reflect($constantName);
132136
$this->assertSame($constantName, $constant->getName());
133-
$this->assertSame($value, $constant->getValue());
137+
$this->assertEquals($value, $constant->getValue());
134138
}
135139

136140
public function dataConstUnknown(): array

tests/PHPStan/Reflection/BetterReflection/SourceLocator/data/const.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@
1010
define('TEST_VARIABLE', $foo);
1111

1212
define('const_with_dir_const', __DIR__);
13+
14+
define('OPTIMIZED_SFSL_OBJECT_CONSTANT', new \stdClass());

tests/PHPStan/Rules/Methods/IncompatibleDefaultParameterTypeRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,18 @@ public function testBug2573(): void
4545
$this->analyse([__DIR__ . '/data/bug-2573.php'], []);
4646
}
4747

48+
public function testNewInInitializers(): void
49+
{
50+
if (PHP_VERSION_ID < 80100 && !self::$useStaticReflectionProvider) {
51+
$this->markTestSkipped('Test requires PHP 8.0.');
52+
}
53+
54+
$this->analyse([__DIR__ . '/data/new-in-initializers.php'], [
55+
[
56+
'Default value of the parameter #1 $i (stdClass) of method MethodNewInInitializers\Foo::doFoo() is incompatible with type int.',
57+
11,
58+
],
59+
]);
60+
}
61+
4862
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php // lint >= 8.1
2+
3+
namespace MethodNewInInitializers;
4+
5+
class Foo
6+
{
7+
8+
/**
9+
* @param int $i
10+
*/
11+
public function doFoo($i = new \stdClass(), object $o = new \stdClass())
12+
{
13+
14+
}
15+
16+
}

0 commit comments

Comments
 (0)