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 |
||
| 15 | class AccountProcessorSpec extends ObjectBehavior |
||
| 16 | { |
||
| 17 | function let( |
||
| 18 | AccountFactory $accountFactory, |
||
| 19 | AccountFactory $bankgiroFactory, |
||
| 20 | AccountNode $accountNode, |
||
| 21 | BankgiroNode $bankgiroNode, |
||
| 22 | AccountNumber $accountNumber |
||
| 23 | ) { |
||
| 24 | $accountFactory->createAccount('not-valid')->willThrow(BankingException::CLASS); |
||
| 25 | $accountFactory->createAccount('valid')->willReturn($accountNumber); |
||
| 26 | |||
| 27 | $bankgiroFactory->createAccount('not-valid')->willThrow(BankingException::CLASS); |
||
| 28 | $bankgiroFactory->createAccount('valid')->willReturn($accountNumber); |
||
| 29 | |||
| 30 | $accountNode->getLineNr()->willReturn(1); |
||
| 31 | $accountNode->getType()->willReturn('AccountNode'); |
||
| 32 | |||
| 33 | $bankgiroNode->getLineNr()->willReturn(1); |
||
| 34 | $bankgiroNode->getType()->willReturn('BankgiroNode'); |
||
| 35 | |||
| 36 | $this->beConstructedWith($accountFactory, $bankgiroFactory); |
||
| 37 | } |
||
| 38 | |||
| 39 | function it_is_initializable() |
||
| 40 | { |
||
| 41 | $this->shouldHaveType(AccountProcessor::CLASS); |
||
| 42 | } |
||
| 43 | |||
| 44 | function it_fails_on_unvalid_account_number($accountNode) |
||
| 45 | { |
||
| 46 | $accountNode->getValue()->willReturn('not-valid'); |
||
| 47 | $this->visitBefore($accountNode); |
||
| 48 | $this->getErrors()->shouldHaveCount(1); |
||
| 49 | } |
||
| 50 | |||
| 51 | function it_creates_valid_account_numbers($accountNode, $accountNumber) |
||
| 52 | { |
||
| 53 | $accountNode->getValue()->willReturn('valid'); |
||
| 54 | $accountNode->setAttribute('account', $accountNumber)->shouldBeCalled(); |
||
| 55 | $this->visitBefore($accountNode); |
||
| 56 | $this->getErrors()->shouldHaveCount(0); |
||
| 57 | } |
||
| 58 | |||
| 59 | function it_fails_on_unvalid_bankgiro_number($bankgiroNode) |
||
| 60 | { |
||
| 61 | $bankgiroNode->getValue()->willReturn('not-valid'); |
||
| 62 | $this->visitBefore($bankgiroNode); |
||
| 63 | $this->getErrors()->shouldHaveCount(1); |
||
| 64 | } |
||
| 65 | |||
| 66 | function it_creates_valid_bankgiro_numbers($bankgiroNode, $accountNumber) |
||
| 67 | { |
||
| 68 | $bankgiroNode->getValue()->willReturn('valid'); |
||
| 69 | $bankgiroNode->setAttribute('account', $accountNumber)->shouldBeCalled(); |
||
| 70 | $this->visitBefore($bankgiroNode); |
||
| 71 | $this->getErrors()->shouldHaveCount(0); |
||
| 72 | } |
||
| 73 | } |
||
| 74 |