Conditions | 1 |
Paths | 1 |
Total Lines | 59 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 2 |
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 |
||
162 | public function test_ExecAndTransaction_ExecWithInTrans() |
||
163 | { |
||
164 | $params = [ |
||
165 | "driver" => $GLOBALS[ 'DB_DRIVER' ], |
||
166 | "user" => $GLOBALS[ 'DB_USER' ], |
||
167 | "pass" => $GLOBALS[ 'DB_PASSWORD' ], |
||
168 | "dbname" => $GLOBALS[ 'DB_DBNAME' ], |
||
169 | "host" => $GLOBALS[ 'DB_HOST' ], |
||
170 | "persistent" => false, |
||
171 | ]; |
||
172 | |||
173 | $instance = new Db; |
||
174 | $instance->connect( $params ); |
||
175 | |||
176 | // commit pattern |
||
177 | $this->assertTrue( $instance->beginTransaction() ); |
||
178 | $this->assertTrue( $instance->inTransaction() ); |
||
179 | |||
180 | $this->assertEquals( 1, |
||
181 | $instance->exec( 'INSERT INTO risoluto_db_test(id, column1, column2) values ("10", "TEST_A", "TEST_B");' ) ); |
||
182 | $this->assertEquals( 2, $this->getconnection()->getRowCount( 'risoluto_db_test' ) ); |
||
183 | |||
184 | $this->assertEquals( 10, $instance->lastInsertId() ); |
||
185 | $this->assertEquals( 10, $instance->lastInsertId( 'id' ) ); |
||
186 | |||
187 | $this->assertTrue( $instance->commit() ); |
||
188 | $this->assertFalse( $instance->inTransaction() ); |
||
189 | |||
190 | $this->assertEquals( 3, $this->getconnection()->getRowCount( 'risoluto_db_test' ) ); |
||
191 | |||
192 | |||
193 | // Rollback pattern |
||
194 | $before_val = $this->getconnection()->createQueryTable( 'risoluto_db_test', |
||
195 | 'SELECT id, column1, column2 FROM risoluto_db_test WHERE id="10";' ); |
||
196 | $this->assertTrue( $instance->beginTransaction() ); |
||
197 | $this->assertTrue( $instance->inTransaction() ); |
||
198 | |||
199 | $this->assertEquals( 1, |
||
200 | $instance->exec( 'UPDATE risoluto_db_test SET column1="TEST_C", column2="TEST_C" WHERE id="10";' ) ); |
||
201 | $this->assertEquals( 3, $this->getconnection()->getRowCount( 'risoluto_db_test' ) ); |
||
202 | |||
203 | $this->assertEquals( 1, $instance->exec( 'DELETE FROM risoluto_db_test WHERE id="10";' ) ); |
||
204 | $this->assertEquals( 3, $this->getconnection()->getRowCount( 'risoluto_db_test' ) ); |
||
205 | |||
206 | $this->assertTrue( $instance->rollBack() ); |
||
207 | $this->assertFalse( $instance->inTransaction() ); |
||
208 | |||
209 | $after_val = $this->getconnection()->createQueryTable( 'risoluto_db_test', |
||
210 | 'SELECT id, column1, column2 FROM risoluto_db_test WHERE id="10";' ); |
||
211 | $this->assertEquals( 3, $this->getconnection()->getRowCount( 'risoluto_db_test' ) ); |
||
212 | $this->assertTablesEqual( $before_val, $after_val ); |
||
213 | |||
214 | // Cleaning |
||
215 | $this->assertEquals( 1, $instance->exec( 'DELETE FROM risoluto_db_test WHERE id="10";' ) ); |
||
216 | $this->assertEquals( 2, $this->getconnection()->getRowCount( 'risoluto_db_test' ) ); |
||
217 | |||
218 | $instance->disConnect(); |
||
219 | unset( $instance ); |
||
220 | } |
||
221 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.