Kernel::defineLocale()   C
last analyzed

Complexity

Conditions 14
Paths 12

Size

Total Lines 77
Code Lines 29

Duplication

Lines 18
Ratio 23.38 %

Importance

Changes 0
Metric Value
cc 14
eloc 29
nc 12
nop 0
dl 18
loc 77
rs 5.2115
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php namespace Anomaly\Streams\Platform\Http;
2
3
use Illuminate\Contracts\Foundation\Application;
4
use Illuminate\Routing\Router;
5
6
/**
7
 * Class Kernel
8
 *
9
 * @link   http://pyrocms.com/
10
 * @author PyroCMS, Inc. <[email protected]>
11
 * @author Ryan Thompson <[email protected]>
12
 */
13
class Kernel extends \Illuminate\Foundation\Http\Kernel
14
{
15
16
    /**
17
     * The application's global HTTP middleware stack.
18
     *
19
     * @var array
20
     */
21
    protected $middleware = [
22
        \Illuminate\Cookie\Middleware\EncryptCookies::class,
23
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
24
        \Illuminate\Session\Middleware\StartSession::class,
25
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
26
        \Anomaly\Streams\Platform\Http\Middleware\SetLocale::class,
27
    ];
28
29
    /**
30
     * The application's route middleware.
31
     *
32
     * @var array
33
     */
34
    protected $routeMiddleware = [
35
        'can'        => \Illuminate\Auth\Middleware\Authorize::class,
36
        'auth'       => \Illuminate\Auth\Middleware\Authenticate::class,
37
        'throttle'   => \Illuminate\Routing\Middleware\ThrottleRequests::class,
38
        'bindings'   => \Illuminate\Routing\Middleware\SubstituteBindings::class,
39
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
40
        //'guest' => \Illuminate\Auth\Middleware\RedirectIfAuthenticated::class,
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
    ];
42
43
    /**
44
     * The application's route middleware groups.
45
     *
46
     * @var array
47
     */
48
    protected $middlewareGroups = [
49
        'web' => [
50
            \Illuminate\Cookie\Middleware\EncryptCookies::class,
51
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
52
            \Illuminate\Session\Middleware\StartSession::class,
53
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
54
            \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class,
55
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
56
        ],
57
58
        'api' => [
59
            'throttle:60,1',
60
            'bindings',
61
        ],
62
    ];
63
64
    /**
65
     * Create a new Kernel instance.
66
     *
67
     * @param Application $app
68
     * @param Router $router
69
     */
70
    public function __construct(Application $app, Router $router)
71
    {
72
        $this->defineLocale();
73
74
        $config = require base_path('config/streams.php');
75
76
        $this->middleware         = array_get($config, 'middleware') ?: $this->middleware;
0 ignored issues
show
Documentation Bug introduced by
It seems like array_get($config, 'midd...') ?: $this->middleware of type * is incompatible with the declared type array of property $middleware.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
77
        $this->routeMiddleware    = array_get($config, 'route_middleware') ?: $this->routeMiddleware;
0 ignored issues
show
Documentation Bug introduced by
It seems like array_get($config, 'rout... $this->routeMiddleware of type * is incompatible with the declared type array of property $routeMiddleware.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
78
        $this->middlewareGroups   = array_get($config, 'middleware_groups') ?: $this->middlewareGroups;
0 ignored issues
show
Documentation Bug introduced by
It seems like array_get($config, 'midd...$this->middlewareGroups of type * is incompatible with the declared type array of property $middlewareGroups.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
        $this->middlewarePriority = array_get($config, 'middleware_priority') ?: $this->middlewarePriority;
0 ignored issues
show
Documentation Bug introduced by
It seems like array_get($config, 'midd...his->middlewarePriority of type * is incompatible with the declared type array of property $middlewarePriority.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
81
        parent::__construct($app, $router);
82
    }
83
84
    /**
85
     * Define the locale
86
     * based on our URI.
87
     *
88
     * Huge thanks to @keevitaja for this one.
89
     *
90
     * @link https://github.com/keevitaja/linguist
91
     */
92
    protected function defineLocale()
0 ignored issues
show
Coding Style introduced by
defineLocale uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
93
    {
94
        /*
95
         * Make sure the ORIGINAL_REQUEST_URI is always available
96
         * Overwrite later as necessary
97
         */
98
        $_SERVER['ORIGINAL_REQUEST_URI'] = array_get($_SERVER, 'REQUEST_URI');
99
100
        /*
101
         * First grab the supported i18n locales
102
         * that we should be looking for.
103
         */
104
        $locales = require __DIR__ . '/../../resources/config/locales.php';
105
106
        if (file_exists($override = __DIR__ . '/../../../../../resources/core/config/streams/locales.php')) {
107
            $locales = array_replace_recursive($locales, require $override);
108
        }
109
110
        if (!$hint = array_get($locales, 'hint')) {
111
            return;
112
        }
113
114
        /*
115
         * Check the domain for a locale.
116
         */
117
        $url = parse_url(array_get($_SERVER, 'HTTP_HOST'));
118
119
        if ($url === false) {
120
            throw new \Exception('Malformed URL: ' . $url);
121
        }
122
123
        $host = array_get($url, 'host');
124
125
        $pattern = '/^(' . implode('|', array_keys($locales['supported'])) . ')(\.)./';
126
127
        if ($host && ($hint === 'domain' || $hint === true) && preg_match($pattern, $host, $matches)) {
128
129
            define('LOCALE', $matches[1]);
130
131
            return;
132
        }
133
134
        /*
135
         * Let's first look in the URI
136
         * path for for a locale.
137
         */
138
        $pattern = '/^\/(' . implode('|', array_keys($locales['supported'])) . ')\//';
139
140
        $uri = array_get($_SERVER, 'REQUEST_URI', filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL));
141
142 View Code Duplication
        if (($hint === 'uri' || $hint === true) && preg_match($pattern, $uri, $matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
144
            $_SERVER['ORIGINAL_REQUEST_URI'] = $uri;
145
            $_SERVER['REQUEST_URI']          = preg_replace($pattern, '/', $uri);
146
147
            define('LOCALE', $matches[1]);
148
149
            return;
150
        }
151
152
        /*
153
         * Check if we're on the home page.
154
         */
155
        $pattern = '/^\/(' . implode('|', array_keys($locales['supported'])) . ')$/';
156
157
        $uri = array_get($_SERVER, 'REQUEST_URI', filter_input(INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL));
158
159 View Code Duplication
        if (($hint === 'uri' || $hint === true) && preg_match($pattern, $uri, $matches)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
161
            $_SERVER['ORIGINAL_REQUEST_URI'] = $uri;
162
            $_SERVER['REQUEST_URI']          = preg_replace($pattern, '/', $uri);
163
164
            define('LOCALE', $matches[1]);
165
166
            return;
167
        }
168
    }
169
}
170