Conditions | 4 |
Paths | 3 |
Total Lines | 56 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
23 | public function __invoke(Request $req, Response $res, callable $next) |
||
|
|||
24 | { |
||
25 | $config = $this->app['config']; |
||
26 | if (!$config->get('sessions.enabled') || $req->isApi()) { |
||
27 | return $next($req, $res); |
||
28 | } |
||
29 | |||
30 | $lifetime = $config->get('sessions.lifetime'); |
||
31 | $hostname = $config->get('app.hostname'); |
||
32 | ini_set('session.use_trans_sid', false); |
||
33 | ini_set('session.use_only_cookies', true); |
||
34 | ini_set('url_rewriter.tags', ''); |
||
35 | ini_set('session.gc_maxlifetime', $lifetime); |
||
36 | |||
37 | // set the session name |
||
38 | $sessionTitle = $config->get('app.title').'-'.$hostname; |
||
39 | $safeSessionTitle = str_replace(['.', ' ', "'", '"'], ['', '_', '', ''], $sessionTitle); |
||
40 | session_name($safeSessionTitle); |
||
41 | |||
42 | // set the session cookie parameters |
||
43 | session_set_cookie_params( |
||
44 | $lifetime, // lifetime |
||
45 | '/', // path |
||
46 | '.'.$hostname, // domain |
||
47 | $req->isSecure(), // secure |
||
48 | true // http only |
||
49 | ); |
||
50 | |||
51 | // register session_write_close as a shutdown function |
||
52 | session_register_shutdown(); |
||
53 | |||
54 | // install any custom session handlers |
||
55 | $class = $config->get('sessions.driver'); |
||
56 | if ($class) { |
||
57 | $handler = new $class($this->app); |
||
58 | $handler::registerHandler($handler); |
||
59 | } |
||
60 | |||
61 | session_start(); |
||
62 | |||
63 | // fix the session cookie |
||
64 | Utility::setCookieFixDomain( |
||
65 | session_name(), |
||
66 | session_id(), |
||
67 | time() + $lifetime, |
||
68 | '/', |
||
69 | $hostname, |
||
70 | $req->isSecure(), |
||
71 | true |
||
72 | ); |
||
73 | |||
74 | // make the newly started session in our request |
||
75 | $req->setSession($_SESSION); |
||
76 | |||
77 | return $next($req, $res); |
||
78 | } |
||
79 | } |
||
80 |
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: