| Conditions | 2 |
| Paths | 2 |
| Total Lines | 118 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 111 | public function testSyncOneWay() |
||
| 112 | { |
||
| 113 | $tableUser = self::$tableUser; |
||
| 114 | $tableUserDest = self::$tableUserDest; |
||
| 115 | $tableNotExist = 'test_not_exist'; |
||
| 116 | |||
| 117 | |||
| 118 | // Add timestamp column in source db |
||
| 119 | self::$dbMysql->Execute( |
||
| 120 | "ALTER TABLE {$tableUser} |
||
| 121 | ADD COLUMN ts TIMESTAMP NOT NULL |
||
| 122 | DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP |
||
| 123 | ;" |
||
| 124 | ); |
||
| 125 | // Refresh cached meta data |
||
| 126 | self::$dbMysql->getMetaColumn($tableUser, true); |
||
| 127 | |||
| 128 | |||
| 129 | // Create another test user table as sync dest |
||
| 130 | self::$dbMysql->Execute( |
||
| 131 | "CREATE TABLE {$tableUserDest} LIKE {$tableUser};" |
||
| 132 | ); |
||
| 133 | |||
| 134 | |||
| 135 | // Prepare dummy date in source table |
||
| 136 | $data = []; |
||
| 137 | for ($i = 0; $i < $this->totalRows; $i ++) { |
||
| 138 | $data[] = [ |
||
| 139 | 'uuid' => self::generateUuid(), |
||
| 140 | 'title' => "Title - $i", |
||
| 141 | 'age' => $i + 20, |
||
| 142 | 'credit' => $i * 100, |
||
| 143 | ]; |
||
| 144 | } |
||
| 145 | self::$dbMysql->write($tableUser, $data, 'I'); |
||
| 146 | $rowsSource = self::$dbMysql->getRowCount($tableUser); |
||
|
|
|||
| 147 | |||
| 148 | |||
| 149 | // Prepare SyncDbData instance |
||
| 150 | // The table $tableNotExist if test dummy, we will use mock to create |
||
| 151 | // convertDataXxx() method for it, and return empty convert result to |
||
| 152 | // skip data write to it. |
||
| 153 | $config = [ |
||
| 154 | $tableUser => [$tableUserDest, $tableNotExist], |
||
| 155 | ]; |
||
| 156 | |||
| 157 | $stringUtil = self::$utilContainer->getString(); |
||
| 158 | |||
| 159 | // Mock instance with 2 additional convert method |
||
| 160 | $convertForNotExist = 'convertData' . |
||
| 161 | $stringUtil->toStudlyCaps($tableUser) . |
||
| 162 | 'To' . $stringUtil->toStudlyCaps($tableNotExist); |
||
| 163 | $convertForUserDest = 'convertData' . |
||
| 164 | $stringUtil->toStudlyCaps($tableUser) . |
||
| 165 | 'To' . $stringUtil->toStudlyCaps($tableUserDest); |
||
| 166 | $sdd = $this->getMock( |
||
| 167 | SyncDbData::class, |
||
| 168 | [$convertForNotExist] |
||
| 169 | ); |
||
| 170 | $sdd->expects($this->any()) |
||
| 171 | ->method($convertForNotExist) |
||
| 172 | ->will($this->returnValue(null)); |
||
| 173 | |||
| 174 | $sdd->setDb(self::$dbMysql, self::$dbMysql); |
||
| 175 | $sdd->batchSize = 10; |
||
| 176 | |||
| 177 | |||
| 178 | // First sync round, limit by batchSize |
||
| 179 | |||
| 180 | $this->assertEquals($sdd->batchSize, $sdd->syncOneway($config)); |
||
| 181 | $this->assertEquals( |
||
| 182 | $sdd->batchSize, |
||
| 183 | self::$dbMysql->getRowCount($tableUserDest) |
||
| 184 | ); |
||
| 185 | |||
| 186 | // Run sync again will sync nothing, because batchSize limit |
||
| 187 | $this->assertEquals(0, $sdd->syncOneWay($config)); |
||
| 188 | |||
| 189 | |||
| 190 | // Second sync round, full sync, not reach batchSize limit |
||
| 191 | |||
| 192 | $sdd = $this->getMock( |
||
| 193 | SyncDbData::class, |
||
| 194 | [$convertForNotExist, $convertForUserDest] |
||
| 195 | ); |
||
| 196 | $sdd->expects($this->any()) |
||
| 197 | ->method($convertForNotExist) |
||
| 198 | ->will($this->returnValue(null)); |
||
| 199 | |||
| 200 | // Change age column through convert data method |
||
| 201 | $callback = function ($sourceAr) { |
||
| 202 | $sourceAr['age'] = 42; |
||
| 203 | return $sourceAr; |
||
| 204 | }; |
||
| 205 | $sdd->expects($this->any()) |
||
| 206 | ->method($convertForUserDest) |
||
| 207 | ->will($this->returnCallback($callback)); |
||
| 208 | |||
| 209 | $sdd->setDb(self::$dbMysql, self::$dbMysql); |
||
| 210 | // Mysql timestamp is not unique, so we need raise batchSize to sync |
||
| 211 | // all rows. It need not clear record table. |
||
| 212 | $sdd->batchSize = 200; |
||
| 213 | //self::$dbMysql->Execute('TRUNCATE TABLE ' . self::$tableUserDest); |
||
| 214 | |||
| 215 | $this->assertEquals($this->totalRows, $sdd->syncOneWay($config)); |
||
| 216 | $this->assertEquals( |
||
| 217 | $this->totalRows, |
||
| 218 | self::$dbMysql->getRowCount($tableUserDest) |
||
| 219 | ); |
||
| 220 | |||
| 221 | // In dest db, column age in all rows are same value return by |
||
| 222 | // callback function we defined, assert it now. |
||
| 223 | $rs = self::$dbMysql->execute( |
||
| 224 | "SELECT DISTINCT age from $tableUserDest" |
||
| 225 | ); |
||
| 226 | $this->assertEquals(1, $rs->RowCount()); |
||
| 227 | $this->assertEquals(42, $rs->fields['age']); |
||
| 228 | } |
||
| 229 | |||
| 379 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.