Skip to content

Commit 376be21

Browse files
Merge pull request #4 from php-enspired/unconstructable
Fix Constructor Process
2 parents c25f2f3 + eb57791 commit 376be21

1 file changed

Lines changed: 27 additions & 16 deletions

File tree

src/Exception.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,22 @@ public static function has_info(int $code) : bool {
9393
/** @see Exceptable::__construct() */
9494
public function __construct(...$arguments) {
9595
$args = $arguments;
96+
9697
$message = is_string(reset($args)) ? array_shift($args) : null;
97-
$code = is_int(reset($args)) ? array_shift($args) : null;
98+
$code = $this->_makeCode(is_int(reset($args)) ? array_shift($args) : null);
9899
$previous = (reset($args) instanceof Throwable) ? array_shift($args) : null;
99-
if (is_array(reset($args))) {
100-
$this->addContext(array_shift($args));
100+
$this->setSeverity($this->_makeSeverity($code));
101+
102+
$context = is_array(reset($args)) ? array_shift($args) : [];
103+
$context['__severity__'] = $this->getSeverity();
104+
if ($previous) {
105+
$context['__rootMessage__'] = $this->_getRoot($previous)->getMessage();
101106
}
102-
$code = $this->_makeCode($code);
107+
$this->addContext($context);
108+
103109
$message = $this->_makeMessage($message, $code);
104-
$this->setSeverity($this->_makeSeverity($code));
105-
$this->addContext(['__severity__' => $this->getSeverity()]);
110+
111+
parent::__construct($message, $code, $previous);
106112

107113
// exceptional exceptable: bad args
108114
if (! empty($args)) {
@@ -111,11 +117,6 @@ public function __construct(...$arguments) {
111117
['args' => $arguments]
112118
);
113119
}
114-
115-
parent::__construct($message, $code, $previous);
116-
if ($previous) {
117-
$this->addContext(['__rootMessage__' => $this->getRoot()->getMessage()]);
118-
}
119120
}
120121

121122
/** @see <http://php.net/__toString> */
@@ -141,11 +142,7 @@ public function getContext() : array {
141142

142143
/** @see Exceptable::getRoot() */
143144
public function getRoot() : Throwable {
144-
$root = $this;
145-
while ($root->getPrevious() !== null) {
146-
$root = $root->getPrevious();
147-
}
148-
return $root;
145+
return $this->_getRoot($this);
149146
}
150147

151148
/** @see Exceptable::getSeverity() */
@@ -182,6 +179,20 @@ public function setSeverity(int $severity) : Exceptable {
182179
return $this;
183180
}
184181

182+
/**
183+
* Finds the root exception.
184+
*
185+
* @param Throwable $leaf The exception to start from
186+
* @return Throwable The root exception
187+
*/
188+
public function _getRoot(Throwable $leaf) : Throwable {
189+
$root = $leaf;
190+
while ($root->getPrevious() !== null) {
191+
$root = $root->getPrevious();
192+
}
193+
return $root;
194+
}
195+
185196
/**
186197
* generates a default exception code.
187198
*

0 commit comments

Comments
 (0)