Completed
Push — newinternal ( 65a0f5...5b021c )
by Simon
08:29
created

MigrateToRoles   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 12.96 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 7
loc 54
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B execute() 7 51 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\ConsoleTasks;
10
11
use PDO;
12
use Waca\DataObjects\Log;
13
use Waca\DataObjects\User;
14
use Waca\DataObjects\UserRole;
15
use Waca\Tasks\ConsoleTaskBase;
16
17
class MigrateToRoles extends ConsoleTaskBase
18
{
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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