<commit_before><?php
/**
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 * @link          https://cakephp.org CakePHP(tm) Project
 * @since         2.0.0
 * @license       https://opensource.org/licenses/mit-license.php MIT License
 */
namespace Cake\Error;

use Cake\Controller\Controller;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Exception\Exception as CakeException;
use Cake\Core\Exception\MissingPluginException;
use Cake\Event\Event;
use Cake\Http\Exception\HttpException;
use Cake\Http\Response;
use Cake\Http\ServerRequestFactory;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use Cake\Utility\Inflector;
use Cake\View\Exception\MissingTemplateException;
use Exception;
use PDOException;
use Psr\Http\Message\ServerRequestInterface;

/**
 * Exception Renderer.
 *
 * Captures and handles all unhandled exceptions. Displays helpful framework errors when debug is true.
 * When debug is false a CakeException will render 404 or 500 errors. If an uncaught exception is thrown
 * and it is a type that ExceptionHandler does not know about it will be treated as a 500 error.
 *
 * ### Implementing application specific exception rendering
 *
 * You can implement application specific exception handling by creating a subclass of
 * ExceptionRenderer and configure it to be the `exceptionRenderer` in config/error.php
 *
 * #### Using a subclass of ExceptionRenderer
 *
 * Using a subclass of ExceptionRenderer gives you full control over how Exceptions are rendered, you
 * can configure your class in your config/app.php.
 */
class ExceptionRenderer implements ExceptionRendererInterface
{

    /**
     * The exception being handled.
     *
     * @var \Exception
     */
    public $error;

    /**
     * Controller instance.
     *
     * @var \Cake\Controller\Controller
     */
    public $controller;

    /**
     * Template to render for Cake\Core\Exception\Exception
     *
     * @var string
     */
    public $template = '';

    /**
     * The method corresponding to the Exception this object is for.
     *
     * @var string
     */
    public $method = '';

    /**
     * If set, this will be request used to create the controller that will render
     * the error.
     *
     * @var \Psr\Http\Message\ServerRequestInterface|null
     */
    protected $request = null;

    /**
     * Creates the controller to perform rendering on the error response.
     * If the error is a Cake\Core\Exception\Exception it will be converted to either a 400 or a 500
     * code error depending on the code used to construct the error.
     *
     * @param \Exception $exception Exception.
     * @param \Psr\Http\Message\ServerRequestInterface $request The request - if this is set it will be used instead of creating a new one
     */
    public function __construct(Exception $exception, ServerRequestInterface $request = null)
    {
        $this->error = $exception;
        $this->request = $request;
        $this->controller = $this->_getController();
    }

    /**
     * Returns the unwrapped exception object in case we are dealing with
     * a PHP 7 Error object
     *
     * @param \Exception $exception The object to unwrap
     * @return \Exception|\Error
     */
    protected function _unwrap($exception)
    {
        return $exception instanceof PHP7ErrorException ? $exception->getError() : $exception;
    }

