| Conditions | 1 |
| Paths | 1 |
| Total Lines | 70 |
| Code Lines | 45 |
| 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 |
||
| 22 | public function testCommand(): void |
||
| 23 | { |
||
| 24 | $db = $this->getConnectionWithData(); |
||
| 25 | |||
| 26 | $db->queryCacheEnable(true); |
||
| 27 | $command = $db->createCommand( |
||
| 28 | <<<SQL |
||
| 29 | SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id |
||
| 30 | SQL, |
||
| 31 | ); |
||
| 32 | |||
| 33 | $this->assertSame('user1', $command->bindValue(':id', 1)->queryScalar()); |
||
| 34 | |||
| 35 | $update = $db->createCommand( |
||
| 36 | <<<SQL |
||
| 37 | UPDATE {{customer}} SET [[name]] = :name WHERE [[id]] = :id |
||
| 38 | SQL, |
||
| 39 | ); |
||
| 40 | $update->bindValues([':id' => 1, ':name' => 'user11'])->execute(); |
||
| 41 | |||
| 42 | $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar()); |
||
| 43 | |||
| 44 | $db->cache(function (ConnectionPDOInterface $db) use ($command, $update) { |
||
| 45 | $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar()); |
||
| 46 | |||
| 47 | $update->bindValues([':id' => 2, ':name' => 'user22'])->execute(); |
||
| 48 | |||
| 49 | $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar()); |
||
| 50 | |||
| 51 | $db->noCache(function () use ($command) { |
||
| 52 | $this->assertEquals('user22', $command->bindValue(':id', 2)->queryScalar()); |
||
| 53 | }); |
||
| 54 | |||
| 55 | $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar()); |
||
| 56 | }, 10); |
||
| 57 | |||
| 58 | $db->queryCacheEnable(false); |
||
| 59 | |||
| 60 | $db->cache(function () use ($command, $update) { |
||
| 61 | $this->assertSame('user22', $command->bindValue(':id', 2)->queryScalar()); |
||
| 62 | |||
| 63 | $update->bindValues([':id' => 2, ':name' => 'user2'])->execute(); |
||
| 64 | |||
| 65 | $this->assertSame('user2', $command->bindValue(':id', 2)->queryScalar()); |
||
| 66 | }, 10); |
||
| 67 | |||
| 68 | $db->queryCacheEnable(true); |
||
| 69 | $command = $db->createCommand( |
||
| 70 | <<<SQL |
||
| 71 | SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id |
||
| 72 | SQL, |
||
| 73 | )->cache(); |
||
| 74 | |||
| 75 | $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar()); |
||
| 76 | |||
| 77 | $update->bindValues([':id' => 1, ':name' => 'user1'])->execute(); |
||
| 78 | |||
| 79 | $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar()); |
||
| 80 | $this->assertSame('user1', $command->noCache()->bindValue(':id', 1)->queryScalar()); |
||
| 81 | |||
| 82 | $command = $db->createCommand( |
||
| 83 | <<<SQL |
||
| 84 | SELECT [[name]] FROM {{customer}} WHERE [[id]] = :id |
||
| 85 | SQL, |
||
| 86 | ); |
||
| 87 | |||
| 88 | $db->cache(function () use ($command) { |
||
| 89 | $this->assertSame('user11', $command->bindValue(':id', 1)->queryScalar()); |
||
| 90 | $this->assertSame('user1', $command->noCache()->bindValue(':id', 1)->queryScalar()); |
||
| 91 | }, 10); |
||
| 92 | } |
||
| 182 |