Skip to content

Commit dd92836

Browse files
authored
Merge pull request #69 from nguyenanhung/v3.2.0-develop
Optimize E_STRICT
2 parents 4fbb55f + d6313fe commit dd92836

File tree

1 file changed

+93
-112
lines changed

1 file changed

+93
-112
lines changed

system/core/Exceptions.php

Lines changed: 93 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* CodeIgniter
45
*
@@ -26,65 +27,70 @@
2627
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2728
* THE SOFTWARE.
2829
*
29-
* @package CodeIgniter
30-
* @author EllisLab Dev Team
31-
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
32-
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33-
* @copyright Copyright (c) 2019 - 2022, CodeIgniter Foundation (https://codeigniter.com/)
34-
* @license https://opensource.org/licenses/MIT MIT License
35-
* @link https://codeigniter.com
36-
* @since Version 1.0.0
30+
* @package CodeIgniter
31+
* @author EllisLab Dev Team
32+
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
33+
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
34+
* @copyright Copyright (c) 2019 - 2022, CodeIgniter Foundation (https://codeigniter.com/)
35+
* @license https://opensource.org/licenses/MIT MIT License
36+
* @link https://codeigniter.com
37+
* @since Version 1.0.0
3738
* @filesource
3839
*/
39-
defined('BASEPATH') OR exit('No direct script access allowed');
40+
defined('BASEPATH') or exit('No direct script access allowed');
4041

4142
/**
4243
* Exceptions Class
4344
*
44-
* @package CodeIgniter
45-
* @subpackage Libraries
46-
* @category Exceptions
47-
* @author EllisLab Dev Team
48-
* @link https://codeigniter.com/userguide3/libraries/exceptions.html
45+
* @package CodeIgniter
46+
* @subpackage Libraries
47+
* @category Exceptions
48+
* @author EllisLab Dev Team
49+
* @link https://codeigniter.com/userguide3/libraries/exceptions.html
4950
*/
50-
class CI_Exceptions {
51+
class CI_Exceptions
52+
{
5153

5254
/**
5355
* Nesting level of the output buffering mechanism
5456
*
55-
* @var int
57+
* @var int
5658
*/
5759
public $ob_level;
5860

5961
/**
6062
* List of available error levels
6163
*
62-
* @var array
64+
* @var array
6365
*/
6466
public $levels = array(
65-
E_ERROR => 'Error',
66-
E_WARNING => 'Warning',
67-
E_PARSE => 'Parsing Error',
68-
E_NOTICE => 'Notice',
69-
E_CORE_ERROR => 'Core Error',
70-
E_CORE_WARNING => 'Core Warning',
71-
E_COMPILE_ERROR => 'Compile Error',
72-
E_COMPILE_WARNING => 'Compile Warning',
73-
E_USER_ERROR => 'User Error',
74-
E_USER_WARNING => 'User Warning',
75-
E_USER_NOTICE => 'User Notice',
76-
E_STRICT => 'Runtime Notice'
67+
E_ERROR => 'Error',
68+
E_WARNING => 'Warning',
69+
E_PARSE => 'Parsing Error',
70+
E_NOTICE => 'Notice',
71+
E_CORE_ERROR => 'Core Error',
72+
E_CORE_WARNING => 'Core Warning',
73+
E_COMPILE_ERROR => 'Compile Error',
74+
E_COMPILE_WARNING => 'Compile Warning',
75+
E_USER_ERROR => 'User Error',
76+
E_USER_WARNING => 'User Warning',
77+
E_USER_NOTICE => 'User Notice'
7778
);
7879

7980
/**
8081
* Class constructor
8182
*
82-
* @return void
83+
* @return void
8384
*/
8485
public function __construct()
8586
{
8687
$this->ob_level = ob_get_level();
8788
// Note: Do not log messages from this constructor.
89+
90+
// Check if PHP version is less than 8.4.0 and E_STRICT is defined
91+
if (version_compare(PHP_VERSION, '8.4.0', '<') && defined('E_STRICT')) {
92+
$this->levels[E_STRICT] = 'Runtime Notice';
93+
}
8894
}
8995

9096
// --------------------------------------------------------------------
@@ -94,47 +100,43 @@ public function __construct()
94100
*
95101
* Logs PHP generated error messages
96102
*
97-
* @param int $severity Log level
98-
* @param string $message Error message
99-
* @param string $filepath File path
100-
* @param int $line Line number
101-
* @return void
103+
* @param int $severity Log level
104+
* @param string $message Error message
105+
* @param string $filepath File path
106+
* @param int $line Line number
107+
* @return void
102108
*/
103109
#[\ReturnTypeWillChange]
104110
public function log_exception($severity, $message, $filepath, $line)
105111
{
106112
$severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
107-
log_message('error', 'Severity: '.$severity.' --> '.$message.' '.$filepath.' '.$line);
113+
log_message('error', 'Severity: ' . $severity . ' --> ' . $message . ' ' . $filepath . ' ' . $line);
108114
}
109115

110116
// --------------------------------------------------------------------
111117

112118
/**
113119
* 404 Error Handler
114120
*
115-
* @uses CI_Exceptions::show_error()
121+
* @param string $page Page URI
122+
* @param bool $log_error Whether to log the error
123+
* @return void
124+
* @uses CI_Exceptions::show_error()
116125
*
117-
* @param string $page Page URI
118-
* @param bool $log_error Whether to log the error
119-
* @return void
120126
*/
121-
public function show_404($page = '', $log_error = TRUE)
127+
public function show_404($page = '', $log_error = true)
122128
{
123-
if (is_cli())
124-
{
129+
if (is_cli()) {
125130
$heading = 'Not Found';
126131
$message = 'The controller/method pair you requested was not found.';
127-
}
128-
else
129-
{
132+
} else {
130133
$heading = '404 Page Not Found';
131134
$message = 'The page you requested was not found.';
132135
}
133136

134137
// By default we log this, but allow a dev to skip it
135-
if ($log_error)
136-
{
137-
log_message('error', $heading.': '.$page);
138+
if ($log_error) {
139+
log_message('error', $heading . ': ' . $page);
138140
}
139141

140142
echo $this->show_error($heading, $message, 'error_404', 404);
@@ -149,50 +151,41 @@ public function show_404($page = '', $log_error = TRUE)
149151
* Takes an error message as input (either as a string or an array)
150152
* and displays it using the specified template.
151153
*
152-
* @param string $heading Page heading
153-
* @param string|string[] $message Error message
154-
* @param string $template Template name
155-
* @param int $status_code (default: 500)
154+
* @param string $heading Page heading
155+
* @param string|string[] $message Error message
156+
* @param string $template Template name
157+
* @param int $status_code (default: 500)
156158
*
157-
* @return string Error page output
159+
* @return string Error page output
158160
*/
159161
public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
160162
{
161163
$templates_path = config_item('error_views_path');
162-
if (empty($templates_path))
163-
{
164-
$templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
164+
if (empty($templates_path)) {
165+
$templates_path = VIEWPATH . 'errors' . DIRECTORY_SEPARATOR;
165166
}
166167

167-
if (is_cli())
168-
{
169-
$message = "\t".(is_array($message) ? implode("\n\t", $message) : $message);
170-
$template = 'cli'.DIRECTORY_SEPARATOR.$template;
171-
}
172-
else
173-
{
168+
if (is_cli()) {
169+
$message = "\t" . (is_array($message) ? implode("\n\t", $message) : $message);
170+
$template = 'cli' . DIRECTORY_SEPARATOR . $template;
171+
} else {
174172
set_status_header($status_code);
175-
if (is_array($message))
176-
{
177-
foreach ($message as &$value)
178-
{
173+
if (is_array($message)) {
174+
foreach ($message as &$value) {
179175
$value = htmlspecialchars($value);
180176
}
181-
}
182-
else
183-
{
177+
} else {
184178
$message = htmlspecialchars($message);
185179
}
186-
$message = '<p>'.(is_array($message) ? implode('</p><p>', $message) : $message).'</p>';
187-
$template = 'html'.DIRECTORY_SEPARATOR.$template;
180+
$message = '<p>' . (is_array($message) ? implode('</p><p>', $message) : $message) . '</p>';
181+
$template = 'html' . DIRECTORY_SEPARATOR . $template;
188182
}
189183

190-
if (ob_get_level() > $this->ob_level + 1)
191-
{
184+
if (ob_get_level() > $this->ob_level + 1) {
192185
ob_end_flush();
193186
}
194187
ob_start();
195-
include($templates_path.$template.'.php');
188+
include($templates_path . $template . '.php');
196189
$buffer = ob_get_contents();
197190
ob_end_clean();
198191
return $buffer;
@@ -203,33 +196,27 @@ public function show_error($heading, $message, $template = 'error_general', $sta
203196
public function show_exception($exception)
204197
{
205198
$templates_path = config_item('error_views_path');
206-
if (empty($templates_path))
207-
{
208-
$templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
199+
if (empty($templates_path)) {
200+
$templates_path = VIEWPATH . 'errors' . DIRECTORY_SEPARATOR;
209201
}
210202

211203
$message = $exception->getMessage();
212-
if (empty($message))
213-
{
204+
if (empty($message)) {
214205
$message = '(null)';
215206
}
216207

217-
if (is_cli())
218-
{
219-
$templates_path .= 'cli'.DIRECTORY_SEPARATOR;
220-
}
221-
else
222-
{
223-
$templates_path .= 'html'.DIRECTORY_SEPARATOR;
208+
if (is_cli()) {
209+
$templates_path .= 'cli' . DIRECTORY_SEPARATOR;
210+
} else {
211+
$templates_path .= 'html' . DIRECTORY_SEPARATOR;
224212
}
225213

226-
if (ob_get_level() > $this->ob_level + 1)
227-
{
214+
if (ob_get_level() > $this->ob_level + 1) {
228215
ob_end_flush();
229216
}
230217

231218
ob_start();
232-
include($templates_path.'error_exception.php');
219+
include($templates_path . 'error_exception.php');
233220
$buffer = ob_get_contents();
234221
ob_end_clean();
235222
echo $buffer;
@@ -240,45 +227,39 @@ public function show_exception($exception)
240227
/**
241228
* Native PHP error handler
242229
*
243-
* @param int $severity Error level
244-
* @param string $message Error message
245-
* @param string $filepath File path
246-
* @param int $line Line number
247-
* @return void
230+
* @param int $severity Error level
231+
* @param string $message Error message
232+
* @param string $filepath File path
233+
* @param int $line Line number
234+
* @return void
248235
*/
249236
public function show_php_error($severity, $message, $filepath, $line)
250237
{
251238
$templates_path = config_item('error_views_path');
252-
if (empty($templates_path))
253-
{
254-
$templates_path = VIEWPATH.'errors'.DIRECTORY_SEPARATOR;
239+
if (empty($templates_path)) {
240+
$templates_path = VIEWPATH . 'errors' . DIRECTORY_SEPARATOR;
255241
}
256242

257243
$severity = isset($this->levels[$severity]) ? $this->levels[$severity] : $severity;
258244

259245
// For safety reasons we don't show the full file path in non-CLI requests
260-
if ( ! is_cli())
261-
{
246+
if (!is_cli()) {
262247
$filepath = str_replace('\\', '/', $filepath);
263-
if (FALSE !== strpos($filepath, '/'))
264-
{
248+
if (false !== strpos($filepath, '/')) {
265249
$x = explode('/', $filepath);
266-
$filepath = $x[count($x)-2].'/'.end($x);
250+
$filepath = $x[count($x) - 2] . '/' . end($x);
267251
}
268252

269-
$template = 'html'.DIRECTORY_SEPARATOR.'error_php';
270-
}
271-
else
272-
{
273-
$template = 'cli'.DIRECTORY_SEPARATOR.'error_php';
253+
$template = 'html' . DIRECTORY_SEPARATOR . 'error_php';
254+
} else {
255+
$template = 'cli' . DIRECTORY_SEPARATOR . 'error_php';
274256
}
275257

276-
if (ob_get_level() > $this->ob_level + 1)
277-
{
258+
if (ob_get_level() > $this->ob_level + 1) {
278259
ob_end_flush();
279260
}
280261
ob_start();
281-
include($templates_path.$template.'.php');
262+
include($templates_path . $template . '.php');
282263
$buffer = ob_get_contents();
283264
ob_end_clean();
284265
echo $buffer;

0 commit comments

Comments
 (0)