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 |
||
| 24 | class TokenTest extends \PHPUnit_Framework_TestCase |
||
| 25 | { |
||
| 26 | public function testCreation() |
||
| 27 | { |
||
| 28 | $rule = new Rule(); |
||
| 29 | $token = new Token(['test.name', 'pos' => 10, 'index' => 10, 'rule' => $rule]); |
||
| 30 | |||
| 31 | $this->assertEquals($token->name, 'test.name', 'Token name is invalid'); |
||
| 32 | $this->assertEquals($token->pos, 10, 'Position is invalid'); |
||
| 33 | $this->assertEquals($token->index, 10, 'Index is invalid'); |
||
| 34 | $this->assertEquals($token->getRule(), $rule, 'Rule is invalid'); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function testCreationWithStart() |
||
| 38 | { |
||
| 39 | $start = new Token(['test.name', 'pos' => 5]); |
||
| 40 | $token = new Token(['test.name', 'pos' => 10, 'start' => $start]); |
||
| 41 | |||
| 42 | $this->assertEquals($token->getStart(), $start, 'Token is not pointing to start.'); |
||
| 43 | $this->assertEquals($start->getEnd(), $token, 'Start is not pointing to token.'); |
||
| 44 | } |
||
| 45 | |||
| 46 | public function testCreationWithEnd() |
||
| 47 | { |
||
| 48 | $end = new Token(['test.name', 'pos' => 15]); |
||
| 49 | $token = new Token(['test.name', 'pos' => 10, 'end' => $end]); |
||
| 50 | |||
| 51 | $this->assertEquals($token->getEnd(), $end, 'Token is not pointing to end.'); |
||
| 52 | $this->assertEquals($end->getStart(), $token, 'End is not pointing to token.'); |
||
| 53 | } |
||
| 54 | |||
| 55 | public function testCreationWithLength() |
||
| 56 | { |
||
| 57 | $token = new Token(['test.name', 'pos' => 15, 'length' => 10]); |
||
| 58 | |||
| 59 | $this->assertEquals($token->getEnd()->pos, 25, 'Token is not pointing to end.'); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function testLength() |
||
| 63 | { |
||
| 64 | $token = new Token(['test.name', 'pos' => 15, 'length' => 10]); |
||
| 65 | |||
| 66 | $this->assertEquals($token->getLength(), 10, 'Length is invalid'); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function testIsStart() |
||
| 70 | { |
||
| 71 | $token = new Token(['test.name', 'pos' => 10]); |
||
| 72 | $close = new Token(['test.name', 'pos' => 10, 'rule' => new CloseRule()]); |
||
| 73 | |||
| 74 | $this->assertTrue($token->isStart()); |
||
| 75 | $this->assertFalse($close->isStart()); |
||
| 76 | } |
||
| 77 | |||
| 78 | public function testIsEnd() |
||
| 79 | { |
||
| 80 | $token = new Token(['test.name', 'pos' => 15]); |
||
| 81 | new Token(['test.name', 'pos' => 10, 'end' => $token]); |
||
| 82 | |||
| 83 | $close = new Token(['test.name', 'pos' => 10, 'rule' => new OpenRule()]); |
||
| 84 | |||
| 85 | $this->assertTrue($token->isEnd()); |
||
| 86 | $this->assertFalse($close->isEnd()); |
||
| 87 | } |
||
| 88 | } |
||
| 89 |