Conditions | 10 |
Paths | 26 |
Total Lines | 41 |
Code Lines | 21 |
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 |
||
78 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
|
|||
79 | { |
||
80 | if (session_status() === PHP_SESSION_DISABLED) { |
||
81 | throw new RuntimeException('PHP sessions are disabled'); |
||
82 | } |
||
83 | |||
84 | if (session_status() === PHP_SESSION_ACTIVE) { |
||
85 | throw new RuntimeException('Failed to start the session: already started by PHP.'); |
||
86 | } |
||
87 | |||
88 | //Session name |
||
89 | $name = $this->name ?: session_name(); |
||
90 | session_name($name); |
||
91 | |||
92 | //Session id |
||
93 | $id = $this->id; |
||
94 | |||
95 | if (empty($id)) { |
||
96 | $cookies = $request->getCookieParams(); |
||
97 | |||
98 | if (!empty($cookies[$name])) { |
||
99 | $id = $cookies[$name]; |
||
100 | } |
||
101 | } |
||
102 | |||
103 | if (!empty($id)) { |
||
104 | session_id($id); |
||
105 | } |
||
106 | |||
107 | session_start(); |
||
108 | |||
109 | $request = self::startStorage($request, isset($_SESSION[self::STORAGE_KEY]) ? $_SESSION[self::STORAGE_KEY] : []); |
||
110 | $response = $next($request, $response); |
||
111 | |||
112 | if ((session_status() === PHP_SESSION_ACTIVE) && (session_name() === $name)) { |
||
113 | $_SESSION[self::STORAGE_KEY] = self::stopStorage($request); |
||
114 | session_write_close(); |
||
115 | } |
||
116 | |||
117 | return $response; |
||
118 | } |
||
119 | } |
||
120 |
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: