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 |
||
| 14 | abstract class FileBaseIntegrationTest extends BaseIntegrationTest |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Temporary storage directory. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected static $tmpDir; |
||
| 22 | |||
| 23 | /** @var IOServiceInterface */ |
||
| 24 | protected $ioService; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @see EZP-23534 |
||
| 28 | */ |
||
| 29 | public function testLoadingContentWithMissingFileWorks() |
||
| 30 | { |
||
| 31 | $contentType = $this->createContentType(); |
||
| 32 | $content = $this->createContent($contentType, $this->getInitialValue()); |
||
| 33 | |||
| 34 | // delete the binary file object |
||
| 35 | $this->deleteStoredFile($content); |
||
| 36 | |||
| 37 | // try loading the content again. It should work even though the image isn't physically here |
||
| 38 | $this->getCustomHandler()->contentHandler()->load($content->versionInfo->contentInfo->id, 1); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Deletes the binary file stored in the field. |
||
| 43 | * |
||
| 44 | * @param $content |
||
| 45 | * |
||
| 46 | * @return mixed |
||
| 47 | */ |
||
| 48 | protected function deleteStoredFile($content) |
||
| 49 | { |
||
| 50 | return $this->ioService->deleteBinaryFile( |
||
| 51 | $this->ioService->loadBinaryFile($content->fields[1]->value->externalData['id']) |
||
| 52 | ); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Returns prefix used by the IOService. |
||
| 57 | * |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | abstract protected function getStoragePrefix(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Sets up a temporary directory and stores its path in self::$tmpDir. |
||
| 64 | */ |
||
| 65 | public static function setUpBeforeClass() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Removes the temp dir. |
||
| 93 | */ |
||
| 94 | public static function tearDownAfterClass() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Removes the given directory path recursively. |
||
| 102 | * |
||
| 103 | * @param string $dir |
||
| 104 | */ |
||
| 105 | protected static function removeRecursive($dir) |
||
| 125 | |||
| 126 | View Code Duplication | protected function getContainer() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Asserts that the IO File with uri $uri exists. |
||
| 173 | * |
||
| 174 | * @param string $uri |
||
| 175 | */ |
||
| 176 | protected function assertIOUriExists($uri) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Asserts that the IO File with id $id exists. |
||
| 186 | * |
||
| 187 | * @param string $id |
||
| 188 | */ |
||
| 189 | protected function assertIOIdExists($id) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns the physical path to the file with id $id. |
||
| 200 | */ |
||
| 201 | protected function getPathFromId($id) |
||
| 205 | |||
| 206 | protected function getStorageDir() |
||
| 210 | |||
| 211 | protected function getFilesize($binaryFileId) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Overrides all services to be public. |
||
| 218 | * |
||
| 219 | * It is a workaround to the change in Symfony 4 which makes all services private by default. |
||
| 220 | * Our integration tests are not prepared for this as they get services directly from the Container. |
||
| 221 | * |
||
| 222 | * Inspired by {@link \Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TestServiceContainerWeakRefPass} |
||
| 223 | * |
||
| 224 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $containerBuilder |
||
| 225 | */ |
||
| 226 | View Code Duplication | private function setAllServicesPublic(ContainerBuilder $containerBuilder): void |
|
| 260 | } |
||
| 261 |
Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.