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 |
||
| 22 | final class BladeTest extends TestCase |
||
| 23 | { |
||
| 24 | private function getSourceLocations($relativePath) |
||
| 25 | { |
||
| 26 | $extractor = new BladeFileExtractor(); |
||
| 27 | |||
| 28 | $filename = substr($relativePath, 1 + strrpos($relativePath, '/')); |
||
| 29 | $path = __DIR__.'/../Resources/'.substr($relativePath, 0, strrpos($relativePath, '/')); |
||
| 30 | |||
| 31 | $finder = new Finder(); |
||
| 32 | $finder->files()->name($filename)->in($path); |
||
| 33 | $collection = new SourceCollection(); |
||
| 34 | foreach ($finder as $file) { |
||
| 35 | $extractor->getSourceLocations($file, $collection); |
||
| 36 | } |
||
| 37 | |||
| 38 | return $collection; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testExtractLang() |
||
| 42 | { |
||
| 43 | $collection = $this->getSourceLocations('Blade/lang.blade.php'); |
||
| 44 | |||
| 45 | $this->assertCount(1, $collection); |
||
| 46 | $source = $collection->first(); |
||
| 47 | $this->assertEquals('foo.bar', $source->getMessage()); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function testExtractTrans() |
||
| 51 | { |
||
| 52 | $collection = $this->getSourceLocations('Blade/trans.blade.php'); |
||
| 53 | |||
| 54 | $this->assertCount(1, $collection); |
||
| 55 | $source = $collection->first(); |
||
| 56 | $this->assertEquals('foo.bar', $source->getMessage()); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testExtractTransChoice() |
||
| 60 | { |
||
| 61 | $collection = $this->getSourceLocations('Blade/trans_choice.blade.php'); |
||
| 62 | |||
| 63 | $this->assertCount(1, $collection); |
||
| 64 | $source = $collection->first(); |
||
| 65 | $this->assertEquals('foo.bar', $source->getMessage()); |
||
| 66 | } |
||
| 67 | } |
||
| 68 |