Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added failling test #2900

Draft
wants to merge 6 commits into
base: 1.10.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Node/ClassPropertiesNode.php
Expand Up @@ -192,6 +192,10 @@ public function getUninitializedProperties(
continue;
}

if ($usageScope->isInAnonymousFunction() && $usageScope->getParentScope() !== null) {
$usageScope = $usageScope->getParentScope();
}

$propertyReflection = $usageScope->getPropertyReflection($fetchedOnType, $propertyName);
if ($propertyReflection === null) {
continue;
Expand Down Expand Up @@ -353,6 +357,10 @@ private function getMethodsCalledFromConstructor(
continue;
}

if ($callScope->isInAnonymousFunction() && $callScope->getParentScope() !== null) {
$callScope = $callScope->getParentScope();
}

$methodName = $methodCallNode->name->toString();
if (array_key_exists($methodName, $initializedProperties)) {
foreach ($this->getInitializedProperties($callScope, $initializedProperties[$inMethod->getName()] ?? $initialInitializedProperties) as $propertyName => $isInitialized) {
Expand Down
Expand Up @@ -247,6 +247,20 @@ public function testBug9577(): void
]);
}

public function testBug10048(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/bug-10048.php'], [
[
'Readonly property Foo::$bar is already assigned',
66,
],
]);
}

public function testAnonymousReadonlyClass(): void
{
if (PHP_VERSION_ID < 80300) {
Expand Down
80 changes: 80 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-10048.php
@@ -0,0 +1,80 @@
<?php // lint >= 8.1

namespace Bug10048;

class Foo
{
private readonly string $bar;
private readonly \Closure $callback;

public function __construct()
{
$this->bar = "hi";
$this->useBar();
echo $this->bar;
$this->callback = function () {
$this->useBar();
};
}

private function useBar(): void
{
echo $this->bar;
}

public function useCallback(): void
{
call_user_func($this->callback);
}
}

function doFoo() {
(new Foo())->useCallback();
}

class Bar
{
private readonly string $bar;
private readonly \Closure $callback;

public function __construct()
{
$this->bar = "hi";

$this->callback = function () {
$this->useBar();
};
}

private function useBar(): void
{
echo $this->bar;
}
}


class CannotModifyAlreadyAssigned
{
private readonly string $bar;
private readonly \Closure $callback;

public function __construct()
{
$this->bar = "hi";

$this->callback = function () {
$this->bar = "hidiho";
$this->useBar();
};
}

private function useBar(): void
{
echo $this->bar;
}

public function useCallback(): void
{
call_user_func($this->callback);
}
}