Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Node/ClassStatementsGatherer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use PhpParser\Node\Identifier;
use PHPStan\Analyser\Scope;
use PHPStan\Node\Constant\ClassConstantFetch;
use PHPStan\Node\Expr\SetExistingOffsetValueTypeExpr;
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
use PHPStan\Node\Property\PropertyAssign;
use PHPStan\Node\Property\PropertyRead;
use PHPStan\Node\Property\PropertyWrite;
Expand Down Expand Up @@ -200,7 +202,15 @@ private function gatherNodes(Node $node, Scope $scope): void
return;
}
if ($node instanceof PropertyAssignNode) {
$this->propertyUsages[] = new PropertyWrite($node->getPropertyFetch(), $scope, false);
$propertyFetch = $node->getPropertyFetch();
$assignedExpr = $node->getAssignedExpr();
if ($assignedExpr instanceof SetOffsetValueTypeExpr || $assignedExpr instanceof SetExistingOffsetValueTypeExpr) {
$propertyType = $scope->getType($propertyFetch);
if (!$propertyType->isObject()->no()) {
$this->propertyUsages[] = new PropertyRead($propertyFetch, $scope);
}
}
$this->propertyUsages[] = new PropertyWrite($propertyFetch, $scope, false);
$this->propertyAssigns[] = new PropertyAssign($node, $scope);
return;
}
Expand Down
19 changes: 9 additions & 10 deletions tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,6 @@ public function testRule(): void
191,
$tip,
],
[
'Property UnusedPrivateProperty\WriteToCollection::$collection1 is never read, only written.',
221,
$tip,
],
[
'Property UnusedPrivateProperty\WriteToCollection::$collection2 is never read, only written.',
224,
$tip,
],
]);
$this->analyse([__DIR__ . '/data/TestExtension.php'], [
[
Expand Down Expand Up @@ -410,4 +400,13 @@ public function testBug9213(): void
$this->analyse([__DIR__ . '/data/bug-9213.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug6777(): void
{
$this->alwaysWrittenTags = [];
$this->alwaysReadTags = [];

$this->analyse([__DIR__ . '/data/bug-6777.php'], []);
}

}
74 changes: 74 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-6777.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug6777;

class HelloWorld
{
/** @param \ArrayObject<int, string> $array */
public function __construct(private \ArrayObject $array){}

public function send(string $s) : void{
$this->array[] = $s;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a similar test with assign by reference

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Background task confirmed — make tests completed successfully as well. Everything was already verified and pushed.

}
}

class WithThreaded
{
private \Threaded $collection;

public function __construct()
{
$this->collection = new \Threaded();
}

public function add(string $s): void
{
$this->collection[] = $s;
}
}

class WithUnionObjectArray
{
/** @var \ArrayObject<int, string>|array<int, string> */
private \ArrayObject|array $collection;

/** @param \ArrayObject<int, string>|array<int, string> $collection */
public function __construct(\ArrayObject|array $collection)
{
$this->collection = $collection;
}

public function add(string $s): void
{
$this->collection[] = $s;
}
}

class WithAssignByReference
{
/** @param \ArrayObject<int, string> $array */
public function __construct(private \ArrayObject $array){}

public function send(string &$s) : void{
$this->array[] =& $s;
}
}

class WithUnionObjectString
{
/** @var \ArrayObject<int, string>|string */
private \ArrayObject|string $collection;

/** @param \ArrayObject<int, string>|string $collection */
public function __construct(\ArrayObject|string $collection)
{
$this->collection = $collection;
}

public function add(string $s): void
{
$this->collection[] = $s;
}
}
Loading