Skip to content

Commit 7ce5f3f

Browse files
authored
Revise composite success flag handling (#41)
Most of the composite methods return a boolean result indicating whether or not all of the child handlers successfully performed the operation. If any of the children fails, the composite result is false. We were previously using a mix of coding styles: 1. $success = $success && func() 2. $success &= func() 3. if (!func()) { $success = false; } (1) works fine but results in more (re)writes to $success than are needed. (2) is logically equivalent but promotes the value to an integer, which results in the composite function returning an integer instead of a boolean. (#40) (3) is nice because it leaves the $success flag alone until there's a reason to change it to a failure. This change updates all of the code to consistently use (3).
1 parent 38e6668 commit 7ce5f3f

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Log/composite.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ public function open(): bool
5959
/* Attempt to open each of our children. */
6060
$this->opened = true;
6161
foreach ($this->children as $child) {
62-
$this->opened = $this->opened && $child->open();
62+
if (!$child->open()) {
63+
$this->opened = false;
64+
}
6365
}
6466

6567
/* If all children were opened, return success. */
@@ -82,8 +84,8 @@ public function close(): bool
8284
/* Attempt to close each of our children. */
8385
$closed = true;
8486
foreach ($this->children as $child) {
85-
if ($child->opened) {
86-
$closed = $closed && $child->close();
87+
if ($child->opened && !$child->close()) {
88+
$closed = false;
8789
}
8890
}
8991

@@ -107,7 +109,9 @@ public function flush(): bool
107109
/* Attempt to flush each of our children. */
108110
$flushed = true;
109111
foreach ($this->children as $child) {
110-
$flushed &= $child->flush();
112+
if (!$child->flush()) {
113+
$flushed = false;
114+
}
111115
}
112116

113117
/* If all children were flushed, return success. */
@@ -169,7 +173,9 @@ public function log($message, ?int $priority = null): bool
169173

170174
/* If this child has yet to be opened, attempt to do so now. */
171175
if (!$child->opened) {
172-
$success &= $child->open();
176+
if (!$child->open()) {
177+
$success = false;
178+
}
173179

174180
/*
175181
* If we've successfully opened our first handler, the
@@ -181,8 +187,8 @@ public function log($message, ?int $priority = null): bool
181187
}
182188

183189
/* Finally, attempt to log the message to the child handler. */
184-
if ($child->opened) {
185-
$success = $success && $child->log($message, $priority);
190+
if ($child->opened && !$child->log($message, $priority)) {
191+
$success = false;
186192
}
187193
}
188194

0 commit comments

Comments
 (0)