Skip to content

Commit

Permalink
Simplify TrackableTrait::getDesiredName() impl (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 6, 2023
1 parent 6592b4e commit 9361910
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/config.rst
Expand Up @@ -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:

Expand Down
7 changes: 5 additions & 2 deletions docs/factory.rst
Expand Up @@ -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
---------------
Expand Down
8 changes: 5 additions & 3 deletions src/Phpunit/TestCase.php
Expand Up @@ -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);
}

Expand Down
15 changes: 2 additions & 13 deletions src/TrackableTrait.php
Expand Up @@ -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)), '_');
}
Expand All @@ -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;
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/AppScopeTraitTest.php
Expand Up @@ -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());
Expand Down
6 changes: 4 additions & 2 deletions tests/ContainerTraitTest.php
Expand Up @@ -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);
}
Expand Down

0 comments on commit 9361910

Please sign in to comment.