From 08d11734a964218f56a57a1cf37c66033beb57a4 Mon Sep 17 00:00:00 2001 From: Theodore Brown Date: Sun, 6 Oct 2024 17:57:28 -0500 Subject: [PATCH] Use constructor property promotion (todo: update commit message to close issue 1) --- src/Helpers.php | 4 +-- src/Prop.php | 78 ++++++++++---------------------------------- src/QueryOptions.php | 26 ++++----------- test/HelpersTest.php | 20 ++++++------ 4 files changed, 36 insertions(+), 92 deletions(-) diff --git a/src/Helpers.php b/src/Helpers.php index a5c6a6f..8b12917 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -109,7 +109,7 @@ public static function mapRows(\Generator $rows, array $fieldProps): array $nullParents[$parent] = $prop; continue; - } elseif ($prop->noOutput) { + } elseif (!$prop->output) { continue; } @@ -227,7 +227,7 @@ public static function getFieldPropMap(array $fields, array $propMap): array foreach ($propMap as $prop => $data) { if (!isset($fieldProps[$prop]) && isset($dependedOn[$prop])) { $data = clone $data; - $data->noOutput = true; + $data->output = false; $fieldProps[$prop] = $data; } } diff --git a/src/Prop.php b/src/Prop.php index b75a7e1..ce7ac0f 100644 --- a/src/Prop.php +++ b/src/Prop.php @@ -4,85 +4,54 @@ namespace theodorejb\Phaster; +use Closure; use DateTimeZone; class Prop { - /** @readonly */ - public string $name; - /** @readonly */ - public string $col; - /** @readonly */ - public string $alias; - - /** - * @var callable|null - * @readonly - */ - public $getValue; - /** @readonly */ - public ?string $type = null; - - /** - * @var DateTimeZone|null|false - * @readonly - */ - public $timeZone; - - /** - * @var string[] - * @readonly - */ - public array $dependsOn; - /** @readonly */ - public bool $nullGroup; - /** @readonly */ - public bool $isDefault; - /** * This is the only property which can be modified externally. * @internal */ - public bool $noOutput; + public bool $output; /** * @var string[] - * @readonly */ - public array $map; - /** @readonly */ - public int $depth; + public readonly array $map; + public readonly int $depth; /** * @var string[] - * @readonly */ - public array $parents = []; + public readonly array $parents; /** - * @param DateTimeZone|null|false $timeZone * @param string[] $dependsOn */ public function __construct( - string $name, - string $col = '', - bool $nullGroup = false, - bool $isDefault = true, - string $alias = '', - ?string $type = null, - $timeZone = false, - ?callable $getValue = null, - array $dependsOn = [], + public readonly string $name, + public readonly string $col = '', + public readonly bool $nullGroup = false, + public readonly bool $isDefault = true, + public readonly string $alias = '', + public readonly ?string $type = null, + public readonly DateTimeZone|null|false $timeZone = false, + public readonly ?Closure $getValue = null, + public readonly array $dependsOn = [], bool $output = true ) { + $this->output = $output; $this->map = explode('.', $name); $this->depth = count($this->map); $parent = ''; + $parents = []; for ($i = 0; $i < $this->depth - 1; $i++) { $parent .= $this->map[$i] . '.'; - $this->parents[] = $parent; + $parents[] = $parent; } + $this->parents = $parents; if ($nullGroup && $this->depth < 2) { throw new \Exception("nullGroup cannot be set on top-level {$name} property"); @@ -113,17 +82,6 @@ public function __construct( throw new \Exception("timeZone cannot be set on {$name} property along with type"); } } - - $this->name = $name; - $this->col = $col; - $this->alias = $alias; - $this->getValue = $getValue; - $this->type = $type; - $this->timeZone = $timeZone; - $this->dependsOn = $dependsOn; - $this->nullGroup = $nullGroup; - $this->isDefault = $isDefault; - $this->noOutput = !$output; } public function getOutputCol(): string diff --git a/src/QueryOptions.php b/src/QueryOptions.php index e4d3af1..dcc38de 100644 --- a/src/QueryOptions.php +++ b/src/QueryOptions.php @@ -9,29 +9,15 @@ */ class QueryOptions { - /** @readonly */ - public array $filter; - /** @readonly */ - public array $originalFilter; - /** @readonly */ - public array $sort; - - /** - * @var Prop[] - * @readonly - */ - public array $fieldProps; - /** * @param Prop[] $fieldProps */ - public function __construct(array $filter, array $originalFilter, array $sort, array $fieldProps) - { - $this->filter = $filter; - $this->originalFilter = $originalFilter; - $this->sort = $sort; - $this->fieldProps = $fieldProps; - } + public function __construct( + public readonly array $filter, + public readonly array $originalFilter, + public readonly array $sort, + public readonly array $fieldProps + ) {} public function getColumns(): string { diff --git a/test/HelpersTest.php b/test/HelpersTest.php index 6bc79f3..010d3d2 100644 --- a/test/HelpersTest.php +++ b/test/HelpersTest.php @@ -163,8 +163,8 @@ public function testGetFieldPropMap(): void $expected = ['client.isDisabled', 'groupName', 'client.id']; $actual = Helpers::getFieldPropMap(['client.isDisabled', 'groupName'], $propMap); $this->assertSame($expected, array_keys($actual)); - $this->assertFalse($actual['groupName']->noOutput); - $this->assertTrue($actual['client.id']->noOutput); + $this->assertTrue($actual['groupName']->output); + $this->assertFalse($actual['client.id']->output); $expected = [ 'client.id' => $propMap['client.id'], @@ -189,8 +189,8 @@ public function testGetFieldPropMap(): void $expected = ['groupName', 'client.name']; $actual = Helpers::getFieldPropMap(['groupName'], $propMap); $this->assertSame($expected, array_keys($actual)); - $this->assertFalse($actual['groupName']->noOutput); - $this->assertTrue($actual['client.name']->noOutput); + $this->assertTrue($actual['groupName']->output); + $this->assertFalse($actual['client.name']->output); try { Helpers::getFieldPropMap(['group.test'], $propMap); @@ -221,17 +221,17 @@ public function testGetFieldPropMap(): void $expected = ['client.name', 'client.id']; $actual = Helpers::getFieldPropMap(['client.name'], $propMap); $this->assertSame($expected, array_keys($actual)); - $this->assertFalse($actual['client.name']->noOutput); - $this->assertTrue($actual['client.id']->noOutput); + $this->assertTrue($actual['client.name']->output); + $this->assertFalse($actual['client.id']->output); // username should be selected even though it isn't default since client.id is marked as dependent on it $expected = ['client.id', 'client.name', 'client.secret', 'username']; $actual = Helpers::getFieldPropMap([], $propMap); $this->assertSame($expected, array_keys($actual)); - $this->assertFalse($actual['client.id']->noOutput); - $this->assertFalse($actual['client.name']->noOutput); - $this->assertTrue($actual['client.secret']->noOutput); - $this->assertTrue($actual['username']->noOutput); + $this->assertTrue($actual['client.id']->output); + $this->assertTrue($actual['client.name']->output); + $this->assertFalse($actual['client.secret']->output); + $this->assertFalse($actual['username']->output); } public function testPropListToPropMap(): void