• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 12739860967

13 Jan 2025 03:03AM UTC coverage: 84.454%. Remained the same
12739860967

push

github

web-flow
chore: add more trailing commas in more places (#9395)

* Apply to parameters

* Apply to array destructuring

* Apply to match

* Apply for arguments

337 of 397 new or added lines in 117 files covered. (84.89%)

1 existing line in 1 file now uncovered.

20464 of 24231 relevant lines covered (84.45%)

189.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

44.83
/system/Debug/Exceptions.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Debug;
15

16
use CodeIgniter\API\ResponseTrait;
17
use CodeIgniter\Exceptions\HasExitCodeInterface;
18
use CodeIgniter\Exceptions\HTTPExceptionInterface;
19
use CodeIgniter\Exceptions\PageNotFoundException;
20
use CodeIgniter\HTTP\Exceptions\HTTPException;
21
use CodeIgniter\HTTP\RequestInterface;
22
use CodeIgniter\HTTP\ResponseInterface;
23
use Config\Exceptions as ExceptionsConfig;
24
use Config\Paths;
25
use ErrorException;
26
use Psr\Log\LogLevel;
27
use Throwable;
28

29
/**
30
 * Exceptions manager
31
 *
32
 * @see \CodeIgniter\Debug\ExceptionsTest
33
 */
34
class Exceptions
35
{
36
    use ResponseTrait;
37

38
    /**
39
     * Nesting level of the output buffering mechanism
40
     *
41
     * @var int
42
     *
43
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
44
     */
45
    public $ob_level;
46

47
    /**
48
     * The path to the directory containing the
49
     * cli and html error view directories.
50
     *
51
     * @var string
52
     *
53
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
54
     */
55
    protected $viewPath;
56

57
    /**
58
     * Config for debug exceptions.
59
     *
60
     * @var ExceptionsConfig
61
     */
62
    protected $config;
63

64
    /**
65
     * The request.
66
     *
67
     * @var RequestInterface|null
68
     */
69
    protected $request;
70

71
    /**
72
     * The outgoing response.
73
     *
74
     * @var ResponseInterface
75
     */
76
    protected $response;
77

78
    private ?Throwable $exceptionCaughtByExceptionHandler = null;
79

80
    public function __construct(ExceptionsConfig $config)
81
    {
82
        // For backward compatibility
83
        $this->ob_level = ob_get_level();
12✔
84
        $this->viewPath = rtrim($config->errorViewPath, '\\/ ') . DIRECTORY_SEPARATOR;
12✔
85

86
        $this->config = $config;
12✔
87

88
        // workaround for upgraded users
89
        // This causes "Deprecated: Creation of dynamic property" in PHP 8.2.
90
        // @TODO remove this after dropping PHP 8.1 support.
91
        if (! isset($this->config->sensitiveDataInTrace)) {
12✔
92
            $this->config->sensitiveDataInTrace = [];
×
93
        }
94
        if (! isset($this->config->logDeprecations, $this->config->deprecationLogLevel)) {
12✔
95
            $this->config->logDeprecations     = false;
×
96
            $this->config->deprecationLogLevel = LogLevel::WARNING;
×
97
        }
98
    }
99

100
    /**
101
     * Responsible for registering the error, exception and shutdown
102
     * handling of our application.
103
     *
104
     * @codeCoverageIgnore
105
     *
106
     * @return void
107
     */
108
    public function initialize()
109
    {
110
        set_exception_handler($this->exceptionHandler(...));
3✔
111
        set_error_handler($this->errorHandler(...));
3✔
112
        register_shutdown_function([$this, 'shutdownHandler']);
3✔
113
    }
114

115
    /**
116
     * Catches any uncaught errors and exceptions, including most Fatal errors
117
     * (Yay PHP7!). Will log the error, display it if display_errors is on,
118
     * and fire an event that allows custom actions to be taken at this point.
119
     *
120
     * @return void
121
     */
122
    public function exceptionHandler(Throwable $exception)
123
    {
124
        $this->exceptionCaughtByExceptionHandler = $exception;
×
125

126
        [$statusCode, $exitCode] = $this->determineCodes($exception);
×
127

128
        $this->request = service('request');
×
129

130
        if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes, true)) {
×
131
            $uri       = $this->request->getPath() === '' ? '/' : $this->request->getPath();
×
132
            $routeInfo = '[Method: ' . $this->request->getMethod() . ', Route: ' . $uri . ']';
×
133

134
            log_message('critical', $exception::class . ": {message}\n{routeInfo}\nin {exFile} on line {exLine}.\n{trace}", [
×
135
                'message'   => $exception->getMessage(),
×
136
                'routeInfo' => $routeInfo,
×
137
                'exFile'    => clean_path($exception->getFile()), // {file} refers to THIS file
×
138
                'exLine'    => $exception->getLine(), // {line} refers to THIS line
×
139
                'trace'     => self::renderBacktrace($exception->getTrace()),
×
140
            ]);
×
141

142
            // Get the first exception.
143
            $last = $exception;
×
144

145
            while ($prevException = $last->getPrevious()) {
×
146
                $last = $prevException;
×
147

148
                log_message('critical', '[Caused by] ' . $prevException::class . ": {message}\nin {exFile} on line {exLine}.\n{trace}", [
×
149
                    'message' => $prevException->getMessage(),
×
150
                    'exFile'  => clean_path($prevException->getFile()), // {file} refers to THIS file
×
151
                    'exLine'  => $prevException->getLine(), // {line} refers to THIS line
×
152
                    'trace'   => self::renderBacktrace($prevException->getTrace()),
×
153
                ]);
×
154
            }
155
        }
156

157
        $this->response = service('response');
×
158

159
        if (method_exists($this->config, 'handler')) {
×
160
            // Use new ExceptionHandler
161
            $handler = $this->config->handler($statusCode, $exception);
×
162
            $handler->handle(
×
163
                $exception,
×
164
                $this->request,
×
165
                $this->response,
×
166
                $statusCode,
×
NEW
167
                $exitCode,
×
168
            );
×
169

170
            return;
×
171
        }
172

173
        // For backward compatibility
174
        if (! is_cli()) {
×
175
            try {
176
                $this->response->setStatusCode($statusCode);
×
177
            } catch (HTTPException) {
×
178
                // Workaround for invalid HTTP status code.
179
                $statusCode = 500;
×
180
                $this->response->setStatusCode($statusCode);
×
181
            }
182

183
            if (! headers_sent()) {
×
184
                header(sprintf('HTTP/%s %s %s', $this->request->getProtocolVersion(), $this->response->getStatusCode(), $this->response->getReasonPhrase()), true, $statusCode);
×
185
            }
186

187
            if (! str_contains($this->request->getHeaderLine('accept'), 'text/html')) {
×
188
                $this->respond(ENVIRONMENT === 'development' ? $this->collectVars($exception, $statusCode) : '', $statusCode)->send();
×
189

190
                exit($exitCode);
×
191
            }
192
        }
193

194
        $this->render($exception, $statusCode);
×
195

196
        exit($exitCode);
×
197
    }
