Skip to content

Commit fa9ee15

Browse files
committed
Returning object from new $class is too aggressive for minor version, let's return mixed~string for now
1 parent 70756b0 commit fa9ee15

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

src/Analyser/MutatingScope.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4641,7 +4641,7 @@ private function getTypeToInstantiateForNew(Type $type): Type
46414641
foreach ($type->getTypes() as $innerType) {
46424642
$decidedType = $decideType($innerType);
46434643
if ($decidedType === null) {
4644-
return new ObjectWithoutClassType();
4644+
return new MixedType(false, new StringType());
46454645
}
46464646

46474647
$types[] = $decidedType;
@@ -4652,7 +4652,7 @@ private function getTypeToInstantiateForNew(Type $type): Type
46524652

46534653
$decidedType = $decideType($type);
46544654
if ($decidedType === null) {
4655-
return new ObjectWithoutClassType();
4655+
return new MixedType(false, new StringType());
46564656
}
46574657

46584658
return $decidedType;

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ public function dataDeductedTypes(): array
17121712
'$loremObjectLiteral',
17131713
],
17141714
[
1715-
'object',
1715+
'mixed~string',
17161716
'$mixedObjectLiteral',
17171717
],
17181718
[

tests/PHPStan/Analyser/data/bug-4579.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
function (string $class): void {
88
$foo = new $class();
9+
assertType('mixed~string', $foo);
910
if (method_exists($foo, 'doFoo')) {
1011
assertType('object&hasMethod(doFoo)', $foo);
1112
}

tests/PHPStan/Analyser/data/generic-class-string.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static function f(): int {
1515
* @param mixed $a
1616
*/
1717
function testMixed($a) {
18-
assertType('object', new $a());
18+
assertType('mixed~string', new $a());
1919

2020
if (is_subclass_of($a, 'DateTimeInterface')) {
2121
assertType('class-string<DateTimeInterface>|DateTimeInterface', $a);
@@ -36,7 +36,7 @@ function testMixed($a) {
3636
* @param object $a
3737
*/
3838
function testObject($a) {
39-
assertType('object', new $a());
39+
assertType('mixed~string', new $a());
4040

4141
if (is_subclass_of($a, 'DateTimeInterface')) {
4242
assertType('DateTimeInterface', $a);
@@ -47,7 +47,7 @@ function testObject($a) {
4747
* @param string $a
4848
*/
4949
function testString($a) {
50-
assertType('object', new $a());
50+
assertType('mixed~string', new $a());
5151

5252
if (is_subclass_of($a, 'DateTimeInterface')) {
5353
assertType('class-string<DateTimeInterface>', $a);
@@ -63,7 +63,7 @@ function testString($a) {
6363
* @param string|object $a
6464
*/
6565
function testStringObject($a) {
66-
assertType('object', new $a());
66+
assertType('mixed~string', new $a());
6767

6868
if (is_subclass_of($a, 'DateTimeInterface')) {
6969
assertType('class-string<DateTimeInterface>|DateTimeInterface', $a);
@@ -92,7 +92,7 @@ function testClassExists(string $str)
9292
assertType('string', $str);
9393
if (class_exists($str)) {
9494
assertType('class-string', $str);
95-
assertType('object', new $str());
95+
assertType('mixed~string', new $str());
9696
}
9797

9898
$existentClass = \stdClass::class;

0 commit comments

Comments
 (0)