    /**
     * Get the controller instance to handle the exception.
     * Override this method in subclasses to customize the controller used.
     * This method returns the built in `ErrorController` normally, or if an error is repeated
     * a bare controller will be used.
     *
     * @return \Cake\Controller\Controller
     * @triggers Controller.startup $controller
     */
    protected function _getController()
    {
        $request = $this->request ?: Router::getRequest(true);
        if ($request === null) {
            $request = ServerRequestFactory::fromGlobals();
        }

        $response = new Response();
        $controller = null;

        try {
            $namespace = 'Controller';
            $prefix = $request->getParam('prefix');
            if ($prefix) {
                if (strpos($request->getParam('prefix'), '/') === false) {
                    $namespace .= '/' . Inflector::camelize($prefix);
                } else {
                    $prefixes = array_map(
                        'Cake\Utility\Inflector::camelize',<commit_msg>Fix error reported by phpstan.<commit_after><?php
/**
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 * @link          https://cakephp.org CakePHP(tm) Project
 * @since         2.0.0
 * @license       https://opensource.org/licenses/mit-license.php MIT License
 */
namespace Cake\Error;

use Cake\Controller\Controller;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Exception\Exception as CakeException;
use Cake\Core\Exception\MissingPluginException;
use Cake\Event\Event;
use Cake\Http\Exception\HttpException;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\Http\ServerRequestFactory;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use Cake\Utility\Inflector;
use Cake\View\Exception\MissingTemplateException;
use Exception;
use PDOException;

/**
 * Exception Renderer.
 *
 * Captures and handles all unhandled exceptions. Displays helpful framework errors when debug is true.
 * When debug is false a CakeException will render 404 or 500 errors. If an uncaught exception is thrown
 * and it is a type that ExceptionHandler does not know about it will be treated as a 500 error.
 *
 * ### Implementing application specific exception rendering
 *
 * You can implement application specific exception handling by creating a subclass of
 * ExceptionRenderer and configure it to be the `exceptionRenderer` in config/error.php
 *
 * #### Using a subclass of ExceptionRenderer
 *
 * Using a subclass of ExceptionRenderer gives you full control over how Exceptions are rendered, you
 * can configure your class in your config/app.php.
 */
class ExceptionRenderer implements ExceptionRendererInterface
{

    /**
     * The exception being handled.
     *
     * @var \Exception
     */
    public $error;

    /**
     * Controller instance.
     *
     * @var \Cake\Controller\Controller
     */
    public $controller;

    /**
     * Template to render for Cake\Core\Exception\Exception
     *
     * @var string
     */
    public $template = '';

    /**
     * The method corresponding to the Exception this object is for.
     *
     * @var string
     */
    public $method = '';

    /**
     * If set, this will be request used to create the controller that will render
     * the error.
     *
     * @var \Cake\Http\ServerRequest|null
     */
    protected $request = null;

    /**
     * Creates the controller to perform rendering on the error response.
     * If the error is a Cake\Core\Exception\Exception it will be converted to either a 400 or a 500
     * code error depending on the code used to construct the error.
     *
     * @param \Exception $exception Exception.
     * @param \Cake\Http\ServerRequest $request The request - if this is set it will be used instead of creating a new one
     */
    public function __construct(Exception $exception, ServerRequest $request = null)
    {
        $this->error = $exception;
        $this->request = $request;
        $this->controller = $this->_getController();
    }

    /**
     * Returns the unwrapped exception object in case we are dealing with
     * a PHP 7 Error object
     *
     * @param \Exception $exception The object to unwrap
     * @return \Exception|\Error
     */
    protected function _unwrap($exception)
    {
        return $exception instanceof PHP7ErrorException ? $exception->getError() : $exception;
    }

    /**
     * Get the controller instance to handle the exception.
     * Override this method in subclasses to customize the controller used.
     * This method returns the built in `ErrorController` normally, or if an error is repeated
     * a bare controller will be used.
     *
     * @return \Cake\Controller\Controller
     * @triggers Controller.startup $controller
     */
    protected function _getController()
    {
        $request = $this->request ?: Router::getRequest(true);
        if ($request === null) {
            $request = ServerRequestFactory::fromGlobals();
        }

        $response = new Response();
        $controller = null;

        try {
            $namespace = 'Controller';
            $prefix = $request->getParam('prefix');
            if ($prefix) {
                if (strpos($prefix, '/') === false) {
                    $namespace .= '/' . Inflector::camelize($prefix);
                } else {
                    $prefixes = array_map(
                        'Cake\Utility\Inflector::camelize',
{'fraction_non_alphanumeric': 0.09533743611140086, 'fraction_numerical': 0.004172316678835924, 'mean_word_length': 2.7162790697674417, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3690335', 'n_tokens_mistral': 2562, 'n_tokens_neox': 2449, 'n_words': 889}