Passed
Push — master ( 796a84...196098 )
by Goffy
38s queued 13s
created

Migrate::moveDoColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Tdmdownloads\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
 * Class Migrate synchronize existing tables with target schema
19
 *
20
 * @category  Migrate
21
 * @author    Richard Griffith <[email protected]>
22
 * @copyright 2016 XOOPS Project (https://xoops.org)
23
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
24
 * @link      https://xoops.org
25
 */
26
class Migrate extends \Xmf\Database\Migrate
27
{
28
    private $moduleDirName;
29
    private $renameColumns;
30
    private $renameTables;
31
32
    /**
33
     * Migrate constructor.
34
     * @throws \RuntimeException
35
     * @throws \InvalidArgumentException
36
     */
37
    public function __construct()
38
    {
39
        $class = __NAMESPACE__ . '\\' . 'Configurator';
40
        if (!\class_exists($class)) {
41
            throw new \RuntimeException("Class '$class' not found");
42
        }
43
        $configurator       = new $class();
44
        $this->renameTables = $configurator->renameTables;
45
        $this->renameColumns = $configurator->renameColumns;
46
47
        $this->moduleDirName = \basename(\dirname(__DIR__, 2));
48
        parent::__construct($this->moduleDirName);
49
    }
50
51
    /**
52
     * change table prefix if needed
53
     */
54
    private function changePrefix(): void
55
    {
56
        //        foreach ($this->renameTables as $oldName => $newName) {
57
        //            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
58
        //                $this->tableHandler->renameTable($oldName, $newName);
59
        //            }
60
        //        }
61
    }
62
63
    /**
64
     * Change integer IPv4 column to varchar IPv6 capable
65
     *
66
     * @param string $tableName  table to convert
67
     * @param string $columnName column with IP address
68
     */
69
    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 $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

69
    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...
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

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

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