66c172c6ceeb247a74bb8e5514245f2a50507311
[yaffs-website] / vendor / psy / psysh / src / Psy / Exception / ErrorException.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Exception;
13
14 /**
15  * A custom error Exception for Psy with a formatted $message.
16  */
17 class ErrorException extends \ErrorException implements Exception
18 {
19     private $rawMessage;
20
21     /**
22      * Construct a Psy ErrorException.
23      *
24      * @param string    $message  (default: "")
25      * @param int       $code     (default: 0)
26      * @param int       $severity (default: 1)
27      * @param string    $filename (default: null)
28      * @param int       $lineno   (default: null)
29      * @param Exception $previous (default: null)
30      */
31     public function __construct($message = '', $code = 0, $severity = 1, $filename = null, $lineno = null, $previous = null)
32     {
33         $this->rawMessage = $message;
34
35         if (!empty($filename) && preg_match('{Psy[/\\\\]ExecutionLoop}', $filename)) {
36             $filename = '';
37         }
38
39         switch ($severity) {
40             case E_WARNING:
41             case E_CORE_WARNING:
42             case E_COMPILE_WARNING:
43             case E_USER_WARNING:
44                 $type = 'warning';
45                 break;
46
47             case E_STRICT:
48                 $type = 'Strict error';
49                 break;
50
51             default:
52                 $type = 'error';
53                 break;
54         }
55
56         $message = sprintf('PHP %s:  %s%s on line %d', $type, $message, $filename ? ' in ' . $filename : '', $lineno);
57         parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
58     }
59
60     /**
61      * Get the raw (unformatted) message for this error.
62      *
63      * @return string
64      */
65     public function getRawMessage()
66     {
67         return $this->rawMessage;
68     }
69
70     /**
71      * Helper for throwing an ErrorException.
72      *
73      * This allows us to:
74      *
75      *     set_error_handler(array('Psy\Exception\ErrorException', 'throwException'));
76      *
77      * @throws ErrorException
78      *
79      * @param int    $errno   Error type
80      * @param string $errstr  Message
81      * @param string $errfile Filename
82      * @param int    $errline Line number
83      */
84     public static function throwException($errno, $errstr, $errfile, $errline)
85     {
86         throw new self($errstr, 0, $errno, $errfile, $errline);
87     }
88
89     /**
90      * Create an ErrorException from an Error.
91      *
92      * @param \Error $e
93      *
94      * @return ErrorException
95      */
96     public static function fromError(\Error $e)
97     {
98         return new self($e->getMessage(), $e->getCode(), 1, $e->getFile(), $e->getLine(), $e);
99     }
100 }