Conditions | 11 |
Paths | 6 |
Total Lines | 46 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
36 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
||
37 | { |
||
38 | foreach ($this->cookieJar->getCookies() as $cookie) { |
||
39 | if ($cookie->isExpired()) { |
||
40 | continue; |
||
41 | } |
||
42 | |||
43 | if (!$cookie->matchDomain($request->getUri()->getHost())) { |
||
44 | continue; |
||
45 | } |
||
46 | |||
47 | if (!$cookie->matchPath($request->getUri()->getPath())) { |
||
48 | continue; |
||
49 | } |
||
50 | |||
51 | if ($cookie->isSecure() && ($request->getUri()->getScheme() !== 'https')) { |
||
52 | continue; |
||
53 | } |
||
54 | |||
55 | $request = $request->withAddedHeader('Cookie', sprintf('%s=%s', $cookie->getName(), $cookie->getValue())); |
||
56 | } |
||
57 | |||
58 | return $next($request)->then(function (ResponseInterface $response) use ($request) { |
||
59 | if ($response->hasHeader('Set-Cookie')) { |
||
60 | $setCookies = $response->getHeader('Set-Cookie'); |
||
61 | |||
62 | foreach ($setCookies as $setCookie) { |
||
63 | $cookie = $this->createCookie($request, $setCookie); |
||
64 | |||
65 | // Cookie invalid do not use it |
||
66 | if (null === $cookie) { |
||
67 | continue; |
||
68 | } |
||
69 | |||
70 | // Restrict setting cookie from another domain |
||
71 | if (false === strpos($cookie->getDomain(), $request->getUri()->getHost())) { |
||
72 | continue; |
||
73 | } |
||
74 | |||
75 | $this->cookieJar->addCookie($cookie); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | return $response; |
||
80 | }); |
||
81 | } |
||
82 | |||
157 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.