Migrate   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 28
c 3
b 0
f 0
dl 0
loc 141
rs 10
wmc 19

7 Methods

Rating   Name   Duplication   Size   Complexity  
A changePrefix() 0 2 1
A moveDoColumns() 0 2 1
A renameTable() 0 5 4
A __construct() 0 12 2
A preSyncActions() 0 17 5
A renameColumns() 0 9 5
A convertIPAddresses() 0 2 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Xoopsfaq\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.0 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
    /**
27
     * @readonly
28
     */
29
    private string $moduleDirName;
30
    /**
31
     * @readonly
32
     */
33
    private array $renameColumns;
34
    /**
35
     * @readonly
36
     */
37
    private array $renameTables;
38
39
    /**
40
     * Migrate constructor.
41
     * @throws \InvalidArgumentException
42
     * @throws \RuntimeException
43
     */
44
    public function __construct()
45
    {
46
        $class = __NAMESPACE__ . '\\' . 'Configurator';
47
        if (!\class_exists($class)) {
48
            throw new \RuntimeException("Class '$class' not found");
49
        }
50
        $configurator        = new $class();
51
        $this->renameTables  = $configurator->renameTables;
52
        $this->renameColumns = $configurator->renameColumns;
53
54
        $this->moduleDirName = \basename(\dirname(__DIR__, 2));
55
        parent::__construct($this->moduleDirName);
56
    }
57
58
    /**
59
     * change table prefix if needed
60
     */
61
    private function changePrefix(): void
62
    {
63
        //        foreach ($this->renameTables as $oldName => $newName) {
64
        //            if ($this->tableHandler->useTable($oldName) && !$this->tableHandler->useTable($newName)) {
65
        //                $this->tableHandler->renameTable($oldName, $newName);
66
        //            }
67
        //        }
68
    }
69
70
    /**
71
     * Change integer IPv4 column to varchar IPv6 capable
72
     *
73
     * @param string $tableName  table to convert
74
     * @param string $columnName column with IP address
75
     */
76
    private function convertIPAddresses(string $tableName, string $columnName): void
0 ignored issues
show
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

76
    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

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

153
        /** @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...
154
        // Convert IP address columns from int to readable varchar(45) for IPv6
155
        //        $this->convertIPAddresses('newbb_posts', 'poster_ip');
156
        //        $this->convertIPAddresses('newbb_report', 'reporter_ip');
157
158
        // rename table
159
        if ($this->renameTables && \is_array($this->renameTables)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->renameTables of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
160
            $this->renameTable();
161
        }
162
        // rename column
163
        if ($this->renameColumns && \is_array($this->renameColumns)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->renameColumns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
164
            $this->renameColumns();
165
        }
166
    }
167
}
168