Skip to content

Commit

Permalink
ParserTest: Make sure recursion checking readonly arrays doesn't error
Browse files Browse the repository at this point in the history
If we assigned a recursion marker to a readonly array we'd get an
error since it's supposed to be immutable. However, since we dump
object properties by first casting instances to arrays it makes a
copy of the array to be checked which _can_ be modified unlike
the property itself.

Now you may be thinking "Doesn't this cause recursion checking to
break for 1 loop when dumping" but it turns out it's impossible to
assign references to readonly properties so this bug exists in the
users code and our recursion check breaking for 1 loop is actually
an accurate representation of what's going on!
  • Loading branch information
jnvsor committed Jul 12, 2024
1 parent 5f75d3a commit 7e7770a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/Fixtures/Php81TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ class Php81TestClass
public readonly string $a;
protected readonly string $b;
private readonly string $c;
public readonly array $d;

public function __construct(string $a)
{
$this->a = $a;
$this->b = $a.$a;
$this->c = $a.$a.$a;
$d = [$this->a, [$this->b], [[$this->c]]];
$d[] = &$d;
$this->d = $d;
}

public function typeHints(X & Y $p1)
Expand Down
2 changes: 2 additions & 0 deletions tests/Parser/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ public function testParseObjectReadonly()
$this->assertTrue($val[1]->readonly);
$this->assertSame('c', $val[2]->name);
$this->assertTrue($val[2]->readonly);
$this->assertSame('d', $val[3]->name);
$this->assertTrue($val[3]->readonly);
}

/**
Expand Down

0 comments on commit 7e7770a

Please sign in to comment.