Skip to content

Commit

Permalink
Support for new in initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 11, 2021
1 parent 9488d34 commit 1a102fe
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 9 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.13.1",
"ondram/ci-detector": "^3.4.0",
"ondrejmirtes/better-reflection": "4.3.79",
"ondrejmirtes/better-reflection": "4.3.80",
"phpstan/php-8-stubs": "^0.1.23",
"phpstan/phpdoc-parser": "^1.2.0",
"react/child-process": "^0.6.4",
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.

2 changes: 2 additions & 0 deletions src/Type/ConstantTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public static function getTypeFromValue($value): Type
$arrayBuilder->setOffsetValueType(self::getTypeFromValue($k), self::getTypeFromValue($v));
}
return $arrayBuilder->getArray();
} elseif (is_object($value)) {
return new ObjectType(get_class($value));
}

return new MixedType();
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,15 @@ public function dataFileAsserts(): iterable

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

if (PHP_VERSION_ID >= 80100 || self::$useStaticReflectionProvider) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/new-in-initializers.php');

if (PHP_VERSION_ID >= 80100) {
define('TEST_OBJECT_CONSTANT', new \stdClass());
yield from $this->gatherAssertTypes(__DIR__ . '/data/new-in-initializers-runtime.php');
}
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/filesystem-functions.php');
}

Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/data/new-in-initializers-runtime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php // lint >= 8.1

namespace NewInInitializers;

use function PHPStan\Testing\assertType;

assertType('stdClass', TEST_OBJECT_CONSTANT);
46 changes: 46 additions & 0 deletions tests/PHPStan/Analyser/data/new-in-initializers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php // lint >= 8.1

namespace NewInInitializers;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @template T of object
* @param T $test
* @return T
*/
public function doFoo(
object $test = new \stdClass()
): object
{
return $test;
}

#[\Test(new \stdClass())]
public function doBar()
{
assertType(\stdClass::class, $this->doFoo());
assertType('$this(NewInInitializers\Foo)', $this->doFoo($this));
assertType(Bar::class, $this->doFoo(new Bar()));
}

}

class Bar extends Foo
{

public function doBar()
{

}

public function doBaz()
{
static $o = new \stdClass();
assertType('mixed', $o);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public function dataConst(): array
'const_with_dir_const',
str_replace('\\', '/', __DIR__ . '/data'),
],
[
'OPTIMIZED_SFSL_OBJECT_CONSTANT',
new \stdClass(),
],
];
}

Expand All @@ -130,7 +134,7 @@ public function testConst(string $constantName, $value): void
$constantReflector = new ConstantReflector($locator, $classReflector);
$constant = $constantReflector->reflect($constantName);
$this->assertSame($constantName, $constant->getName());
$this->assertSame($value, $constant->getValue());
$this->assertEquals($value, $constant->getValue());
}

public function dataConstUnknown(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
define('TEST_VARIABLE', $foo);

define('const_with_dir_const', __DIR__);

define('OPTIMIZED_SFSL_OBJECT_CONSTANT', new \stdClass());
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ public function testBug2573(): void
$this->analyse([__DIR__ . '/data/bug-2573.php'], []);
}

public function testNewInInitializers(): void
{
if (PHP_VERSION_ID < 80100 && !self::$useStaticReflectionProvider) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->analyse([__DIR__ . '/data/new-in-initializers.php'], [
[
'Default value of the parameter #1 $i (stdClass) of method MethodNewInInitializers\Foo::doFoo() is incompatible with type int.',
11,
],
]);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Methods/data/new-in-initializers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php // lint >= 8.1

namespace MethodNewInInitializers;

class Foo
{

/**
* @param int $i
*/
public function doFoo($i = new \stdClass(), object $o = new \stdClass())
{

}

}

0 comments on commit 1a102fe

Please sign in to comment.