Skip to content

Commit 977929f

Browse files
committed
refactoring
1 parent 32679dd commit 977929f

File tree

2 files changed

+11
-21
lines changed

2 files changed

+11
-21
lines changed

src/ComponentModel/Component.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,14 @@ final public function lookup(?string $type, bool $throw = true): ?IComponent
5656
}
5757
}
5858

59-
if ($obj) {
60-
$this->monitors[$type] = [$obj, $depth, substr($path, 1), []];
61-
62-
} else {
63-
$this->monitors[$type] = [null, null, null, []]; // not found
64-
}
59+
$this->monitors[$type] = $obj
60+
? [$obj, $depth, substr($path, 1), []]
61+
: [null, null, null, []]; // not found
6562
}
6663

6764
if ($throw && $this->monitors[$type][0] === null) {
68-
$message = $this->name !== null
69-
? "Component '$this->name' is not attached to '$type'."
70-
: "Component of type '" . static::class . "' is not attached to '$type'.";
71-
throw new Nette\InvalidStateException($message);
65+
$desc = $this->name === null ? "type of '" . static::class . "'" : "'$this->name'";
66+
throw new Nette\InvalidStateException("Component $desc is not attached to '$type'.");
7267
}
7368

7469
return $this->monitors[$type][0];
@@ -179,7 +174,7 @@ public function setParent(?IContainer $parent, ?string $name = null): static
179174
throw new Nette\InvalidStateException("Component '$this->name' already has a parent.");
180175
}
181176

182-
// remove from parent?
177+
// remove from parent
183178
if ($parent === null) {
184179
$this->refreshMonitors(0);
185180
$this->parent = null;

src/ComponentModel/Container.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ public function addComponent(IComponent $component, ?string $name, ?string $inse
4646

4747
if (!preg_match(self::NameRegexp, $name)) {
4848
throw new Nette\InvalidArgumentException("Component name must be non-empty alphanumeric string, '$name' given.");
49-
}
5049

51-
if (isset($this->components[$name])) {
50+
} elseif (isset($this->components[$name])) {
5251
throw new Nette\InvalidStateException("Component with name '$name' already exists.");
5352
}
5453

@@ -58,9 +57,7 @@ public function addComponent(IComponent $component, ?string $name, ?string $inse
5857
if ($obj === $component) {
5958
throw new Nette\InvalidStateException("Circular reference detected while adding component '$name'.");
6059
}
61-
62-
$obj = $obj->getParent();
63-
} while ($obj !== null);
60+
} while (($obj = $obj->getParent()) !== null);
6461

6562
// user checking
6663
$this->validateChildComponent($component);
@@ -117,11 +114,9 @@ final public function getComponent(string $name, bool $throw = true): ?IComponen
117114

118115
if (!isset($this->components[$name])) {
119116
if (!preg_match(self::NameRegexp, $name)) {
120-
if ($throw) {
121-
throw new Nette\InvalidArgumentException("Component name must be non-empty alphanumeric string, '$name' given.");
122-
}
123-
124-
return null;
117+
return $throw
118+
? throw new Nette\InvalidArgumentException("Component name must be non-empty alphanumeric string, '$name' given.")
119+
: null;
125120
}
126121

127122
$component = $this->createComponent($name);

0 commit comments

Comments
 (0)