Migrate::moveDoColumns()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
nc 1
nop 0
dl 0
loc 14
rs 9.8666
c 2
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Pedigree\Common;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * Class Migrate synchronize existing tables with target schema
17
 *
18
 * @category  Migrate
19
 * @author    Richard Griffith <[email protected]>
20
 * @copyright 2016 XOOPS Project (https://xoops.org)
21
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
22
 * @link      https://xoops.org
23
 */
24
class Migrate extends \Xmf\Database\Migrate
25
{
26
    private $renameTables;
27
    private $moduleDirName;
28
29
    /**
30
     * Migrate constructor
31
     *
32
     * @throws \RuntimeException
33
     * @throws \InvalidArgumentException
34
     */
35
    public function __construct(?Configurator $configurator = null)
36
    {
37
        if (null !== $configurator) {
38
            $this->renameTables  = $configurator->renameTables;
39
            $this->moduleDirName = \basename(\dirname(__DIR__, 2));
40
            parent::__construct($this->moduleDirName);
41
        }
42
    }
43
44
    /**
45
     * change table prefix if needed
46
     *
47
     * @return void
48
     */
49
    private function changePrefix(): void
50
    {
51
        foreach ($this->renameTables as $oldName => $newName) {
52
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
53
                $this->tableHandler->renameTable($oldName, $newName);
54
            }
55
        }
56
    }
57
58
    /**
59
     * Change integer IPv4 column to varchar IPv6 capable
60
     *
61
     * @param string $tableName  table to convert
62
     * @param string $columnName column with IP address
63
     *
64
     * @return void
65
     */
66
    private function convertIPAddresses(string $tableName, string $columnName): void
0 ignored issues
show
Unused Code introduced by
The method convertIPAddresses() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
67
    {
68
        if ($this->tableHandler->useTable($tableName)) {
69
            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
70
            if (false !== \mb_strpos($attributes, ' int(')) {
0 ignored issues
show
Bug introduced by
It seems like $attributes can also be of type false; however, parameter $haystack of mb_strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            if (false !== \mb_strpos(/** @scrutinizer ignore-type */ $attributes, ' int(')) {
Loading history...
71
                if (false === \mb_strpos($attributes, 'unsigned')) {
72
                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
73
                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
74
                }
75
                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
76
                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
77
            }
78
        }
79
    }
80
81
    /**
82
     * @deprecated - not used in Pedigree module
83
     *
84
     * Move do* columns from newbb_posts to newbb_posts_text table
85
     */
86
    private function moveDoColumns()
0 ignored issues
show
Unused Code introduced by
The method moveDoColumns() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
87
    {
88
        return; // added when method was deprecated
89
        $tableName    = 'newbb_posts_text';
0 ignored issues
show
Unused Code introduced by
$tableName = 'newbb_posts_text' is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
90
        $srcTableName = 'newbb_posts';
91
        if ($this->tableHandler->useTable($tableName)
92
            && $this->tableHandler->useTable($srcTableName)) {
93
            $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
94
            if (false === $attributes) {
95
                $this->synchronizeTable($tableName);
96
                $updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
97
                $joinTable   = $GLOBALS['xoopsDB']->prefix($srcTableName);
98
                $sql         = "UPDATE `$updateTable` t1 INNER JOIN `$joinTable` t2 ON t1.post_id = t2.post_id \n" . "SET t1.dohtml = t2.dohtml,  t1.dosmiley = t2.dosmiley, t1.doxcode = t2.doxcode\n" . '  , t1.doimage = t2.doimage, t1.dobr = t2.dobr';
99
                $this->tableHandler->addToQueue($sql);
100
            }
101
        }
102
    }
103
104
    /**
105
     * Perform any upfront actions before synchronizing the schema
106
     *
107
     * Some typical uses include:
108
     *   table and column renames
109
     *   data conversions
110
     *
111
     * @return void
112
     */
113
    protected function preSyncActions(): void
114
    {
115
        // change 'bb' table prefix to 'newbb'
116
        $this->changePrefix();
117
        // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
118
        //$this->moveDoColumns();
119
        // Convert IP address columns from int to readable varchar(45) for IPv6
120
        //$this->convertIPAddresses('newbb_posts', 'poster_ip');
121
        //$this->convertIPAddresses('newbb_report', 'reporter_ip');
122
    }
123
}
124