Skip to content

Commit

Permalink
Property async visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jnvsor committed Oct 4, 2024
1 parent 0e7f684 commit 50acf41
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 7 deletions.
Binary file modified build/kint.phar
Binary file not shown.
7 changes: 7 additions & 0 deletions src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,12 @@ private function parseObject(&$var, Value $o): Value
$child->depth = $object->depth + 1;

if (KINT_PHP84 && $rprop->isDefault()) {
if ($rprop->isProtectedSet()) {
$child->access_set |= Value::ACCESS_PROTECTED;
} elseif ($rprop->isPrivateSet()) {
$child->access_set |= Value::ACCESS_PRIVATE;
}

$hooks = $rprop->getHooks();
if (isset($hooks['get'])) {
$child->hooks |= Value::HOOK_GET;
Expand All @@ -515,6 +521,7 @@ private function parseObject(&$var, Value $o): Value
}
if (isset($hooks['set'])) {
$child->hooks |= Value::HOOK_SET;

$child->hook_set_type = (string) $rprop->getSettableType();
if ($child->hook_set_type !== (string) $rprop->getType()) {
$child->hooks |= Value::HOOK_SET_TYPE;
Expand Down
23 changes: 19 additions & 4 deletions src/Zval/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Value
public bool $const = false;
/** @psalm-var self::ACCESS_* */
public int $access = self::ACCESS_NONE;
/** @psalm-var self::ACCESS_* */
public int $access_set = self::ACCESS_NONE;
/** @psalm-var ?class-string */
public ?string $owner_class = null;
public ?string $access_path = null;
Expand Down Expand Up @@ -199,12 +201,25 @@ public function getModifiers(): ?string
public function getAccess(): ?string
{
switch ($this->access) {
case self::ACCESS_PRIVATE:
return 'private';
case self::ACCESS_PROTECTED:
return 'protected';
case self::ACCESS_PUBLIC:
if (self::ACCESS_PRIVATE === $this->access_set) {
return 'private(set)';
}
if (self::ACCESS_PROTECTED === $this->access_set) {
return 'protected(set)';
}

return 'public';

case self::ACCESS_PROTECTED:
if (self::ACCESS_PRIVATE === $this->access_set) {
return 'protected private(set)';
}

return 'protected';

case self::ACCESS_PRIVATE:
return 'private';
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion tests/CallFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ public function sourceProvider()
'<?php
class TestClass {
public string $test_property {
public private(set) string $test_property {
set (string $value) {
test("Setter called:", $value);
$this->test_property = $value;
Expand Down
6 changes: 6 additions & 0 deletions tests/Fixtures/Php84TestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ class Php84TestClass
$this->b = $value - 1;
}
}

public private(set) int $g;
public protected(set) int $h;
private(set) int $i;
protected(set) int $j;
protected private(set) int $k;
}
52 changes: 51 additions & 1 deletion tests/Parser/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,32 +375,44 @@ public function testParseObject()

$this->assertSame('pub', $props[0]->name);
$this->assertSame('array', $props[0]->type);
$this->assertSame(Value::ACCESS_PUBLIC, $props[0]->access);
$this->assertSame(Value::ACCESS_NONE, $props[0]->access_set);
$this->assertSame(Value::OPERATOR_OBJECT, $props[0]->operator);
$this->assertSame(TestClass::class, $props[0]->owner_class);
$this->assertSame('$v->pub', $props[0]->access_path);
$this->assertSame('pro', $props[1]->name);
$this->assertSame('array', $props[1]->type);
$this->assertSame(ChildTestClass::class, $props[1]->owner_class);
$this->assertSame(Value::ACCESS_PROTECTED, $props[1]->access);
$this->assertSame(Value::ACCESS_NONE, $props[1]->access_set);
$this->assertSame(Value::OPERATOR_OBJECT, $props[1]->operator);
$this->assertSame(ChildTestClass::class, $props[1]->owner_class);
$this->assertSame('$v->pro', $props[1]->access_path);
$this->assertSame('pri', $props[2]->name);
$this->assertSame('array', $props[2]->type);
$this->assertSame(Value::ACCESS_PRIVATE, $props[2]->access);
$this->assertSame(Value::ACCESS_NONE, $props[2]->access_set);
$this->assertSame(Value::OPERATOR_OBJECT, $props[2]->operator);
$this->assertSame(TestClass::class, $props[2]->owner_class);
$this->assertSame('$v->pri', $props[2]->access_path);

$this->assertSame('pub2', $props[3]->name);
$this->assertSame('null', $props[3]->type);
$this->assertSame(Value::ACCESS_PUBLIC, $props[3]->access);
$this->assertSame(Value::ACCESS_NONE, $props[3]->access_set);
$this->assertSame(Value::OPERATOR_OBJECT, $props[3]->operator);
$this->assertSame(ChildTestClass::class, $props[3]->owner_class);
$this->assertSame('$v->pub2', $props[3]->access_path);
$this->assertSame('pro2', $props[4]->name);
$this->assertSame('null', $props[4]->type);
$this->assertSame(Value::ACCESS_PROTECTED, $props[4]->access);
$this->assertSame(Value::ACCESS_NONE, $props[4]->access_set);
$this->assertSame(Value::OPERATOR_OBJECT, $props[4]->operator);
$this->assertSame(ChildTestClass::class, $props[4]->owner_class);
$this->assertSame('$v->pro2', $props[4]->access_path);
$this->assertSame('pri2', $props[5]->name);
$this->assertSame('null', $props[5]->type);
$this->assertSame(Value::ACCESS_PRIVATE, $props[5]->access);
$this->assertSame(Value::ACCESS_NONE, $props[5]->access_set);
$this->assertSame(Value::OPERATOR_OBJECT, $props[5]->operator);
$this->assertSame(ChildTestClass::class, $props[5]->owner_class);
$this->assertNull($props[5]->access_path);
Expand Down Expand Up @@ -986,6 +998,44 @@ public function testParseHooks()
$this->assertSame('virtual', $props[5]->type);
}

/**
* @covers \Kint\Parser\Parser::parseObject
*/
public function testParseAsymmetricVisibility()
{
if (!KINT_PHP84) {
$this->markTestSkipped('Not testing asymmetric property visilibity below 8.4');
}

$b = new Value('$v');
$b->access_path = '$v';
$v = new Php84TestClass();

$p = new Parser();
$o = $p->parse($v, clone $b);
$props = $o->value->contents;

$this->assertSame('g', $props[6]->name);
$this->assertSame(Value::ACCESS_PUBLIC, $props[6]->access);
$this->assertSame(Value::ACCESS_PRIVATE, $props[6]->access_set);

$this->assertSame('h', $props[7]->name);
$this->assertSame(Value::ACCESS_PUBLIC, $props[7]->access);
$this->assertSame(Value::ACCESS_PROTECTED, $props[7]->access_set);

$this->assertSame('i', $props[8]->name);
$this->assertSame(Value::ACCESS_PUBLIC, $props[8]->access);
$this->assertSame(Value::ACCESS_PRIVATE, $props[8]->access_set);

$this->assertSame('j', $props[9]->name);
$this->assertSame(Value::ACCESS_PUBLIC, $props[9]->access);
$this->assertSame(Value::ACCESS_PROTECTED, $props[9]->access_set);

$this->assertSame('k', $props[10]->name);
$this->assertSame(Value::ACCESS_PROTECTED, $props[10]->access);
$this->assertSame(Value::ACCESS_PRIVATE, $props[10]->access_set);
}

/**
* @covers \Kint\Parser\Parser::addPlugin
* @covers \Kint\Parser\Parser::applyPlugins
Expand Down
38 changes: 37 additions & 1 deletion tests/Zval/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,78 +291,113 @@ public function modifierProvider()
false,
false,
Value::ACCESS_PUBLIC,
Value::ACCESS_NONE,
'public',
],
'public const' => [
true,
false,
false,
Value::ACCESS_PUBLIC,
Value::ACCESS_NONE,
'public const',
],
'public static' => [
false,
true,
false,
Value::ACCESS_PUBLIC,
Value::ACCESS_NONE,
'public static',
],
'protected' => [
false,
false,
false,
Value::ACCESS_PROTECTED,
Value::ACCESS_NONE,
'protected',
],
'private' => [
false,
false,
false,
Value::ACCESS_PRIVATE,
Value::ACCESS_NONE,
'private',
],
'none' => [
false,
false,
false,
Value::ACCESS_NONE,
Value::ACCESS_NONE,
null,
],
'private static' => [
false,
true,
false,
Value::ACCESS_PRIVATE,
Value::ACCESS_NONE,
'private static',
],
'public const static' => [
true,
true,
false,
Value::ACCESS_PUBLIC,
Value::ACCESS_NONE,
'public const static',
],
'const' => [
true,
false,
false,
Value::ACCESS_NONE,
Value::ACCESS_NONE,
'const',
],
'public readonly' => [
false,
false,
true,
Value::ACCESS_PUBLIC,
Value::ACCESS_NONE,
'public readonly',
],
'private readonly' => [
false,
false,
true,
Value::ACCESS_PRIVATE,
Value::ACCESS_NONE,
'private readonly',
],
'private(set) set hook' => [
false,
false,
false,
Value::ACCESS_PUBLIC,
Value::ACCESS_PRIVATE,
'private(set)',
],
'protected(set) set hook' => [
false,
false,
false,
Value::ACCESS_PUBLIC,
Value::ACCESS_PROTECTED,
'protected(set)',
],
'protected private(set) set hook' => [
false,
false,
false,
Value::ACCESS_PROTECTED,
Value::ACCESS_PRIVATE,
'protected private(set)',
],
];
}

Expand All @@ -371,13 +406,14 @@ public function modifierProvider()
*
* @covers \Kint\Zval\Value::getModifiers
*/
public function testGetModifiers(bool $const, bool $static, bool $readonly, int $access, ?string $expect)
public function testGetModifiers(bool $const, bool $static, bool $readonly, int $access, int $access_set, ?string $expect)
{
$o = new Value('base');
$o->readonly = $readonly;
$o->const = $const;
$o->static = $static;
$o->access = $access;
$o->access_set = $access_set;
$this->assertSame($expect, $o->getModifiers());
}

Expand Down

0 comments on commit 50acf41

Please sign in to comment.