Passed
Branch feature/6.x (3df42d)
by Schlaefer
08:49
created

PagesController::display()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 17
c 2
b 0
f 0
nc 14
nop 1
dl 0
loc 28
rs 8.4444
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice.
11
 *
12
 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
13
 * @link      https://cakephp.org CakePHP(tm) Project
14
 * @since     0.2.9
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 */
17
namespace App\Controller;
18
19
use Cake\Core\Configure;
20
use Cake\Http\Exception\ForbiddenException;
21
use Cake\Http\Exception\NotFoundException;
22
use Cake\Http\Response;
23
use Cake\View\Exception\MissingTemplateException;
24
25
/**
26
 * Static content controller
27
 *
28
 * This controller will render views from templates/Pages/
29
 *
30
 * @link https://book.cakephp.org/4/en/controllers/pages-controller.html
31
 */
32
class PagesController extends AppController
33
{
34
    /**
35
     * Displays a view
36
     *
37
     * @param array ...$path Path segments.
38
     * @return \Cake\Http\Response|null
39
     * @throws \Cake\Http\Exception\ForbiddenException When a directory traversal attempt.
40
     * @throws \Cake\View\Exception\MissingTemplateException When the view file could not
41
     *   be found and in debug mode.
42
     * @throws \Cake\Http\Exception\NotFoundException When the view file could not
43
     *   be found and not in debug mode.
44
     * @throws \Cake\View\Exception\MissingTemplateException In debug mode.
45
     */
46
    public function display(...$path): ?Response
47
    {
48
        if (!$path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $path of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
49
            return $this->redirect('/');
50
        }
51
        if (in_array('..', $path, true) || in_array('.', $path, true)) {
52
            throw new ForbiddenException();
53
        }
54
        $page = $subpage = null;
55
56
        if (!empty($path[0])) {
57
            $page = $path[0];
58
        }
59
        if (!empty($path[1])) {
60
            $subpage = $path[1];
61
        }
62
        $this->set(compact('page', 'subpage'));
63
64
        try {
65
            return $this->render(implode('/', $path));
66
        } catch (MissingTemplateException $exception) {
67
            if (Configure::read('debug')) {
68
                throw $exception;
69
            }
70
            throw new NotFoundException();
71
        }
72
73
        return $this->render();
0 ignored issues
show
Unused Code introduced by
return $this->render() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
74
    }
75
}