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 ContainerConfigTest extends TestCase |
||
| 35 | { |
||
| 36 | use ContainerAwareTrait; |
||
| 37 | |||
| 38 | private $containerConfig; |
||
| 39 | |||
| 40 | protected function setUp() |
||
| 41 | { |
||
| 42 | $this->setConfigProviders([ModuleConfig::class, TestConfig::class]); |
||
| 43 | $this->setContainerConfigs([ |
||
| 44 | ContainerConfig::class, |
||
| 45 | ExpressiveAuraConfig::class, |
||
| 46 | TestContainerConfig::class |
||
| 47 | ]); |
||
| 48 | $this->setUpContainer(); |
||
| 49 | |||
| 50 | $this->containerConfig = new ContainerConfig(['testconfig']); |
||
| 51 | } |
||
| 52 | |||
| 53 | public function testGetConfig() |
||
| 54 | { |
||
| 55 | $this->assertSame(['testconfig'], $this->containerConfig->getConfig()); |
||
| 56 | } |
||
| 57 | |||
| 58 | public function testDefine() |
||
| 59 | { |
||
| 60 | $container = $this->getContainer(); |
||
| 61 | |||
| 62 | $this->assertInstanceOf(BlogOverviewAction::class, $container->get(BlogOverviewAction::class)); |
||
| 63 | $this->assertInstanceOf(SingleBlogEntryAction::class, $container->get(SingleBlogEntryAction::class)); |
||
| 64 | $this->assertInstanceOf(ParseContentCommand::class, $container->get(ParseContentCommand::class)); |
||
| 65 | $this->assertInstanceOf(ContentCreator::class, $container->get(ContentCreator::class)); |
||
| 66 | |||
| 67 | $markdownFilterIterator = $container->get(MarkdownFileIterator::class); |
||
| 68 | |||
| 69 | $this->assertInstanceOf(MarkdownFileIterator::class, $markdownFilterIterator); |
||
| 70 | $recursiveIteratorIterator = $markdownFilterIterator->getInnerIterator(); |
||
| 71 | $this->assertInstanceOf(\RecursiveIteratorIterator::class, $recursiveIteratorIterator); |
||
| 72 | $directoryIterator = $recursiveIteratorIterator->getInnerIterator(); |
||
| 73 | $this->assertInstanceOf(\RecursiveDirectoryIterator::class, $directoryIterator); |
||
| 74 | $this->assertSame('test/_files', $directoryIterator->getPath()); |
||
| 75 | |||
| 76 | |||
| 77 | $parser = $container->get(Parser::class); |
||
| 78 | $this->assertInstanceOf(Parser::class, $parser); |
||
| 79 | $this->assertInstanceOf(Parser::class, $parser); |
||
| 80 | $reflectionObject = new \ReflectionObject($parser); |
||
| 81 | $reflectionProperty = $reflectionObject->getProperty('markdownParser'); |
||
| 82 | $reflectionProperty->setAccessible(true); |
||
| 83 | $this->assertInstanceOf(CommonMarkParser::class, $reflectionProperty->getValue($parser)); |
||
| 84 | |||
| 85 | $this->assertInstanceOf(MarkdownDocumentParser::class, $container->get(MarkdownDocumentParser::class)); |
||
| 86 | $this->assertInstanceOf(SaveContentConfigObserver::class, $container->get(SaveContentConfigObserver::class)); |
||
| 87 | $this->assertInstanceOf(SitemapObserver::class, $container->get(SitemapObserver::class)); |
||
| 88 | $this->assertInstanceOf(ContentParser::class, $container->get(ContentParser::class)); |
||
| 89 | $this->assertInstanceOf(ParseContentExecutor::class, $container->get(ParseContentExecutor::class)); |
||
| 90 | $this->assertInstanceOf(EntryParser::class, $container->get(EntryParser::class)); |
||
| 91 | $this->assertInstanceOf(TagArchiveAction::class, $container->get(TagArchiveAction::class)); |
||
| 92 | $this->assertInstanceOf(Environment::class, $container->get(Environment::class)); |
||
| 93 | $this->assertInstanceOf(ImageRenderer::class, $container->get(ImageRenderer::class)); |
||
| 94 | } |
||
| 95 | } |