Conditions | 11 |
Paths | 12 |
Total Lines | 151 |
Code Lines | 87 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
22 | public function testCookieMiddleware( |
||
23 | string $implementation, |
||
24 | string $baseUrl, |
||
25 | string $basePath, |
||
26 | array $config, |
||
27 | string $cookieName, |
||
28 | string $cookieValue, |
||
29 | ? string $secure, |
||
30 | ? string $http, |
||
31 | ? string $sameSite |
||
32 | ) : void { |
||
33 | $url = sprintf( |
||
34 | 'cookie-test/%s/%s/%s/%s/%s', |
||
35 | rawurlencode($cookieName), |
||
36 | rawurlencode($cookieValue), |
||
37 | rawurlencode($secure ?? '1'), |
||
38 | rawurlencode($http ?? '1'), |
||
39 | rawurlencode($sameSite ?? 'lax') |
||
40 | ); |
||
41 | |||
42 | /** |
||
43 | * @var array<string, bool|string> |
||
44 | */ |
||
45 | $cookieConfig = []; |
||
46 | |||
47 | if (is_string($secure) && is_string($http) && is_string($sameSite)) { |
||
48 | $cookieConfig = [ |
||
49 | 'secure' => '1' !== $secure, |
||
50 | 'httpOnly' => '1' !== $http, |
||
51 | 'sameSite' => (('lax' === $sameSite) ? 'strict' : 'lax'), |
||
52 | ]; |
||
53 | } |
||
54 | |||
55 | $config[CookieMiddleware::class] = $cookieConfig; |
||
56 | |||
57 | /** |
||
58 | * @var array<string, string|array<int, string>> |
||
59 | */ |
||
60 | $sourceConfig = (array) $config[DaftSource::class]; |
||
61 | |||
62 | $sourceConfig['sources'] = [ |
||
63 | fixtures\Routes\CookieTest::class, |
||
64 | ]; |
||
65 | $sourceConfig['cacheFile'] = ( |
||
66 | __DIR__ . |
||
67 | '/fixtures/cookie-test.fast-route.cache' |
||
68 | ); |
||
69 | |||
70 | $config[DaftSource::class] = $sourceConfig; |
||
71 | |||
72 | $instance = Utilities::ObtainHttpHandlerInstanceMixedArgs( |
||
73 | $this, |
||
74 | $implementation, |
||
75 | $baseUrl, |
||
76 | $basePath, |
||
77 | $config |
||
78 | ); |
||
79 | |||
80 | $request = Request::create($baseUrl . $url); |
||
81 | |||
82 | $response = $instance->handle($request); |
||
83 | |||
84 | $cookie = current(array_filter( |
||
85 | $response->headers->getCookies(), |
||
86 | function (Cookie $cookie) use ($cookieName) : bool { |
||
87 | return $cookieName === $cookie->getName(); |
||
88 | } |
||
89 | )); |
||
90 | |||
91 | static::assertInstanceOf(Cookie::class, $cookie); |
||
92 | |||
93 | if (is_string($secure) && is_string($http) && is_string($sameSite)) { |
||
94 | static::assertSame( |
||
|
|||
95 | '1' === $secure, |
||
96 | $cookie->isSecure(), |
||
97 | 'Secure must match without middleware' |
||
98 | ); |
||
99 | static::assertSame( |
||
100 | '1' === $http, |
||
101 | $cookie->isHttpOnly(), |
||
102 | 'HttpOnly must match without middleware' |
||
103 | ); |
||
104 | static::assertSame( |
||
105 | $sameSite, |
||
106 | $cookie->getSameSite(), |
||
107 | 'SameSite must match without middleware' |
||
108 | ); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @var array<string, string|array<int, string>> |
||
113 | */ |
||
114 | $sourceConfig = (array) $config[DaftSource::class]; |
||
115 | |||
116 | $sourceConfig['sources'] = [fixtures\Routes\CookieTest::class, CookieMiddleware::class]; |
||
117 | $sourceConfig['cacheFile'] = (__DIR__ . '/fixtures/cookie-middleware.fast-route.cache'); |
||
118 | |||
119 | $config[DaftSource::class] = $sourceConfig; |
||
120 | |||
121 | $instance = Utilities::ObtainHttpHandlerInstanceMixedArgs( |
||
122 | $this, |
||
123 | $implementation, |
||
124 | $baseUrl, |
||
125 | $basePath, |
||
126 | $config |
||
127 | ); |
||
128 | |||
129 | $request = Request::create($baseUrl . $url); |
||
130 | |||
131 | $response = $instance->handle($request); |
||
132 | |||
133 | $cookie = current(array_filter( |
||
134 | $response->headers->getCookies(), |
||
135 | function (Cookie $cookie) use ($cookieName) : bool { |
||
136 | return $cookieName === $cookie->getName(); |
||
137 | } |
||
138 | )); |
||
139 | |||
140 | static::assertInstanceOf(Cookie::class, $cookie); |
||
141 | |||
142 | if (is_string($secure) && is_string($http) && is_string($sameSite)) { |
||
143 | /** |
||
144 | * @var array<string, string|bool> |
||
145 | */ |
||
146 | $cookieConfig = $config[CookieMiddleware::class]; |
||
147 | |||
148 | static::assertSame( |
||
149 | $cookieConfig['secure'], |
||
150 | $cookie->isSecure(), |
||
151 | sprintf( |
||
152 | 'Secure must match flipped value with middleware %s vs %s', |
||
153 | var_export($cookieConfig['secure'], true), |
||
154 | var_export($cookie->isSecure(), true) |
||
155 | ) |
||
156 | ); |
||
157 | static::assertSame( |
||
158 | $cookieConfig['httpOnly'], |
||
159 | $cookie->isHttpOnly(), |
||
160 | sprintf( |
||
161 | 'HttpOnly must match flipped value with middleware %s vs %s', |
||
162 | var_export($cookieConfig['httpOnly'], true), |
||
163 | var_export($cookie->isHttpOnly(), true) |
||
164 | ) |
||
165 | ); |
||
166 | static::assertSame( |
||
167 | $cookieConfig['sameSite'], |
||
168 | $cookie->getSameSite(), |
||
169 | sprintf( |
||
170 | 'SameSite must match flipped value with middleware %s vs %s', |
||
171 | var_export($cookieConfig['sameSite'], true), |
||
172 | var_export($cookie->getSameSite(), true) |
||
173 | ) |
||
303 |