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 |
||
| 34 | class AllExtractorsTest extends TestCase |
||
| 35 | { |
||
| 36 | public function testNoException() |
||
| 37 | { |
||
| 38 | $extractor = new Extractor(); |
||
| 39 | $extractor->addFileExtractor($this->getPHPFileExtractor()); |
||
| 40 | $extractor->addFileExtractor($this->getTwigFileExtractor()); |
||
| 41 | |||
| 42 | $finder = new Finder(); |
||
| 43 | $finder->in(dirname(__DIR__)); |
||
| 44 | |||
| 45 | $sc = $extractor->extract($finder); |
||
| 46 | $this->translationExists($sc, 'trans.issue_34'); |
||
| 47 | $this->translationMissing($sc, 'trans.issue_62'); |
||
| 48 | |||
| 49 | /* |
||
| 50 | * It is okey to increase the error count if you adding more fixtures/code. |
||
| 51 | * We just need to be aware that it changes. |
||
| 52 | */ |
||
| 53 | $this->assertCount(9, $sc->getErrors(), 'There was an unexpected number of errors. Please investigate.'); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Assert that a translation key exists in source collection. |
||
| 58 | * |
||
| 59 | * @param SourceCollection $sc |
||
| 60 | * @param $translationKey |
||
| 61 | * @param string $message |
||
| 62 | */ |
||
| 63 | private function translationExists(SourceCollection $sc, $translationKey, $message = null) |
||
| 64 | { |
||
| 65 | if (empty($message)) { |
||
| 66 | $message = sprintf('Tried to find "%s" but failed', $translationKey); |
||
| 67 | } |
||
| 68 | |||
| 69 | $found = false; |
||
| 70 | foreach ($sc as $source) { |
||
| 71 | if ($translationKey === $source->getMessage()) { |
||
| 72 | $found = true; |
||
| 73 | |||
| 74 | break; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->assertTrue($found, $message); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Assert that a translation key is missing in source collection. |
||
| 83 | * |
||
| 84 | * @param SourceCollection $sc |
||
| 85 | * @param $translationKey |
||
| 86 | * @param string $message |
||
| 87 | */ |
||
| 88 | private function translationMissing(SourceCollection $sc, $translationKey, $message = null) |
||
| 89 | { |
||
| 90 | if (empty($message)) { |
||
| 91 | $message = sprintf('The translation key "%s" should not exist', $translationKey); |
||
| 92 | } |
||
| 93 | |||
| 94 | $found = false; |
||
| 95 | foreach ($sc as $source) { |
||
| 96 | if ($translationKey === $source->getMessage()) { |
||
| 97 | $found = true; |
||
| 98 | |||
| 99 | break; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | $this->assertFalse($found, $message); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @return PHPFileExtractor |
||
| 108 | */ |
||
| 109 | private function getPHPFileExtractor() |
||
| 110 | { |
||
| 111 | $file = new PHPFileExtractor(); |
||
| 112 | $file->addVisitor(new ContainerAwareTrans()); |
||
| 113 | $file->addVisitor(new ContainerAwareTransChoice()); |
||
| 114 | $file->addVisitor(new FlashMessage()); |
||
| 115 | $file->addVisitor(new FormTypeChoices()); |
||
| 116 | $file->addVisitor(new FormTypeLabelExplicit()); |
||
| 117 | $file->addVisitor(new FormTypeLabelImplicit()); |
||
| 118 | |||
| 119 | return $file; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return TwigFileExtractor |
||
| 124 | */ |
||
| 125 | private function getTwigFileExtractor() |
||
| 126 | { |
||
| 127 | $file = new TwigFileExtractor(TwigEnvironmentFactory::create()); |
||
| 128 | $file->addVisitor(TwigVisitorFactory::create()); |
||
| 129 | |||
| 130 | return $file; |
||
| 131 | } |
||
| 132 | } |
||
| 133 |