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 |
||
| 10 | /** |
||
| 11 | * @return MagentoHelper |
||
| 12 | */ |
||
| 13 | protected function getHelper() |
||
| 14 | { |
||
| 15 | $inputMock = $this->getMock('Symfony\Component\Console\Input\InputInterface'); |
||
| 16 | $outputMock = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); |
||
| 17 | |||
| 18 | return new MagentoHelper($inputMock, $outputMock); |
||
| 19 | } |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @test |
||
| 23 | */ |
||
| 24 | public function testHelperInstance() |
||
| 25 | { |
||
| 26 | $this->assertInstanceOf('\N98\Util\Console\Helper\MagentoHelper', $this->getHelper()); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @test |
||
| 31 | */ |
||
| 32 | public function detectMagentoInStandardFolder() |
||
| 33 | { |
||
| 34 | vfsStream::setup('root'); |
||
| 35 | vfsStream::create( |
||
| 36 | array( |
||
| 37 | 'app' => array( |
||
| 38 | 'Mage.php' => '', |
||
| 39 | ), |
||
| 40 | ) |
||
| 41 | ); |
||
| 42 | |||
| 43 | $helper = $this->getHelper(); |
||
| 44 | $helper->detect(vfsStream::url('root'), array()); |
||
| 45 | |||
| 46 | $this->assertEquals(vfsStream::url('root'), $helper->getRootFolder()); |
||
| 47 | $this->assertEquals(\N98\Magento\Application::MAGENTO_MAJOR_VERSION_1, $helper->getMajorVersion()); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @test |
||
| 52 | */ |
||
| 53 | View Code Duplication | public function detectMagentoInHtdocsSubfolder() |
|
| 54 | { |
||
| 55 | vfsStream::setup('root'); |
||
| 56 | vfsStream::create( |
||
| 57 | array( |
||
| 58 | 'htdocs' => array( |
||
| 59 | 'app' => array( |
||
| 60 | 'Mage.php' => '', |
||
| 61 | ), |
||
| 62 | ), |
||
| 63 | ) |
||
| 64 | ); |
||
| 65 | |||
| 66 | $helper = $this->getHelper(); |
||
| 67 | |||
| 68 | // vfs cannot resolve relative path so we do 'root/htdocs' etc. |
||
| 69 | $helper->detect( |
||
| 70 | vfsStream::url('root'), |
||
| 71 | array( |
||
| 72 | vfsStream::url('root/www'), |
||
| 73 | vfsStream::url('root/public'), |
||
| 74 | vfsStream::url('root/htdocs'), |
||
| 75 | ) |
||
| 76 | ); |
||
| 77 | |||
| 78 | $this->assertEquals(vfsStream::url('root/htdocs'), $helper->getRootFolder()); |
||
| 79 | $this->assertEquals(\N98\Magento\Application::MAGENTO_MAJOR_VERSION_1, $helper->getMajorVersion()); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @test |
||
| 84 | */ |
||
| 85 | public function detectMagentoFailed() |
||
| 86 | { |
||
| 87 | vfsStream::setup('root'); |
||
| 88 | vfsStream::create( |
||
| 89 | array( |
||
| 90 | 'htdocs' => array(), |
||
| 91 | ) |
||
| 92 | ); |
||
| 93 | |||
| 94 | $helper = $this->getHelper(); |
||
| 95 | |||
| 96 | // vfs cannot resolve relative path so we do 'root/htdocs' etc. |
||
| 97 | $helper->detect( |
||
| 98 | vfsStream::url('root') |
||
| 99 | ); |
||
| 100 | |||
| 101 | $this->assertNull($helper->getRootFolder()); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @test |
||
| 106 | */ |
||
| 107 | public function detectMagentoInModmanInfrastructure() |
||
| 108 | { |
||
| 109 | vfsStream::setup('root'); |
||
| 110 | vfsStream::create( |
||
| 111 | array( |
||
| 112 | '.basedir' => 'root/htdocs/magento_root', |
||
| 113 | 'htdocs' => array( |
||
| 114 | 'magento_root' => array( |
||
| 115 | 'app' => array( |
||
| 116 | 'Mage.php' => '', |
||
| 117 | ), |
||
| 118 | ), |
||
| 119 | ), |
||
| 120 | ) |
||
| 121 | ); |
||
| 122 | |||
| 123 | $helper = $this->getHelper(); |
||
| 124 | |||
| 125 | // vfs cannot resolve relative path so we do 'root/htdocs' etc. |
||
| 126 | $helper->detect( |
||
| 127 | vfsStream::url('root') |
||
| 128 | ); |
||
| 129 | |||
| 130 | // Verify if this could be checked with more elegance |
||
| 131 | $this->assertEquals(vfsStream::url('root/../root/htdocs/magento_root'), $helper->getRootFolder()); |
||
| 132 | |||
| 133 | $this->assertEquals(\N98\Magento\Application::MAGENTO_MAJOR_VERSION_1, $helper->getMajorVersion()); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @test |
||
| 138 | */ |
||
| 139 | View Code Duplication | public function detectMagento2InHtdocsSubfolder() |
|
| 140 | { |
||
| 141 | vfsStream::setup('root'); |
||
| 142 | vfsStream::create( |
||
| 143 | array( |
||
| 144 | 'htdocs' => array( |
||
| 145 | 'app' => array( |
||
| 146 | 'autoload.php' => '', |
||
| 147 | 'bootstrap.php' => '', |
||
| 148 | ), |
||
| 149 | ), |
||
| 150 | ) |
||
| 151 | ); |
||
| 152 | |||
| 153 | $helper = $this->getHelper(); |
||
| 154 | |||
| 155 | // vfs cannot resolve relative path so we do 'root/htdocs' etc. |
||
| 156 | $helper->detect( |
||
| 157 | vfsStream::url('root'), |
||
| 158 | array( |
||
| 159 | vfsStream::url('root/www'), |
||
| 160 | vfsStream::url('root/public'), |
||
| 161 | vfsStream::url('root/htdocs'), |
||
| 162 | ) |
||
| 163 | ); |
||
| 164 | |||
| 165 | $this->assertEquals(vfsStream::url('root/htdocs'), $helper->getRootFolder()); |
||
| 166 | $this->assertEquals(\N98\Magento\Application::MAGENTO_MAJOR_VERSION_2, $helper->getMajorVersion()); |
||
| 167 | } |
||
| 168 | } |
||
| 169 |
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.