Passed
Pull Request — master (#11)
by Michael
11:08
created

Migrate::convertIPAddresses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

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

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

70
    private function convertIPAddresses(/** @scrutinizer ignore-unused */ string $tableName, string $columnName): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $columnName is not used and could be removed. ( Ignorable by Annotation )

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

70
    private function convertIPAddresses(string $tableName, /** @scrutinizer ignore-unused */ string $columnName): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
    {
72
        //        if ($this->tableHandler->useTable($tableName)) {
73
        //            $attributes = $this->tableHandler->getColumnAttributes($tableName, $columnName);
74
        //            if (false !== \mb_strpos($attributes, ' int(')) {
75
        //                if (false === \mb_strpos($attributes, 'unsigned')) {
76
        //                    $this->tableHandler->alterColumn($tableName, $columnName, " bigint(16) NOT NULL  DEFAULT '0' ");
77
        //                    $this->tableHandler->update($tableName, [$columnName => "4294967296 + $columnName"], "WHERE $columnName < 0", false);
78
        //                }
79
        //                $this->tableHandler->alterColumn($tableName, $columnName, " varchar(45)  NOT NULL  DEFAULT '' ");
80
        //                $this->tableHandler->update($tableName, [$columnName => "INET_NTOA($columnName)"], '', false);
81
        //            }
82
        //        }
83
    }
84
85
    /**
86
     * @deprecated (just as an example here)
87
     * Move do* columns from newbb_posts to newbb_posts_text table
88
     */
89
    private function moveDoColumns(): void
90
    {
91
        //        $tableName    = 'newbb_posts_text';
92
        //        $srcTableName = 'newbb_posts';
93
        //        if ($this->tableHandler->useTable($tableName)
94
        //            && $this->tableHandler->useTable($srcTableName)) {
95
        //            $attributes = $this->tableHandler->getColumnAttributes($tableName, 'dohtml');
96
        //            if (false === $attributes) {
97
        //                $this->synchronizeTable($tableName);
98
        //                $updateTable = $GLOBALS['xoopsDB']->prefix($tableName);
99
        //                $joinTable   = $GLOBALS['xoopsDB']->prefix($srcTableName);
100
        //                $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';
101
        //                $this->tableHandler->addToQueue($sql);
102
        //            }
103
        //        }
104
                }
105
106
    /**
107
     * rename table if needed
108
     */
109
    private function renameTable(): void
110
    {
111
        foreach ($this->renameTables as $oldName => $newName) {
112
            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
113
                $this->tableHandler->renameTable($oldName, $newName);
114
            }
115
        }
116
    }
117
118
    /**
119
     * rename columns if needed
120
     */
121
    private function renameColumns(): void
122
    {
123
        foreach ($this->renameColumns as $tableName) {
124
            if ($this->tableHandler->useTable($tableName)) {
125
                $oldName    = $tableName['from'];
126
                $newName    = $tableName['to'];
127
                $attributes = $this->tableHandler->getColumnAttributes($tableName, $oldName);
128
                if (\is_string($attributes) && false !== \strpos($attributes, ' int(')) {
129
                    $this->tableHandler->alterColumn($tableName, $oldName, $attributes, $newName);
130
                }
131
            }
132
        }
133
    }
134
135
    /**
136
     * Perform any upfront actions before synchronizing the schema
137
     *
138
     * Some typical uses include
139
     *   table and column renames
140
     *   data conversions
141
     */
142
    protected function preSyncActions(): void
143
    {
144
        // change 'bb' table prefix to 'newbb'
145
        $this->changePrefix();
146
        // columns dohtml, dosmiley, doxcode, doimage and dobr moved between tables as some point
147
        $this->moveDoColumns();
0 ignored issues
show
Deprecated Code introduced by
The function XoopsModules\Xoopsfaq\Co...igrate::moveDoColumns() has been deprecated: (just as an example here) Move do* columns from newbb_posts to newbb_posts_text table ( Ignorable by Annotation )

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

147
        /** @scrutinizer ignore-deprecated */ $this->moveDoColumns();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
148
        // Convert IP address columns from int to readable varchar(45) for IPv6
149
        //        $this->convertIPAddresses('newbb_posts', 'poster_ip');
150
        //        $this->convertIPAddresses('newbb_report', 'reporter_ip');
151
152
        // rename table
153
        if ($this->renameTables && \is_array($this->renameTables)) {
154
            $this->renameTable();
155
        }
156
        // rename column
157
        if ($this->renameColumns && \is_array($this->renameColumns)) {
158
            $this->renameColumns();
159
        }
160
    }
161
}
162