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 |
||
17 | class RuleTest extends TestCase |
||
18 | { |
||
19 | /** |
||
20 | * @expectedException \LogicException |
||
21 | * @expectedExceptionMessage rule type is not defined |
||
22 | */ |
||
23 | public function testIvalidInitializationThrowAndException() |
||
24 | { |
||
25 | $rule = Rule::fromArray([]); |
||
26 | } |
||
27 | |||
28 | public function testContainsConfigurationAsArray() |
||
29 | { |
||
30 | $rule = Rule::fromArray( |
||
31 | $ruleConfiguration = [ |
||
32 | Rule::TYPE_OBJECT => [ |
||
33 | '\DateTime', |
||
34 | ] |
||
35 | ] |
||
36 | ); |
||
37 | |||
38 | $this->assertEquals( |
||
39 | $ruleConfiguration, |
||
40 | $rule->asArray() |
||
41 | ); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @expectedException \Sensorario\Resources\Exceptions\InvalidTypeException |
||
46 | * @expectedExceptionMessageRegex #Oops! Invalid configuration!!!Type `foo` is not valid.# |
||
47 | */ |
||
48 | public function testInvalidTypeIsNotAllowed() |
||
49 | { |
||
50 | $rule = Rule::fromArray([ |
||
51 | 'foo' => [ ] |
||
52 | ]); |
||
53 | |||
54 | $rule->ensureRuleNameIsValid(); |
||
55 | } |
||
56 | |||
57 | public function testCheckFunctionReturnRuleValidityAsBoolean() |
||
58 | { |
||
59 | $rule = Rule::fromArray([ |
||
60 | 'foo' => [ ] |
||
61 | ]); |
||
62 | |||
63 | $this->assertSame( |
||
64 | false, |
||
65 | $rule->isValid() |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | public function testCheckIfValueIsOfRightObjectType() |
||
70 | { |
||
71 | $rule = Rule::fromArray( |
||
72 | $ruleConfiguration = [ |
||
73 | Rule::TYPE_OBJECT => [ |
||
74 | '\DateTime', |
||
75 | ] |
||
76 | ] |
||
77 | ); |
||
78 | |||
79 | $this->assertSame(true, $rule->is(Rule::TYPE_OBJECT)); |
||
80 | $this->assertSame(false, $rule->isNot(Rule::TYPE_OBJECT)); |
||
81 | $this->assertSame(['\DateTime'], $rule->getExpectedType()); |
||
82 | } |
||
83 | } |
||
84 |