| Conditions | 24 |
| Paths | 3480 |
| Total Lines | 185 |
| Code Lines | 111 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 35 | function toHTML() { |
||
| 36 | if ( $this->mPerRow > 0 ) { |
||
| 37 | $maxwidth = $this->mPerRow * ( $this->mWidths + $this->getAllPadding() ); |
||
| 38 | $oldStyle = isset( $this->mAttribs['style'] ) ? $this->mAttribs['style'] : ''; |
||
| 39 | # _width is ignored by any sane browser. IE6 doesn't know max-width |
||
| 40 | # so it uses _width instead |
||
| 41 | $this->mAttribs['style'] = "max-width: {$maxwidth}px;_width: {$maxwidth}px;" . |
||
| 42 | $oldStyle; |
||
| 43 | } |
||
| 44 | |||
| 45 | $attribs = Sanitizer::mergeAttributes( |
||
| 46 | [ 'class' => 'gallery mw-gallery-' . $this->mMode ], $this->mAttribs ); |
||
| 47 | |||
| 48 | $modules = $this->getModules(); |
||
| 49 | |||
| 50 | if ( $this->mParser ) { |
||
| 51 | $this->mParser->getOutput()->addModules( $modules ); |
||
| 52 | $this->mParser->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' ); |
||
| 53 | } else { |
||
| 54 | $this->getOutput()->addModules( $modules ); |
||
| 55 | $this->getOutput()->addModuleStyles( 'mediawiki.page.gallery.styles' ); |
||
| 56 | } |
||
| 57 | $output = Xml::openElement( 'ul', $attribs ); |
||
| 58 | if ( $this->mCaption ) { |
||
| 59 | $output .= "\n\t<li class='gallerycaption'>{$this->mCaption}</li>"; |
||
| 60 | } |
||
| 61 | |||
| 62 | if ( $this->mShowFilename ) { |
||
| 63 | // Preload LinkCache info for when generating links |
||
| 64 | // of the filename below |
||
| 65 | $lb = new LinkBatch(); |
||
| 66 | foreach ( $this->mImages as $img ) { |
||
| 67 | $lb->addObj( $img[0] ); |
||
| 68 | } |
||
| 69 | $lb->execute(); |
||
| 70 | } |
||
| 71 | |||
| 72 | $lang = $this->getRenderLang(); |
||
| 73 | # Output each image... |
||
| 74 | foreach ( $this->mImages as $pair ) { |
||
| 75 | /** @var Title $nt */ |
||
| 76 | $nt = $pair[0]; |
||
| 77 | $text = $pair[1]; # "text" means "caption" here |
||
| 78 | $alt = $pair[2]; |
||
| 79 | $link = $pair[3]; |
||
| 80 | |||
| 81 | $descQuery = false; |
||
| 82 | if ( $nt->getNamespace() === NS_FILE ) { |
||
| 83 | # Get the file... |
||
| 84 | if ( $this->mParser instanceof Parser ) { |
||
| 85 | # Give extensions a chance to select the file revision for us |
||
| 86 | $options = []; |
||
| 87 | Hooks::run( 'BeforeParserFetchFileAndTitle', |
||
| 88 | [ $this->mParser, $nt, &$options, &$descQuery ] ); |
||
| 89 | # Fetch and register the file (file title may be different via hooks) |
||
| 90 | list( $img, $nt ) = $this->mParser->fetchFileAndTitle( $nt, $options ); |
||
| 91 | } else { |
||
| 92 | $img = wfFindFile( $nt ); |
||
| 93 | } |
||
| 94 | } else { |
||
| 95 | $img = false; |
||
| 96 | } |
||
| 97 | |||
| 98 | $params = $this->getThumbParams( $img ); |
||
| 99 | // $pair[4] is per image handler options |
||
| 100 | $transformOptions = $params + $pair[4]; |
||
| 101 | |||
| 102 | $thumb = false; |
||
| 103 | |||
| 104 | if ( !$img ) { |
||
| 105 | # We're dealing with a non-image, spit out the name and be done with it. |
||
| 106 | $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' |
||
| 107 | . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' |
||
| 108 | . htmlspecialchars( $nt->getText() ) . '</div>'; |
||
| 109 | |||
| 110 | if ( $this->mParser instanceof Parser ) { |
||
| 111 | $this->mParser->addTrackingCategory( 'broken-file-category' ); |
||
| 112 | } |
||
| 113 | } elseif ( $this->mHideBadImages |
||
| 114 | && wfIsBadImage( $nt->getDBkey(), $this->getContextTitle() ) |
||
| 115 | ) { |
||
| 116 | # The image is blacklisted, just show it as a text link. |
||
| 117 | $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' . |
||
| 118 | ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' . |
||
| 119 | Linker::linkKnown( |
||
| 120 | $nt, |
||
| 121 | htmlspecialchars( $nt->getText() ) |
||
| 122 | ) . |
||
| 123 | '</div>'; |
||
| 124 | } else { |
||
| 125 | $thumb = $img->transform( $transformOptions ); |
||
| 126 | if ( !$thumb ) { |
||
| 127 | # Error generating thumbnail. |
||
| 128 | $thumbhtml = "\n\t\t\t" . '<div class="thumb" style="height: ' |
||
| 129 | . ( $this->getThumbPadding() + $this->mHeights ) . 'px;">' |
||
| 130 | . htmlspecialchars( $img->getLastError() ) . '</div>'; |
||
| 131 | } else { |
||
| 132 | /** @var MediaTransformOutput $thumb */ |
||
| 133 | $vpad = $this->getVPad( $this->mHeights, $thumb->getHeight() ); |
||
| 134 | |||
| 135 | $imageParameters = [ |
||
| 136 | 'desc-link' => true, |
||
| 137 | 'desc-query' => $descQuery, |
||
| 138 | 'alt' => $alt, |
||
| 139 | 'custom-url-link' => $link |
||
| 140 | ]; |
||
| 141 | |||
| 142 | // In the absence of both alt text and caption, fall back on |
||
| 143 | // providing screen readers with the filename as alt text |
||
| 144 | if ( $alt == '' && $text == '' ) { |
||
| 145 | $imageParameters['alt'] = $nt->getText(); |
||
| 146 | } |
||
| 147 | |||
| 148 | $this->adjustImageParameters( $thumb, $imageParameters ); |
||
| 149 | |||
| 150 | Linker::processResponsiveImages( $img, $thumb, $transformOptions ); |
||
| 151 | |||
| 152 | # Set both fixed width and min-height. |
||
| 153 | $thumbhtml = "\n\t\t\t" |
||
| 154 | . '<div class="thumb" style="width: ' |
||
| 155 | . $this->getThumbDivWidth( $thumb->getWidth() ) . 'px;">' |
||
| 156 | # Auto-margin centering for block-level elements. Needed |
||
| 157 | # now that we have video handlers since they may emit block- |
||
| 158 | # level elements as opposed to simple <img> tags. ref |
||
| 159 | # http://css-discuss.incutio.com/?page=CenteringBlockElement |
||
| 160 | . '<div style="margin:' . $vpad . 'px auto;">' |
||
| 161 | . $thumb->toHtml( $imageParameters ) . '</div></div>'; |
||
| 162 | |||
| 163 | // Call parser transform hook |
||
| 164 | /** @var MediaHandler $handler */ |
||
| 165 | $handler = $img->getHandler(); |
||
| 166 | if ( $this->mParser && $handler ) { |
||
| 167 | $handler->parserTransformHook( $this->mParser, $img ); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | // @todo Code is incomplete. |
||
| 173 | // $linkTarget = Title::newFromText( $wgContLang->getNsText( MWNamespace::getUser() ) . |
||
| 174 | // ":{$ut}" ); |
||
| 175 | // $ul = Linker::link( $linkTarget, $ut ); |
||
| 176 | |||
| 177 | if ( $this->mShowBytes ) { |
||
| 178 | if ( $img ) { |
||
| 179 | $fileSize = htmlspecialchars( $lang->formatSize( $img->getSize() ) ); |
||
| 180 | } else { |
||
| 181 | $fileSize = $this->msg( 'filemissing' )->escaped(); |
||
| 182 | } |
||
| 183 | $fileSize = "$fileSize<br />\n"; |
||
| 184 | } else { |
||
| 185 | $fileSize = ''; |
||
| 186 | } |
||
| 187 | |||
| 188 | $textlink = $this->mShowFilename ? |
||
| 189 | // Preloaded into LinkCache above |
||
| 190 | Linker::linkKnown( |
||
| 191 | $nt, |
||
| 192 | htmlspecialchars( |
||
| 193 | $this->mCaptionLength !== true ? |
||
| 194 | $lang->truncate( $nt->getText(), $this->mCaptionLength ) : |
||
| 195 | $nt->getText() |
||
| 196 | ), |
||
| 197 | [ |
||
| 198 | 'class' => 'galleryfilename' . |
||
| 199 | ( $this->mCaptionLength === true ? ' galleryfilename-truncate' : '' ) |
||
| 200 | ] |
||
| 201 | ) . "\n" : |
||
| 202 | ''; |
||
| 203 | |||
| 204 | $galleryText = $textlink . $text . $fileSize; |
||
| 205 | $galleryText = $this->wrapGalleryText( $galleryText, $thumb ); |
||
| 206 | |||
| 207 | # Weird double wrapping (the extra div inside the li) needed due to FF2 bug |
||
| 208 | # Can be safely removed if FF2 falls completely out of existence |
||
| 209 | $output .= "\n\t\t" . '<li class="gallerybox" style="width: ' |
||
| 210 | . $this->getGBWidth( $thumb ) . 'px">' |
||
| 211 | . '<div style="width: ' . $this->getGBWidth( $thumb ) . 'px">' |
||
| 212 | . $thumbhtml |
||
| 213 | . $galleryText |
||
| 214 | . "\n\t\t</div></li>"; |
||
| 215 | } |
||
| 216 | $output .= "\n</ul>"; |
||
| 217 | |||
| 218 | return $output; |
||
| 219 | } |
||
| 220 | |||
| 365 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: