Conditions | 38 |
Paths | > 20000 |
Total Lines | 195 |
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 |
||
21 | public function actionMigrate() |
||
22 | { |
||
23 | try { |
||
24 | $module = Module::getInstance(); |
||
25 | /** @var Connection $db */ |
||
26 | $db = Instance::ensure($module->db, Connection::class); |
||
27 | |||
28 | /////////////// |
||
29 | /// |
||
30 | /// AccessToken |
||
31 | /// |
||
32 | /////////////// |
||
33 | $this->stdout('Start migrating AccessTokens ...' . "\n"); |
||
34 | /** @var AccessTokenService $accessTokenService */ |
||
35 | $accessTokenService = Yii::createObject('sweelix\oauth2\server\interfaces\AccessTokenServiceInterface'); |
||
36 | $accessTokenList = $accessTokenService->getAccessTokenListKey(); |
||
37 | if ($db->executeCommand('EXISTS', [$accessTokenList]) == 1) { |
||
38 | $prompt = $this->prompt('The key "'.$accessTokenList.'" already exists. Do you want to continue ? (yes, no)', [ |
||
39 | 'required' => true, |
||
40 | ]); |
||
41 | switch ($prompt) { |
||
42 | case 'no': |
||
43 | case 'n': |
||
44 | exit(); |
||
45 | } |
||
46 | } |
||
47 | $userListKey = $accessTokenService->getUserListKey(); |
||
48 | if ($db->executeCommand('EXISTS', [$userListKey]) == 1) { |
||
49 | $prompt = $this->prompt('The key "'.$userListKey.'" already exists. Do you want to continue ? (yes, no)', [ |
||
50 | 'required' => true, |
||
51 | ]); |
||
52 | switch ($prompt) { |
||
53 | case 'no': |
||
54 | case 'n': |
||
55 | exit(); |
||
56 | } |
||
57 | } |
||
58 | $accessToken = Yii::createObject('sweelix\oauth2\server\interfaces\AccessTokenModelInterface'); |
||
59 | $accessTokenClass = get_class($accessToken); |
||
60 | $accessTokens = $db->executeCommand('KEYS', [$accessTokenService->getUserAccessTokensKey('*')]); |
||
61 | if (!empty($accessTokens)) { |
||
62 | $db->executeCommand('DEL', $accessTokens); |
||
63 | } |
||
64 | $accessTokens = $db->executeCommand('KEYS', [$accessTokenService->getClientAccessTokensKey('*')]); |
||
65 | if (!empty($accessTokens)) { |
||
66 | $db->executeCommand('DEL', $accessTokens); |
||
67 | } |
||
68 | $accessTokens = $db->executeCommand('KEYS', [$accessTokenService->getAccessTokenKey('*')]); |
||
69 | foreach ($accessTokens as $accessTokenHash) { |
||
|
|||
70 | if ($db->executeCommand('TYPE', [$accessTokenHash]) !== 'hash') { |
||
71 | continue; |
||
72 | } |
||
73 | $accessTokenData = $db->executeCommand('HGETALL', [$accessTokenHash]); |
||
74 | if (!empty($accessTokenData)) { |
||
75 | /** @var AccessTokenModelInterface $accessToken */ |
||
76 | $accessToken = $accessTokenClass::findOne($accessTokenData[1]); // id |
||
77 | if (!empty($accessToken)) { |
||
78 | $accessToken->delete(); |
||
79 | $accessToken->save(); |
||
80 | } |
||
81 | } |
||
82 | } |
||
83 | $this->stdout('Done migrating AccessTokens !' . "\n"); |
||
84 | |||
85 | /////////////// |
||
86 | /// |
||
87 | /// Clients |
||
88 | /// |
||
89 | /////////////// |
||
90 | $this->stdout('Start migrating Clients ...' . "\n"); |
||
91 | /** @var ClientService $clientService */ |
||
92 | $clientService = Yii::createObject('sweelix\oauth2\server\interfaces\ClientServiceInterface'); |
||
93 | $clientList = $clientService->getClientListKey(); |
||
94 | if ($db->executeCommand('EXISTS', [$clientList]) == 1) { |
||
95 | $prompt = $this->prompt('The key "'.$clientList.'" already exists. Do you want to continue ? (yes, no)', [ |
||
96 | 'required' => true, |
||
97 | ]); |
||
98 | switch ($prompt) { |
||
99 | case 'no': |
||
100 | case 'n': |
||
101 | exit(); |
||
102 | } |
||
103 | } |
||
104 | $clients = $db->executeCommand('KEYS', [$clientService->getClientKey('*')]); |
||
105 | foreach ($clients as $clientHash) { |
||
106 | if ($db->executeCommand('TYPE', [$clientHash]) !== 'hash') { |
||
107 | continue; |
||
108 | } |
||
109 | $clientData = $db->executeCommand('HGETALL', [$clientHash]); |
||
110 | if (!empty($clientData)) { |
||
111 | $db->executeCommand('SADD', [$clientService->getClientListKey(), $clientData[1]]); |
||
112 | } |
||
113 | } |
||
114 | $this->stdout('Done migrating Clients !' . "\n"); |
||
115 | |||
116 | /////////////// |
||
117 | /// |
||
118 | /// Jti |
||
119 | /// |
||
120 | /////////////// |
||
121 | $this->stdout('Start migrating Jtis ...' . "\n"); |
||
122 | /** @var JtiService $jtiService */ |
||
123 | $jtiService = Yii::createObject('sweelix\oauth2\server\interfaces\JtiServiceInterface'); |
||
124 | $jtiList = $jtiService->getJtiListKey(); |
||
125 | if ($db->executeCommand('EXISTS', [$jtiList]) == 1) { |
||
126 | $prompt = $this->prompt('The key "'.$jtiList.'" already exists. Do you want to continue ? (yes, no)', [ |
||
127 | 'required' => true, |
||
128 | ]); |
||
129 | switch ($prompt) { |
||
130 | case 'no': |
||
131 | case 'n': |
||
132 | exit(); |
||
133 | } |
||
134 | } |
||
135 | $jti = Yii::createObject('sweelix\oauth2\server\interfaces\JtiModelInterface'); |
||
136 | $jtiClass = get_class($jti); |
||
137 | $jtis = $db->executeCommand('KEYS', [$jtiService->getSubjectJtisKey('*')]); |
||
138 | if (!empty($jtis)) { |
||
139 | $db->executeCommand('DEL', $jtis); |
||
140 | } |
||
141 | $jtis = $db->executeCommand('KEYS', [$jtiService->getSubjectJtisKey('*')]); |
||
142 | if (!empty($jtis)) { |
||
143 | $db->executeCommand('DEL', $jtis); |
||
144 | } |
||
145 | $jtis = $db->executeCommand('KEYS', [$jtiService->getJtiKey('*')]); |
||
146 | foreach ($jtis as $jtiHash) { |
||
147 | if ($db->executeCommand('TYPE', [$jtiHash]) !== 'hash') { |
||
148 | continue; |
||
149 | } |
||
150 | $jtiData = $db->executeCommand('HGETALL', [$jtiHash]); |
||
151 | if (!empty($jtiData)) { |
||
152 | /** @var JtiModelInterface $jti */ |
||
153 | $jti = $jtiClass::findOne($jtiData[1]); // id |
||
154 | if (!empty($jti)) { |
||
155 | $jti->delete(); |
||
156 | $jti->save(); |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | $this->stdout('Done migrating Jtis !' . "\n"); |
||
161 | |||
162 | /////////////// |
||
163 | /// |
||
164 | /// RefreshToken |
||
165 | /// |
||
166 | /////////////// |
||
167 | $this->stdout('Start migrating RefreshTokens ...' . "\n"); |
||
168 | /** @var RefreshTokenService $refreshTokenService */ |
||
169 | $refreshTokenService = Yii::createObject('sweelix\oauth2\server\interfaces\RefreshTokenServiceInterface'); |
||
170 | $refreshTokenList = $refreshTokenService->getRefreshTokenListKey(); |
||
171 | if ($db->executeCommand('EXISTS', [$refreshTokenList]) == 1) { |
||
172 | $prompt = $this->prompt('The key "'.$refreshTokenList.'" already exists. Do you want to continue ? (yes, no)', [ |
||
173 | 'required' => true, |
||
174 | ]); |
||
175 | switch ($prompt) { |
||
176 | case 'no': |
||
177 | case 'n': |
||
178 | exit(); |
||
179 | } |
||
180 | } |
||
181 | $refreshToken = Yii::createObject('sweelix\oauth2\server\interfaces\RefreshTokenModelInterface'); |
||
182 | $refreshTokenClass = get_class($refreshToken); |
||
183 | $refreshTokens = $db->executeCommand('KEYS', [$refreshTokenService->getUserRefreshTokensKey('*')]); |
||
184 | if (!empty($refreshTokens)) { |
||
185 | $db->executeCommand('DEL', $refreshTokens); |
||
186 | } |
||
187 | $refreshTokens = $db->executeCommand('KEYS', [$refreshTokenService->getClientRefreshTokensKey('*')]); |
||
188 | if (!empty($refreshTokens)) { |
||
189 | $db->executeCommand('DEL', $refreshTokens); |
||
190 | } |
||
191 | $refreshTokens = $db->executeCommand('KEYS', [$refreshTokenService->getRefreshTokenKey('*')]); |
||
192 | foreach ($refreshTokens as $refreshTokenHash) { |
||
193 | if ($db->executeCommand('TYPE', [$refreshTokenHash]) !== 'hash') { |
||
194 | continue; |
||
195 | } |
||
196 | $refreshTokenData = $db->executeCommand('HGETALL', [$refreshTokenHash]); |
||
197 | if (!empty($refreshTokenData)) { |
||
198 | /** @var RefreshTokenModelInterface $refreshToken */ |
||
199 | $refreshToken = $refreshTokenClass::findOne($refreshTokenData[1]); // id |
||
200 | if (!empty($refreshToken)) { |
||
201 | $refreshToken->delete(); |
||
202 | $refreshToken->save(); |
||
203 | } |
||
204 | } |
||
205 | } |
||
206 | $this->stdout('Done migrating RefreshTokens !' . "\n"); |
||
207 | |||
208 | $this->stdout('Migration done !' . "\n"); |
||
209 | $exitCode = ExitCode::OK; |
||
210 | } catch (\Exception $e) { |
||
211 | $this->stderr($e->getMessage() . "\n"); |
||
212 | $exitCode = ExitCode::getReason($e->getCode()); |
||
213 | } |
||
214 | return $exitCode; |
||
215 | } |
||
216 | } |
||
217 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.