Conditions | 10 |
Paths | 9 |
Total Lines | 49 |
Code Lines | 32 |
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 |
||
91 | private function createCookie(RequestInterface $request, $setCookie) |
||
92 | { |
||
93 | $parts = array_map('trim', explode(';', $setCookie)); |
||
94 | |||
95 | if (empty($parts) || !strpos($parts[0], '=')) { |
||
96 | return; |
||
97 | } |
||
98 | |||
99 | list($name, $cookieValue) = $this->createValueKey(array_shift($parts)); |
||
100 | |||
101 | $expires = 0; |
||
102 | $domain = $request->getUri()->getHost(); |
||
103 | $path = $request->getUri()->getPath(); |
||
104 | $secure = false; |
||
105 | $httpOnly = false; |
||
106 | |||
107 | // Add the cookie pieces into the parsed data array |
||
108 | foreach ($parts as $part) { |
||
109 | list($key, $value) = $this->createValueKey($part); |
||
110 | |||
111 | switch (strtolower($key)) { |
||
112 | case 'expires': |
||
113 | $expires = \DateTime::createFromFormat(DATE_COOKIE, $value); |
||
114 | break; |
||
115 | |||
116 | case 'max-age': |
||
117 | $expires = (new \DateTime())->add(new \DateInterval('PT'.(int) $value.'S')); |
||
118 | break; |
||
119 | |||
120 | case 'domain': |
||
121 | $domain = $value; |
||
122 | break; |
||
123 | |||
124 | case 'path': |
||
125 | $path = $value; |
||
126 | break; |
||
127 | |||
128 | case 'secure': |
||
129 | $secure = true; |
||
130 | break; |
||
131 | |||
132 | case 'httponly': |
||
133 | $httpOnly = true; |
||
134 | break; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | return new Cookie($name, $cookieValue, $expires, $domain, $path, $secure, $httpOnly); |
||
139 | } |
||
140 | |||
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.