| Conditions | 1 |
| Paths | 1 |
| Total Lines | 89 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 25 | protected function getBatchInsertSql(string $driverName): array |
||
| 26 | { |
||
| 27 | return [ |
||
| 28 | 'multirow' => [ |
||
| 29 | 'type', |
||
| 30 | ['int_col', 'float_col', 'char_col', 'bool_col'], |
||
| 31 | 'values' => [ |
||
| 32 | ['0', '0.0', 'test string', true,], |
||
| 33 | [false, 0, 'test string2', false,], |
||
| 34 | ], |
||
| 35 | 'expected' => DbHelper::replaceQuotes( |
||
| 36 | 'INSERT INTO [[type]] ([[int_col]], [[float_col]], [[char_col]], [[bool_col]])' |
||
| 37 | . ' VALUES (:qp0, :qp1, :qp2, :qp3), (:qp4, :qp5, :qp6, :qp7)', |
||
| 38 | $driverName, |
||
| 39 | ), |
||
| 40 | 'expectedParams' => [ |
||
| 41 | ':qp0' => 0, |
||
| 42 | ':qp1' => 0.0, |
||
| 43 | ':qp2' => 'test string', |
||
| 44 | ':qp3' => true, |
||
| 45 | ':qp4' => 0, |
||
| 46 | ':qp5' => 0.0, |
||
| 47 | ':qp6' => 'test string2', |
||
| 48 | ':qp7' => false, |
||
| 49 | ], |
||
| 50 | 2, |
||
| 51 | ], |
||
| 52 | 'issue11242' => [ |
||
| 53 | 'type', |
||
| 54 | ['int_col', 'float_col', 'char_col', 'bool_col'], |
||
| 55 | 'values' => [[1.0, 1.1, 'Kyiv {{city}}, Ukraine', true]], |
||
| 56 | /** |
||
| 57 | * {@see https://github.com/yiisoft/yii2/issues/11242} |
||
| 58 | * |
||
| 59 | * Make sure curly bracelets (`{{..}}`) in values will not be escaped |
||
| 60 | */ |
||
| 61 | 'expected' => DbHelper::replaceQuotes( |
||
| 62 | 'INSERT INTO [[type]] ([[int_col]], [[float_col]], [[char_col]], [[bool_col]])' |
||
| 63 | . ' VALUES (:qp0, :qp1, :qp2, :qp3)', |
||
| 64 | $driverName, |
||
| 65 | ), |
||
| 66 | 'expectedParams' => [ |
||
| 67 | ':qp0' => 1, |
||
| 68 | ':qp1' => 1.1, |
||
| 69 | ':qp2' => 'Kyiv {{city}}, Ukraine', |
||
| 70 | ':qp3' => true, |
||
| 71 | ], |
||
| 72 | ], |
||
| 73 | 'wrongBehavior' => [ |
||
| 74 | '{{%type}}', |
||
| 75 | ['{{%type}}.[[int_col]]', '[[float_col]]', 'char_col', 'bool_col'], |
||
| 76 | 'values' => [['0', '0.0', 'Kyiv {{city}}, Ukraine', false]], |
||
| 77 | /** |
||
| 78 | * Test covers potentially wrong behavior and marks it as expected!. |
||
| 79 | * |
||
| 80 | * In case table name or table column is passed with curly or square bracelets, QueryBuilder can not |
||
| 81 | * determine the table schema and typecast values properly. |
||
| 82 | * TODO: make it work. Impossible without BC breaking for public methods. |
||
| 83 | */ |
||
| 84 | 'expected' => DbHelper::replaceQuotes( |
||
| 85 | 'INSERT INTO [[type]] ([[type]].[[int_col]], [[float_col]], [[char_col]], [[bool_col]])' |
||
| 86 | . ' VALUES (:qp0, :qp1, :qp2, :qp3)', |
||
| 87 | $driverName, |
||
| 88 | ), |
||
| 89 | 'expectedParams' => [ |
||
| 90 | ':qp0' => '0', |
||
| 91 | ':qp1' => '0.0', |
||
| 92 | ':qp2' => 'Kyiv {{city}}, Ukraine', |
||
| 93 | ':qp3' => false, |
||
| 94 | ], |
||
| 95 | ], |
||
| 96 | 'batchInsert binds params from expression' => [ |
||
| 97 | '{{%type}}', |
||
| 98 | ['int_col', 'float_col', 'char_col', 'bool_col'], |
||
| 99 | /** |
||
| 100 | * This example is completely useless. This feature of batchInsert is intended to be used with complex |
||
| 101 | * expression objects, such as JsonExpression. |
||
| 102 | */ |
||
| 103 | 'values' => [[new Expression(':exp1', [':exp1' => 42]), 1, 'test', false]], |
||
| 104 | 'expected' => DbHelper::replaceQuotes( |
||
| 105 | 'INSERT INTO [[type]] ([[int_col]], [[float_col]], [[char_col]], [[bool_col]])' |
||
| 106 | . ' VALUES (:exp1, :qp1, :qp2, :qp3)', |
||
| 107 | $driverName, |
||
| 108 | ), |
||
| 109 | 'expectedParams' => [ |
||
| 110 | ':exp1' => 42, |
||
| 111 | ':qp1' => 1.0, |
||
| 112 | ':qp2' => 'test', |
||
| 113 | ':qp3' => false, |
||
| 114 | ], |
||
| 371 |