Conditions | 7 |
Paths | 17 |
Total Lines | 51 |
Lines | 7 |
Ratio | 13.73 % |
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 |
||
19 | public function execute() |
||
20 | { |
||
21 | $communityUser = User::getCommunity(); |
||
22 | |||
23 | $database = $this->getDatabase(); |
||
24 | $statement = $database->query('SELECT id, status, checkuser FROM user;'); |
||
25 | $update = $database->prepare("UPDATE user SET status = 'Active' WHERE id = :id;"); |
||
26 | |||
27 | $users = $statement->fetchAll(PDO::FETCH_ASSOC); |
||
28 | |||
29 | foreach ($users as $user) { |
||
30 | $toAdd = array('user'); |
||
31 | |||
32 | if($user['status'] === 'Admin'){ |
||
33 | $toAdd[] = 'admin'; |
||
34 | } |
||
35 | |||
36 | if($user['checkuser'] == 1){ |
||
37 | $toAdd[] = 'checkuser'; |
||
38 | } |
||
39 | |||
40 | View Code Duplication | foreach ($toAdd as $x) { |
|
|
|||
41 | $a = new UserRole(); |
||
42 | $a->setUser($user['id']); |
||
43 | $a->setRole($x); |
||
44 | $a->setDatabase($database); |
||
45 | $a->save(); |
||
46 | } |
||
47 | |||
48 | $logData = serialize(array( |
||
49 | 'added' => $toAdd, |
||
50 | 'removed' => array(), |
||
51 | 'reason' => 'Initial migration' |
||
52 | )); |
||
53 | |||
54 | $log = new Log(); |
||
55 | $log->setDatabase($database); |
||
56 | $log->setAction('RoleChange'); |
||
57 | $log->setObjectId($user['id']); |
||
58 | $log->setObjectType('User'); |
||
59 | $log->setUser($communityUser); |
||
60 | $log->setComment($logData); |
||
61 | $log->save(); |
||
62 | |||
63 | if($user['status'] === 'Admin' || $user['status'] === 'User'){ |
||
64 | $update->execute(array('id' => $user['id'])); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | $database->exec("UPDATE schemaversion SET version = 25;"); |
||
69 | } |
||
70 | } |
||
71 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.