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 | class LocationAwareStore extends Store implements ContentPurger |
||
| 23 | { |
||
| 24 | const LOCATION_CACHE_DIR = 'ezlocation', |
||
| 25 | LOCATION_STALE_CACHE_DIR = 'ezlocation_stale'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \Symfony\Component\Filesystem\Filesystem |
||
| 29 | */ |
||
| 30 | private $fs; |
||
| 31 | |||
| 32 | public function __construct($root) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Injects a Filesystem instance |
||
| 40 | * For unit tests only. |
||
| 41 | * |
||
| 42 | * @internal |
||
| 43 | * |
||
| 44 | * @param \Symfony\Component\Filesystem\Filesystem $fs |
||
| 45 | */ |
||
| 46 | public function setFilesystem(Filesystem $fs) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return \Symfony\Component\Filesystem\Filesystem |
||
| 53 | */ |
||
| 54 | public function getFilesystem() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Injects eZ Publish specific information in the content digest if needed. |
||
| 65 | * X-Location-Id response header is set in the ViewController. |
||
| 66 | * |
||
| 67 | * @see \eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController::viewLocation() |
||
| 68 | * |
||
| 69 | * @param \Symfony\Component\HttpFoundation\Response $response |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | protected function generateContentDigest(Response $response) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Returns the right path where cache is being stored. |
||
| 85 | * Will detect if $key is eZ Publish specific. |
||
| 86 | * |
||
| 87 | * @param string $key |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | public function getPath($key) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Purges data from $request. |
||
| 132 | * If X-Location-Id or X-Group-Location-Id header is present, the store will purge cache for given locationId or group of locationIds. |
||
| 133 | * If not, regular purge by URI will occur. |
||
| 134 | * |
||
| 135 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
| 136 | * |
||
| 137 | * @return bool True if purge was successful. False otherwise |
||
| 138 | */ |
||
| 139 | public function purgeByRequest(Request $request) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Purges all cached content. |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | public function purgeAllContent() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Purges cache for $locationId. |
||
| 185 | * |
||
| 186 | * @param int|null $locationId. If null, all locations will be purged. |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | private function purgeLocation($locationId) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Returns cache lock name for $locationId. |
||
| 225 | * |
||
| 226 | * This method is public only for unit tests. |
||
| 227 | * Use it only if you know what you are doing. |
||
| 228 | * |
||
| 229 | * @internal |
||
| 230 | * |
||
| 231 | * @param int $locationId. If null, will return a global cache lock name (purging all content) |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function getLocationCacheLockName($locationId = null) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Returns cache dir for $locationId. |
||
| 244 | * |
||
| 245 | * This method is public only for unit tests. |
||
| 246 | * Use it only if you know what you are doing. |
||
| 247 | * |
||
| 248 | * @internal |
||
| 249 | * |
||
| 250 | * @param int $locationId |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function getLocationCacheDir($locationId = null) |
||
| 263 | } |
||
| 264 |
If you suppress an error, we recommend checking for the error condition explicitly: