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 |
||
| 29 | class WebPHandler extends BitmapHandler { |
||
| 30 | const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata. |
||
| 31 | /** |
||
| 32 | * @var int Minimum chunk header size to be able to read all header types |
||
| 33 | */ |
||
| 34 | const MINIMUM_CHUNK_HEADER_LENGTH = 18; |
||
| 35 | /** |
||
| 36 | * @var int version of the metadata stored in db records |
||
| 37 | */ |
||
| 38 | const _MW_WEBP_VERSION = 1; |
||
| 39 | |||
| 40 | const VP8X_ICC = 32; |
||
| 41 | const VP8X_ALPHA = 16; |
||
| 42 | const VP8X_EXIF = 8; |
||
| 43 | const VP8X_XMP = 4; |
||
| 44 | const VP8X_ANIM = 2; |
||
| 45 | |||
| 46 | public function getMetadata( $image, $filename ) { |
||
| 55 | |||
| 56 | public function getMetadataType( $image ) { |
||
| 59 | |||
| 60 | View Code Duplication | public function isMetadataValid( $image, $metadata ) { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Extracts the image size and WebP type from a file |
||
| 88 | * |
||
| 89 | * @param string $chunks Chunks as extracted by RiffExtractor |
||
|
|
|||
| 90 | * @return array|bool Header data array with entries 'compression', 'width' and 'height', |
||
| 91 | * where 'compression' can be 'lossy', 'lossless', 'animated' or 'unknown'. False if |
||
| 92 | * file is not a valid WebP file. |
||
| 93 | */ |
||
| 94 | public static function extractMetadata( $filename ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Extracts the image size and WebP type from a file based on the chunk list |
||
| 120 | * @param array $chunks Chunks as extracted by RiffExtractor |
||
| 121 | * @return array Header data array with entries 'compression', 'width' and 'height', where |
||
| 122 | * 'compression' can be 'lossy', 'lossless', 'animated' or 'unknown' |
||
| 123 | */ |
||
| 124 | public static function extractMetadataFromChunks( $chunks, $filename ) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Decodes a lossy chunk header |
||
| 156 | * @param string $header Header string |
||
| 157 | * @return bool|array See WebPHandler::decodeHeader |
||
| 158 | */ |
||
| 159 | protected static function decodeLossyChunkHeader( $header ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Decodes a lossless chunk header |
||
| 182 | * @param string $header Header string |
||
| 183 | * @return bool|array See WebPHandler::decodeHeader |
||
| 184 | */ |
||
| 185 | public static function decodeLosslessChunkHeader( $header ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Decodes an extended chunk header |
||
| 207 | * @param string $header Header string |
||
| 208 | * @return bool|array See WebPHandler::decodeHeader |
||
| 209 | */ |
||
| 210 | public static function decodeExtendedChunkHeader( $header ) { |
||
| 228 | |||
| 229 | public function getImageSize( $file, $path, $metadata = false ) { |
||
| 230 | if ( $file === null ) { |
||
| 231 | $metadata = self::getMetadata( $file, $path ); |
||
| 232 | } |
||
| 233 | if ( $metadata === false && $file instanceof File ) { |
||
| 234 | $metadata = $file->getMetadata(); |
||
| 235 | } |
||
| 236 | |||
| 237 | MediaWiki\suppressWarnings(); |
||
| 238 | $metadata = unserialize( $metadata ); |
||
| 239 | MediaWiki\restoreWarnings(); |
||
| 240 | |||
| 241 | if ( $metadata == false ) { |
||
| 242 | return false; |
||
| 243 | } |
||
| 244 | return [ $metadata['width'], $metadata['height'] ]; |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param $file |
||
| 249 | * @return bool True, not all browsers support WebP |
||
| 250 | */ |
||
| 251 | public function mustRender( $file ) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param $file |
||
| 257 | * @return bool False if we are unable to render this image |
||
| 258 | */ |
||
| 259 | public function canRender( $file ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param File $image |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | View Code Duplication | public function isAnimatedImage( $image ) { |
|
| 281 | |||
| 282 | public function canAnimateThumbnail( $file ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Render files as PNG |
||
| 288 | * |
||
| 289 | * @param $ext |
||
| 290 | * @param $mime |
||
| 291 | * @param $params |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | public function getThumbType( $ext, $mime, $params = null ) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Must use "im" for XCF |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | protected function getScalerType( $dstPath, $checkDstPath = true ) { |
||
| 306 | } |
||
| 307 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.