198

199
    /**
200
     * The callback to be registered to `set_error_handler()`.
201
     *
202
     * @return bool
203
     *
204
     * @throws ErrorException
205
     *
206
     * @codeCoverageIgnore
207
     */
208
    public function errorHandler(int $severity, string $message, ?string $file = null, ?int $line = null)
209
    {
210
        if ($this->isDeprecationError($severity)) {
77✔
211
            if (! $this->config->logDeprecations || (bool) env('CODEIGNITER_SCREAM_DEPRECATIONS')) {
2✔
212
                throw new ErrorException($message, 0, $severity, $file, $line);
×
213
            }
214

215
            return $this->handleDeprecationError($message, $file, $line);
2✔
216
        }
217

218
        if ((error_reporting() & $severity) !== 0) {
75✔
219
            throw new ErrorException($message, 0, $severity, $file, $line);
44✔
220
        }
221

222
        return false; // return false to propagate the error to PHP standard error handler
31✔
223
    }
224

225
    /**
226
     * Checks to see if any errors have happened during shutdown that
227
     * need to be caught and handle them.
228
     *
229
     * @codeCoverageIgnore
230
     *
231
     * @return void
232
     */
233
    public function shutdownHandler()
234
    {
235
        $error = error_get_last();
×
236

237
        if ($error === null) {
×
238
            return;
×
239
        }
240

241
        ['type' => $type, 'message' => $message, 'file' => $file, 'line' => $line] = $error;
×
242

243
        if ($this->exceptionCaughtByExceptionHandler instanceof Throwable) {
×
244
            $message .= "\n【Previous Exception】\n"
×
245
                . $this->exceptionCaughtByExceptionHandler::class . "\n"
×
246
                . $this->exceptionCaughtByExceptionHandler->getMessage() . "\n"
×
247
                . $this->exceptionCaughtByExceptionHandler->getTraceAsString();
×
248
        }
249

250
        if (in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE], true)) {
×
251
            $this->exceptionHandler(new ErrorException($message, 0, $type, $file, $line));
×
252
        }
253
    }
254

255
    /**
256
     * Determines the view to display based on the exception thrown,
257
     * whether an HTTP or CLI request, etc.
258
     *
259
     * @return string The path and filename of the view file to use
260
     *
261
     * @deprecated 4.4.0 No longer used. Moved to ExceptionHandler.
262
     */
263
    protected function determineView(Throwable $exception, string $templatePath): string
264
    {
265
        // Production environments should have a custom exception file.
266
        $view         = 'production.php';
1✔
267
        $templatePath = rtrim($templatePath, '\\/ ') . DIRECTORY_SEPARATOR;
1✔
268

269
        if (
270
            in_array(
1✔
271
                strtolower(ini_get('display_errors')),
1✔
272
                ['1', 'true', 'on', 'yes'],
1✔
273
                true,
1✔
274
            )
1✔
275
        ) {
276
            $view = 'error_exception.php';
1✔
277
        }
278

279
        // 404 Errors
280
        if ($exception instanceof PageNotFoundException) {
1✔
281
            return 'error_404.php';
1✔
282
        }
283

284
        // Allow for custom views based upon the status code
285
        if (is_file($templatePath . 'error_' . $exception->getCode() . '.php')) {
1✔
286
            return 'error_' . $exception->getCode() . '.php';
1✔
287
        }
288

289
        return $view;
1✔
290
    }
291

292
    /**
293
     * Given an exception and status code will display the error to the client.
294
     *
295
     * @return void
296
     *
297
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
298
     */
299
    protected function render(Throwable $exception, int $statusCode)
300
    {
301
        // Determine possible directories of error views
302
        $path    = $this->viewPath;
×
303
        $altPath = rtrim((new Paths())->viewDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'errors' . DIRECTORY_SEPARATOR;
×
304

305
        $path    .= (is_cli() ? 'cli' : 'html') . DIRECTORY_SEPARATOR;
×
306
        $altPath .= (is_cli() ? 'cli' : 'html') . DIRECTORY_SEPARATOR;
×
307

308
        // Determine the views
309
        $view    = $this->determineView($exception, $path);
×
310
        $altView = $this->determineView($exception, $altPath);
×
311

312
        // Check if the view exists
313
        if (is_file($path . $view)) {
×
314
            $viewFile = $path . $view;
×
315
        } elseif (is_file($altPath . $altView)) {
×
316
            $viewFile = $altPath . $altView;
×
317
        }
318

319
        if (! isset($viewFile)) {
×
320
            echo 'The error view files were not found. Cannot render exception trace.';
×
321

322
            exit(1);
×
323
        }
324

325
        echo (function () use ($exception, $statusCode, $viewFile): string {
×
326
            $vars = $this->collectVars($exception, $statusCode);
×
327
            extract($vars, EXTR_SKIP);
×
328

329
            ob_start();
×
330
            include $viewFile;
×
331

332
            return ob_get_clean();
×
333
        })();
×
334
    }
335

336
    /**
337
     * Gathers the variables that will be made available to the view.
338
     *
339
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
340
     */
341
    protected function collectVars(Throwable $exception, int $statusCode): array
342
    {
343
        // Get the first exception.
344
        $firstException = $exception;
1✔
345

346
        while ($prevException = $firstException->getPrevious()) {
1✔
347
            $firstException = $prevException;
×
348
        }
349

350
        $trace = $firstException->getTrace();
1✔
351

352
        if ($this->config->sensitiveDataInTrace !== []) {
1✔
353
            $trace = $this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);
×
354
        }
355

356
        return [
1✔
357
            'title'   => $exception::class,
1✔
358
            'type'    => $exception::class,
1✔
359
            'code'    => $statusCode,
1✔
360
            'message' => $exception->getMessage(),
1✔
361
            'file'    => $exception->getFile(),
1✔
362
            'line'    => $exception->getLine(),
1✔
363
            'trace'   => $trace,
1✔
364
        ];
1✔
365
    }
366

367
    /**
368
     * Mask sensitive data in the trace.
369
     *
370
     * @param array $trace
371
     *
372
     * @return array
373
     *
374
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
375
     */
376
    protected function maskSensitiveData($trace, array $keysToMask, string $path = '')
377
    {
378
        foreach ($trace as $i => $line) {
2✔
379
            $trace[$i]['args'] = $this->maskData($line['args'], $keysToMask);
2✔
380
        }
381

382
        return $trace;
2✔
383
    }
