From: Tim Düsterhus Date: Sat, 17 Dec 2011 14:39:11 +0000 (+0100) Subject: Improved error-handling of \wcf\system\Regex X-Git-Tag: 2.0.0_Beta_1~1480^2~1 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=a25f25ee771a16a3004b31707c425a0da865becf;p=GitHub%2FWoltLab%2FWCF.git Improved error-handling of \wcf\system\Regex --- diff --git a/wcfsetup/install/files/lib/system/Regex.class.php b/wcfsetup/install/files/lib/system/Regex.class.php index 5f9ade7c63..966f698a2b 100644 --- a/wcfsetup/install/files/lib/system/Regex.class.php +++ b/wcfsetup/install/files/lib/system/Regex.class.php @@ -112,16 +112,10 @@ final class Regex { */ public function match($string, $all = false) { if ($all) { - $result = preg_match_all($this->regex, $string, $this->matches); - } - else { - $result = preg_match($this->regex, $string, $this->matches); + return $this->checkResult(preg_match_all($this->regex, $string, $this->matches), 'match'); } - if ($result === false) { - throw new SystemException('Could not execute match on '.$this->regex); - } - return $result; + return $this->checkResult(preg_match($this->regex, $string, $this->matches), 'match'); } /** @@ -133,16 +127,10 @@ final class Regex { */ public function replace($string, $replacement) { if ($replacement instanceof Callback) { - $result = preg_replace_callback($this->regex, $replacement, $string); - } - else { - $result = preg_replace($this->regex, $replacement, $string); + return $this->checkResult(preg_replace_callback($this->regex, $replacement, $string), 'replace'); } - if ($result === false) { - throw new SystemException('Could not execute replace on '.$this->regex); - } - return $result; + return $this->checkResult(preg_replace($this->regex, $replacement, $string), 'replace'); } /** @@ -152,10 +140,34 @@ final class Regex { * @return array */ public function split($string) { - $result = preg_split($this->regex, $string); - + return $this->checkResult(preg_split($this->regex, $string), 'split'); + } + + /** + * Checks whether there was success. + * + * @param mixed $result + */ + private function checkResult($result, $method = '') { if ($result === false) { - throw new SystemException('Could not execute split on '.$this->regex); + switch (preg_last_error()) { + case PREG_INTERNAL_ERROR: + $error = 'Internal error'; + break; + case PREG_BACKTRACK_LIMIT_ERROR: + $error = 'Backtrack limit was exhausted'; + break; + case PREG_RECURSION_LIMIT_ERROR: + $error = 'Recursion limit was exhausted'; + break; + case PREG_BAD_UTF8_ERROR: + $error = 'Bad UTF8'; + break; + default: + $error = 'Unknown error'; + } + + throw new SystemException('Could not execute '.($method ? $method.' on ' : '').$this->regex.': '.$error); } return $result; }