From 9361910fc7393c44d7257331fe65c98fc9e153f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Fri, 6 Jan 2023 20:14:54 +0100 Subject: [PATCH] Simplify TrackableTrait::getDesiredName() impl (#368) --- docs/config.rst | 2 +- docs/factory.rst | 7 +++++-- src/Phpunit/TestCase.php | 8 +++++--- src/TrackableTrait.php | 15 ++------------- tests/AppScopeTraitTest.php | 3 ++- tests/ContainerTraitTest.php | 6 ++++-- 6 files changed, 19 insertions(+), 22 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index 24a32800..1ea48bb9 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -10,7 +10,7 @@ Introduction ============ This trait can be added to any object to load a configuration. -Configuration files can be of 4 types : php, php-inline, json, yaml. +Configuration files can be of 4 types: php, php-inline, json, yaml. Loading can be done in this way: diff --git a/docs/factory.rst b/docs/factory.rst index 305c145c..e76e7c27 100644 --- a/docs/factory.rst +++ b/docs/factory.rst @@ -166,14 +166,17 @@ Note that passing 'icon' => ['book'] will also use factory to initialize icon ob Finally, if you are using IDE and type hinting, a preferred code would be:: use Atk4\Ui\Button; - Factory::factory($button = new Button('A Label'), ['icon' => ['book'], 'action' => new Action(..)]); + + $button = new Button('A Label'); + Factory::factory($button, ['icon' => ['book'], 'action' => new Action(..)]); This will properly set type to $button variable, while still setting properties for icon/action. More commonly, however, you would use this through the add() method:: use Atk4\Ui\Button; - $view->add([$button = new Button('A Label'), 'icon' => ['book'], 'action' => new Action('..')]); + $button = new Button('A Label'); + $view->add([$button, 'icon' => ['book'], 'action' => new Action('..')]); Seed Components --------------- diff --git a/src/Phpunit/TestCase.php b/src/Phpunit/TestCase.php index d0b7b75b..83155bd3 100644 --- a/src/Phpunit/TestCase.php +++ b/src/Phpunit/TestCase.php @@ -200,9 +200,11 @@ public function &callProtected(object $obj, string $name, ...$args) { return \Closure::bind(static function &() use ($obj, $name, $args) { $objRefl = new \ReflectionClass($obj); - if ($objRefl - ->getMethod(!$objRefl->hasMethod($name) && $objRefl->hasMethod('__call') ? '__call' : $name) - ->returnsReference()) { + if ( + $objRefl + ->getMethod(!$objRefl->hasMethod($name) && $objRefl->hasMethod('__call') ? '__call' : $name) + ->returnsReference() + ) { return $obj->{$name}(...$args); } diff --git a/src/TrackableTrait.php b/src/TrackableTrait.php index 0177dd69..e49710fc 100644 --- a/src/TrackableTrait.php +++ b/src/TrackableTrait.php @@ -64,18 +64,7 @@ public function unsetOwner() public function getDesiredName(): string { // can be anything, but better to build meaningful name - $name = static::class; - if ((new \ReflectionClass($name))->isAnonymous()) { - $name = ''; - foreach (class_parents(static::class) as $v) { - if (!(new \ReflectionClass($v))->isAnonymous()) { - $name = $v; - - break; - } - } - $name .= '@anonymous'; - } + $name = get_debug_type($this); return trim(preg_replace('~^atk4\\\\[^\\\\]+\\\\|[^0-9a-z\x7f-\xfe]+~s', '_', mb_strtolower($name)), '_'); } @@ -94,7 +83,7 @@ public function destroy(): void $this->_app = null; // @phpstan-ignore-line } - // GC : remove reference to owner + // GC remove reference to owner $this->_owner = null; } } diff --git a/tests/AppScopeTraitTest.php b/tests/AppScopeTraitTest.php index e15dc4ba..68db6e86 100644 --- a/tests/AppScopeTraitTest.php +++ b/tests/AppScopeTraitTest.php @@ -33,7 +33,8 @@ public function testConstruct(): void // test for GC $m = new AppScopeMock(); $m->setApp($m); - $m->add($child = new AppScopeChildTrackable()); + $child = new AppScopeChildTrackable(); + $m->add($child); $child->destroy(); static::assertNull($this->getProtected($child, '_app')); static::assertFalse($child->issetOwner()); diff --git a/tests/ContainerTraitTest.php b/tests/ContainerTraitTest.php index 67a126fb..c39e7061 100644 --- a/tests/ContainerTraitTest.php +++ b/tests/ContainerTraitTest.php @@ -14,11 +14,13 @@ public function testBasic(): void $m = new ContainerMock(); // add to return object - $tr = $m->add($tr2 = new \stdClass()); + $tr2 = new \stdClass(); + $tr = $m->add($tr2); static::assertSame($tr, $tr2); // trackable object can be referenced by name - $m->add($tr3 = new TrackableMock(), 'foo'); + $tr3 = new TrackableMock(); + $m->add($tr3, 'foo'); $tr = $m->getElement('foo'); static::assertSame($tr, $tr3); }