384

385
    /**
386
     * @param array|object $args
387
     *
388
     * @return array|object
389
     *
390
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
391
     */
392
    private function maskData($args, array $keysToMask, string $path = '')
393
    {
394
        foreach ($keysToMask as $keyToMask) {
2✔
395
            $explode = explode('/', $keyToMask);
2✔
396
            $index   = end($explode);
2✔
397

398
            if (str_starts_with(strrev($path . '/' . $index), strrev($keyToMask))) {
2✔
399
                if (is_array($args) && array_key_exists($index, $args)) {
2✔
400
                    $args[$index] = '******************';
1✔
401
                } elseif (
402
                    is_object($args) && property_exists($args, $index)
2✔
403
                    && isset($args->{$index}) && is_scalar($args->{$index})
2✔
404
                ) {
405
                    $args->{$index} = '******************';
1✔
406
                }
407
            }
408
        }
409

410
        if (is_array($args)) {
2✔
411
            foreach ($args as $pathKey => $subarray) {
2✔
412
                $args[$pathKey] = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
1✔
413
            }
414
        } elseif (is_object($args)) {
1✔
415
            foreach ($args as $pathKey => $subarray) {
1✔
416
                $args->{$pathKey} = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
1✔
417
            }
418
        }
419

420
        return $args;
2✔
421
    }
422

423
    /**
424
     * Determines the HTTP status code and the exit status code for this request.
425
     */
426
    protected function determineCodes(Throwable $exception): array
427
    {
428
        $statusCode = 500;
1✔
429
        $exitStatus = EXIT_ERROR;
1✔
430

431
        if ($exception instanceof HTTPExceptionInterface) {
1✔
432
            $statusCode = $exception->getCode();
×
433
        }
434

435
        if ($exception instanceof HasExitCodeInterface) {
1✔
436
            $exitStatus = $exception->getExitCode();
1✔
437
        }
438

439
        return [$statusCode, $exitStatus];
1✔
440
    }
441

442
    private function isDeprecationError(int $error): bool
443
    {
444
        $deprecations = E_DEPRECATED | E_USER_DEPRECATED;
77✔
445

446
        return ($error & $deprecations) !== 0;
77✔
447
    }
448

449
    /**
450
     * @return true
451
     */
452
    private function handleDeprecationError(string $message, ?string $file = null, ?int $line = null): bool
453
    {
454
        // Remove the trace of the error handler.
455
        $trace = array_slice(debug_backtrace(), 2);
2✔
456

457
        log_message(
2✔
458
            $this->config->deprecationLogLevel,
2✔
459
            "[DEPRECATED] {message} in {errFile} on line {errLine}.\n{trace}",
2✔
460
            [
2✔
461
                'message' => $message,
2✔
462
                'errFile' => clean_path($file ?? ''),
2✔
463
                'errLine' => $line ?? 0,
2✔
464
                'trace'   => self::renderBacktrace($trace),
2✔
465
            ],
2✔
466
        );
2✔
467

468
        return true;
2✔
469
    }
470

471
    // --------------------------------------------------------------------
472
    // Display Methods
473
    // --------------------------------------------------------------------
474

475
    /**
476
     * This makes nicer looking paths for the error output.
477
     *
478
     * @deprecated Use dedicated `clean_path()` function.
479
     */
480
    public static function cleanPath(string $file): string
481
    {
482
        return match (true) {
483
            str_starts_with($file, APPPATH)                             => 'APPPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(APPPATH)),
×
484
            str_starts_with($file, SYSTEMPATH)                          => 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(SYSTEMPATH)),
×
485
            str_starts_with($file, FCPATH)                              => 'FCPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(FCPATH)),
×
486
            defined('VENDORPATH') && str_starts_with($file, VENDORPATH) => 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(VENDORPATH)),
×
487
            default                                                     => $file,
×
488
        };
489
    }
490

491
    /**
492
     * Describes memory usage in real-world units. Intended for use
493
     * with memory_get_usage, etc.
494
     *
495
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
496
     */
497
    public static function describeMemory(int $bytes): string
498
    {
499
        if ($bytes < 1024) {
×
500
            return $bytes . 'B';
×
501
        }
502

503
        if ($bytes < 1_048_576) {
×
504
            return round($bytes / 1024, 2) . 'KB';
×
505
        }
506

507
        return round($bytes / 1_048_576, 2) . 'MB';
×
508
    }
509

