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 |
||
| 13 | class AmountProcessorSpec extends ObjectBehavior |
||
| 14 | { |
||
| 15 | function it_is_initializable() |
||
| 16 | { |
||
| 17 | $this->shouldHaveType(AmountProcessor::CLASS); |
||
| 18 | } |
||
| 19 | |||
| 20 | function it_fails_on_unvalid_amounts(AmountNode $amountNode) |
||
| 21 | { |
||
| 22 | $amountNode->getLineNr()->willReturn(1); |
||
| 23 | $amountNode->getType()->willReturn('AmountNode'); |
||
| 24 | $amountNode->getValue()->willReturn('this-is-not-a-valid-signal-string'); |
||
| 25 | $this->visitBefore($amountNode); |
||
| 26 | $this->getErrors()->shouldHaveCount(1); |
||
| 27 | } |
||
| 28 | |||
| 29 | function it_creates_valid_amounts(AmountNode $amountNode) |
||
| 30 | { |
||
| 31 | $amountNode->getType()->willReturn('AmountNode'); |
||
| 32 | $amountNode->getValue()->willReturn('1230K'); |
||
| 33 | |||
| 34 | $amountNode->setAttribute('amount', Argument::exact(new SEK('-123.02')))->shouldBeCalled(); |
||
| 35 | |||
| 36 | $this->visitBefore($amountNode); |
||
| 37 | $this->getErrors()->shouldHaveCount(0); |
||
| 38 | } |
||
| 39 | } |
||
| 40 |