Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 14 | class BreakerTest extends \PHPUnit_Framework_TestCase |
||
| 15 | { |
||
| 16 | protected $dir; |
||
| 17 | |||
| 18 | public function setUp() |
||
| 22 | |||
| 23 | /** |
||
| 24 | * testMultiProcess. |
||
| 25 | */ |
||
| 26 | public function testMultiProcess() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * testOpenBehavior. |
||
| 53 | */ |
||
| 54 | public function testOpenBehavior() |
||
| 55 | { |
||
| 56 | $breaker = new Breaker( |
||
| 57 | 'exception breaker', |
||
| 58 | ['exclude_exceptions' => ['Eljam\CircuitBreaker\Exception\CustomException']] |
||
| 59 | ); |
||
| 60 | |||
| 61 | $breaker->addListener(CircuitEvents::OPEN, function (CircuitEvent $event) { |
||
| 62 | $this->assertInstanceOf('Eljam\CircuitBreaker\Circuit', $event->getCircuit()); |
||
| 63 | }); |
||
| 64 | |||
| 65 | $this->setExpectedException('Eljam\CircuitBreaker\Exception\CircuitOpenException'); |
||
| 66 | |||
| 67 | $fn = function () { |
||
| 68 | throw new CustomException("An error as occurred"); |
||
| 69 | }; |
||
| 70 | |||
| 71 | for ($i = 0; $i <= 5; $i++) { |
||
| 72 | $breaker->protect($fn); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * testHalfOpenBehavior. |
||
| 78 | */ |
||
| 79 | public function testHalfOpenBehavior() |
||
| 80 | { |
||
| 81 | $breaker = new Breaker( |
||
| 82 | 'exception breaker', |
||
| 83 | [ |
||
| 84 | 'reset_timeout' => 1, |
||
| 85 | 'ignore_exceptions' => true, |
||
| 86 | ] |
||
| 87 | ); |
||
| 88 | |||
| 89 | $breaker->addListener(CircuitEvents::HALF_OPEN, function (CircuitEvent $event) { |
||
| 90 | $this->assertInstanceOf('Eljam\CircuitBreaker\Circuit', $event->getCircuit()); |
||
| 91 | }); |
||
| 92 | |||
| 93 | $fn = function () { |
||
| 94 | throw new CustomException("An error as occurred"); |
||
| 95 | }; |
||
| 96 | |||
| 97 | try { |
||
| 98 | for ($i = 0; $i <= 5; $i++) { |
||
| 99 | $breaker->protect($fn); |
||
| 100 | } |
||
| 101 | } catch (CircuitOpenException $e) { |
||
| 102 | $this->assertSame('Eljam\CircuitBreaker\Exception\CircuitOpenException', get_class($e)); |
||
| 103 | } |
||
| 104 | |||
| 105 | sleep(2); |
||
| 106 | |||
| 107 | $fnPass = function () { |
||
| 108 | return 'ok'; |
||
| 109 | }; |
||
| 110 | |||
| 111 | $breaker->protect($fnPass); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * testGetTheResult. |
||
| 116 | */ |
||
| 117 | public function testGetTheResult() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * testIgnoreException. |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function testIgnoreAllException() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * testThrowCustomException. |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function testThrowCustomException() |
|
| 173 | |||
| 174 | public function testAllowedException() |
||
| 175 | { |
||
| 202 | |||
| 203 | public function tearDown() |
||
| 207 | } |
||
| 208 |
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.