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