Conditions | 10 |
Paths | 16 |
Total Lines | 67 |
Code Lines | 41 |
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 |
||
64 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null) |
||
65 | { |
||
66 | $requestMethod = $request->getMethod(); |
||
67 | $key = str_replace(['\\', '/', ':', '*', '?', '"', '<', '>', '|'], '_', $request->getUri()->getPath().'?'.$request->getUri()->getQuery()); |
||
68 | |||
69 | if ($this->cacheCondition->isOk() && $requestMethod == 'GET') { |
||
70 | $cacheResponse = $this->cache->get($key); |
||
71 | if ($cacheResponse) { |
||
72 | $this->log->debug("Cache HIT on $key"); |
||
73 | |||
74 | return $cacheResponse; |
||
75 | } else { |
||
76 | $this->log->debug("Cache MISS on key $key"); |
||
77 | $response = $out($request, $response); |
||
78 | |||
79 | $noCache = false; |
||
80 | if ($response->hasHeader('Mouf-Cache-Control') && $response->getHeader('Mouf-Cache-Control')[0] == 'no-cache') { |
||
81 | $noCache = true; |
||
82 | } |
||
83 | |||
84 | if ($noCache) { |
||
85 | $this->log->debug("Mouf NO CACHE header found, not storing '$key'"); |
||
86 | } else { |
||
87 | $ttl = null; |
||
88 | |||
89 | // TODO: continue here! |
||
90 | // Use PSR-7 response to analyze maxage and expires... |
||
91 | // ...or... use a completely different HTTP cache implementation!!! |
||
92 | // There must be one around for PSR-7! |
||
93 | |||
94 | $maxAge = $response->getMaxAge(); |
||
95 | $expires = $response->getExpires(); |
||
96 | if ($maxAge) { |
||
97 | $this->log->debug("MaxAge specified : $maxAge"); |
||
98 | $ttl = $maxAge; |
||
99 | } elseif ($expires) { |
||
100 | $this->log->debug("Expires specified : $expires"); |
||
101 | $ttl = date_diff($expires, new \DateTime())->s; |
||
102 | } |
||
103 | |||
104 | if ($ttl) { |
||
105 | $this->log->debug("TTL is : $ttl"); |
||
106 | } |
||
107 | |||
108 | // Make sure the response is serializable |
||
109 | $serializableResponse = new Response(); |
||
110 | $serializableResponse->headers = $response->headers; |
||
111 | |||
112 | ob_start(); |
||
113 | $response->sendContent(); |
||
114 | $content = ob_get_clean(); |
||
115 | |||
116 | $serializableResponse->setContent($content); |
||
117 | |||
118 | $this->cache->set($key, $serializableResponse, $ttl); |
||
119 | $this->log->debug("Cache STORED on key $key"); |
||
120 | $response = $serializableResponse; |
||
121 | } |
||
122 | |||
123 | return $response; |
||
124 | } |
||
125 | } else { |
||
126 | $this->log->debug("No cache for $key"); |
||
127 | |||
128 | return $this->fallBackRouter->handle($request, $type, $catch); |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: