FixStaleReportStates   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
dl 0
loc 70
rs 10
c 1
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A _migrateToNewStatus() 0 18 2
A _migrateDuplicateReports() 0 22 2
A up() 0 5 1
A _migrateBasedOnGithubLinked() 0 10 1
1
<?php
2
3
use Cake\Log\Log;
4
use Migrations\AbstractMigration;
5
6
class FixStaleReportStates extends AbstractMigration
7
{
8
    /**
9
     * This migration changes the old report statuses to more
10
     * Github-related status
11
     */
12
    public function up(): void
13
    {
14
        $this->_migrateBasedOnGithubLinked();
15
        $this->_migrateToNewStatus();
16
        $this->_migrateDuplicateReports();
17
    }
18
19
    private function _migrateBasedOnGithubLinked(): void
20
    {
21
        $sql = 'UPDATE `reports` SET `status` = \'forwarded\''
22
            . ' WHERE `sourceforge_bug_id` IS NOT NULL AND `status` <> \'fixed\'';
23
        $rowsAffected = $this->execute($sql);
24
25
        Log::debug(
26
            $rowsAffected . ' reports are linked to a'
27
                . ' Github issue.'
28
                . ' These have been marked to have a \'forwarded\' status.'
29
        );
30
    }
31
32
    private function _migrateToNewStatus(): void
33
    {
34
        $statusMap = [
35
            'fixed' => 'resolved',
36
            'open' => 'new',
37
            'out-of-date' => 'invalid',
38
            'works-for-me' => 'invalid',
39
            'wontfix' => 'invalid',
40
        ];
41
42
        foreach ($statusMap as $oldStatus => $newStatus) {
43
            $sql = 'UPDATE `reports` SET `status` = \'' . $newStatus . '\''
44
                . ' WHERE `status` = \'' . $oldStatus . '\'';
45
            $rowsAffected = $this->execute($sql);
46
47
            Log::debug(
48
                $rowsAffected . ' reports with \'' . $oldStatus . '\''
49
                    . ' were mapped to \'' . $newStatus . '\'.'
50
            );
51
        }
52
    }
53
54
    private function _migrateDuplicateReports(): void
55
    {
56
        // Find the original reports and set the status
57
        // of their duplicate reports same as their status
58
        $sql = 'SELECT `id`, `status` FROM `reports` WHERE'
59
            . ' `related_to` IS NULL';
60
        $origCount = 0;
61
        $result = $this->query($sql);
62
        $rowsAffected = 0;
63
64
        while ($row = $result->fetch()) {
65
            $update_sql = 'UPDATE `reports` SET `status` = \''
66
                . $row['status'] . '\' WHERE `id` = \''
67
                . $row['id'] . '\'';
68
69
            $rowsAffected += $this->execute($update_sql);
70
            $origCount++;
71
        }
72
73
        Log::debug(
74
            $rowsAffected . ' duplicate reports were mapped to '
75
                . ' same status as their ' . $origCount . ' original reports'
76
        );
77
    }
78
}
79