Skip to content

Commit

Permalink
Fix method_exists() after new $class
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 21, 2021
1 parent e713aa8 commit 70756b0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4641,7 +4641,7 @@ private function getTypeToInstantiateForNew(Type $type): Type
foreach ($type->getTypes() as $innerType) {
$decidedType = $decideType($innerType);
if ($decidedType === null) {
return new MixedType();
return new ObjectWithoutClassType();
}

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

$decidedType = $decideType($type);
if ($decidedType === null) {
return new MixedType();
return new ObjectWithoutClassType();
}

return $decidedType;
Expand Down
8 changes: 7 additions & 1 deletion tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@ public function dataDeductedTypes(): array
'$loremObjectLiteral',
],
[
'mixed',
'object',
'$mixedObjectLiteral',
],
[
Expand Down Expand Up @@ -10897,6 +10897,11 @@ public function dataBug4577(): array
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4577.php');
}

public function dataBug4579(): array
{
return $this->gatherAssertTypes(__DIR__ . '/data/bug-4579.php');
}

/**
* @param string $file
* @return array<string, mixed[]>
Expand Down Expand Up @@ -11138,6 +11143,7 @@ private function gatherAssertTypes(string $file): array
* @dataProvider dataArrayMapClosure
* @dataProvider dataBug4573
* @dataProvider dataBug4577
* @dataProvider dataBug4579
* @param string $assertType
* @param string $file
* @param mixed ...$args
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4579.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Bug4579;

use function PHPStan\Analyser\assertType;

function (string $class): void {
$foo = new $class();
if (method_exists($foo, 'doFoo')) {
assertType('object&hasMethod(doFoo)', $foo);
}
};
9 changes: 5 additions & 4 deletions tests/PHPStan/Analyser/data/generic-class-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function f(): int {
* @param mixed $a
*/
function testMixed($a) {
assertType('mixed', new $a());
assertType('object', new $a());

if (is_subclass_of($a, 'DateTimeInterface')) {
assertType('class-string<DateTimeInterface>|DateTimeInterface', $a);
Expand All @@ -36,7 +36,7 @@ function testMixed($a) {
* @param object $a
*/
function testObject($a) {
assertType('mixed', new $a());
assertType('object', new $a());

if (is_subclass_of($a, 'DateTimeInterface')) {
assertType('DateTimeInterface', $a);
Expand All @@ -47,7 +47,7 @@ function testObject($a) {
* @param string $a
*/
function testString($a) {
assertType('mixed', new $a());
assertType('object', new $a());

if (is_subclass_of($a, 'DateTimeInterface')) {
assertType('class-string<DateTimeInterface>', $a);
Expand All @@ -63,7 +63,7 @@ function testString($a) {
* @param string|object $a
*/
function testStringObject($a) {
assertType('mixed', new $a());
assertType('object', new $a());

if (is_subclass_of($a, 'DateTimeInterface')) {
assertType('class-string<DateTimeInterface>|DateTimeInterface', $a);
Expand Down Expand Up @@ -92,6 +92,7 @@ function testClassExists(string $str)
assertType('string', $str);
if (class_exists($str)) {
assertType('class-string', $str);
assertType('object', new $str());
}

$existentClass = \stdClass::class;
Expand Down

0 comments on commit 70756b0

Please sign in to comment.