510
    /**
511
     * Creates a syntax-highlighted version of a PHP file.
512
     *
513
     * @return bool|string
514
     *
515
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
516
     */
517
    public static function highlightFile(string $file, int $lineNumber, int $lines = 15)
518
    {
519
        if ($file === '' || ! is_readable($file)) {
×
520
            return false;
×
521
        }
522

523
        // Set our highlight colors:
524
        if (function_exists('ini_set')) {
×
525
            ini_set('highlight.comment', '#767a7e; font-style: italic');
×
526
            ini_set('highlight.default', '#c7c7c7');
×
527
            ini_set('highlight.html', '#06B');
×
528
            ini_set('highlight.keyword', '#f1ce61;');
×
529
            ini_set('highlight.string', '#869d6a');
×
530
        }
531

532
        try {
533
            $source = file_get_contents($file);
×
534
        } catch (Throwable) {
×
535
            return false;
×
536
        }
537

538
        $source = str_replace(["\r\n", "\r"], "\n", $source);
×
539
        $source = explode("\n", highlight_string($source, true));
×
540
        $source = str_replace('<br />', "\n", $source[1]);
×
541
        $source = explode("\n", str_replace("\r\n", "\n", $source));
×
542

543
        // Get just the part to show
544
        $start = max($lineNumber - (int) round($lines / 2), 0);
×
545

546
        // Get just the lines we need to display, while keeping line numbers...
547
        $source = array_splice($source, $start, $lines, true);
×
548

549
        // Used to format the line number in the source
550
        $format = '% ' . strlen((string) ($start + $lines)) . 'd';
×
551

552
        $out = '';
×
553
        // Because the highlighting may have an uneven number
554
        // of open and close span tags on one line, we need
555
        // to ensure we can close them all to get the lines
556
        // showing correctly.
557
        $spans = 1;
×
558

559
        foreach ($source as $n => $row) {
×
560
            $spans += substr_count($row, '<span') - substr_count($row, '</span');
×
561
            $row = str_replace(["\r", "\n"], ['', ''], $row);
×
562

563
            if (($n + $start + 1) === $lineNumber) {
×
564
                preg_match_all('#<[^>]+>#', $row, $tags);
×
565

566
                $out .= sprintf(
×
567
                    "<span class='line highlight'><span class='number'>{$format}</span> %s\n</span>%s",
×
568
                    $n + $start + 1,
×
569
                    strip_tags($row),
×
NEW
570
                    implode('', $tags[0]),
×
571
                );
×
572
            } else {
573
                $out .= sprintf('<span class="line"><span class="number">' . $format . '</span> %s', $n + $start + 1, $row) . "\n";
×
574
            }
575
        }
576

577
        if ($spans > 0) {
×
578
            $out .= str_repeat('</span>', $spans);
×
579
        }
580

581
        return '<pre><code>' . $out . '</code></pre>';
×
582
    }
583

584
    private static function renderBacktrace(array $backtrace): string
585
    {
586
        $backtraces = [];
3✔
587

588
        foreach ($backtrace as $index => $trace) {
3✔
589
            $frame = $trace + ['file' => '[internal function]', 'line' => '', 'class' => '', 'type' => '', 'args' => []];
3✔
590

591
            if ($frame['file'] !== '[internal function]') {
3✔
592
                $frame['file'] = sprintf('%s(%s)', $frame['file'], $frame['line']);
3✔
593
            }
594

595
            unset($frame['line']);
3✔
596
            $idx = $index;
3✔
597
            $idx = str_pad((string) ++$idx, 2, ' ', STR_PAD_LEFT);
3✔
598

599
            $args = implode(', ', array_map(static fn ($value): string => match (true) {
3✔
600
                is_object($value)   => sprintf('Object(%s)', $value::class),
3✔
601
                is_array($value)    => $value !== [] ? '[...]' : '[]',
3✔
602
                $value === null     => 'null',
3✔
603
                is_resource($value) => sprintf('resource (%s)', get_resource_type($value)),
3✔
604
                default             => var_export($value, true),
3✔
605
            }, $frame['args']));
3✔
606

607
            $backtraces[] = sprintf(
3✔
608
                '%s %s: %s%s%s(%s)',
3✔
609
                $idx,
3✔
610
                clean_path($frame['file']),
3✔
611
                $frame['class'],
3✔
612
                $frame['type'],
3✔
613
                $frame['function'],
3✔
614
                $args,
3✔
615
            );
3✔
616
        }
617

618
        return implode("\n", $backtraces);
3✔
619
    }
620
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc