| Conditions | 23 |
| Paths | 462 |
| Total Lines | 113 |
| Code Lines | 71 |
| 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 |
||
| 45 | public static function stream( |
||
| 46 | $fname, $headers = [], $sendErrors = true, $optHeaders = [], $flags = 0 |
||
| 47 | ) { |
||
| 48 | $section = new ProfileSection( __METHOD__ ); |
||
| 49 | |||
| 50 | if ( FileBackend::isStoragePath( $fname ) ) { // sanity |
||
| 51 | throw new MWException( __FUNCTION__ . " given storage path '$fname'." ); |
||
| 52 | } |
||
| 53 | |||
| 54 | // Don't stream it out as text/html if there was a PHP error |
||
| 55 | if ( ( ( $flags & self::STREAM_HEADLESS ) == 0 || $headers ) && headers_sent() ) { |
||
| 56 | echo "Headers already sent, terminating.\n"; |
||
| 57 | return false; |
||
| 58 | } |
||
| 59 | |||
| 60 | $headerFunc = ( $flags & self::STREAM_HEADLESS ) |
||
| 61 | ? function ( $header ) { |
||
| 62 | // no-op |
||
| 63 | } |
||
| 64 | : function ( $header ) { |
||
| 65 | is_int( $header ) ? HttpStatus::header( $header ) : header( $header ); |
||
| 66 | }; |
||
| 67 | |||
| 68 | MediaWiki\suppressWarnings(); |
||
| 69 | $info = stat( $fname ); |
||
| 70 | MediaWiki\restoreWarnings(); |
||
| 71 | |||
| 72 | if ( !is_array( $info ) ) { |
||
| 73 | if ( $sendErrors ) { |
||
| 74 | self::send404Message( $fname, $flags ); |
||
| 75 | } |
||
| 76 | return false; |
||
| 77 | } |
||
| 78 | |||
| 79 | // Send Last-Modified HTTP header for client-side caching |
||
| 80 | $headerFunc( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $info['mtime'] ) ); |
||
| 81 | |||
| 82 | if ( ( $flags & self::STREAM_ALLOW_OB ) == 0 ) { |
||
| 83 | // Cancel output buffering and gzipping if set |
||
| 84 | wfResetOutputBuffers(); |
||
| 85 | } |
||
| 86 | |||
| 87 | $type = self::contentTypeFromPath( $fname ); |
||
| 88 | if ( $type && $type != 'unknown/unknown' ) { |
||
| 89 | $headerFunc( "Content-type: $type" ); |
||
| 90 | } else { |
||
| 91 | // Send a content type which is not known to Internet Explorer, to |
||
| 92 | // avoid triggering IE's content type detection. Sending a standard |
||
| 93 | // unknown content type here essentially gives IE license to apply |
||
| 94 | // whatever content type it likes. |
||
| 95 | $headerFunc( 'Content-type: application/x-wiki' ); |
||
| 96 | } |
||
| 97 | |||
| 98 | // Don't send if client has up to date cache |
||
| 99 | if ( isset( $optHeaders['if-modified-since'] ) ) { |
||
| 100 | $modsince = preg_replace( '/;.*$/', '', $optHeaders['if-modified-since'] ); |
||
| 101 | if ( wfTimestamp( TS_UNIX, $info['mtime'] ) <= strtotime( $modsince ) ) { |
||
| 102 | ini_set( 'zlib.output_compression', 0 ); |
||
| 103 | $headerFunc( 304 ); |
||
| 104 | return true; // ok |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | // Send additional headers |
||
| 109 | foreach ( $headers as $header ) { |
||
| 110 | header( $header ); // always use header(); specifically requested |
||
| 111 | } |
||
| 112 | |||
| 113 | if ( isset( $optHeaders['range'] ) ) { |
||
| 114 | $range = self::parseRange( $optHeaders['range'], $info['size'] ); |
||
| 115 | if ( is_array( $range ) ) { |
||
| 116 | $headerFunc( 206 ); |
||
| 117 | $headerFunc( 'Content-Length: ' . $range[2] ); |
||
| 118 | $headerFunc( "Content-Range: bytes {$range[0]}-{$range[1]}/{$info['size']}" ); |
||
| 119 | } elseif ( $range === 'invalid' ) { |
||
| 120 | if ( $sendErrors ) { |
||
| 121 | $headerFunc( 416 ); |
||
| 122 | $headerFunc( 'Cache-Control: no-cache' ); |
||
| 123 | $headerFunc( 'Content-Type: text/html; charset=utf-8' ); |
||
| 124 | $headerFunc( 'Content-Range: bytes */' . $info['size'] ); |
||
| 125 | } |
||
| 126 | return false; |
||
| 127 | } else { // unsupported Range request (e.g. multiple ranges) |
||
| 128 | $range = null; |
||
| 129 | $headerFunc( 'Content-Length: ' . $info['size'] ); |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | $range = null; |
||
| 133 | $headerFunc( 'Content-Length: ' . $info['size'] ); |
||
| 134 | } |
||
| 135 | |||
| 136 | if ( is_array( $range ) ) { |
||
| 137 | $handle = fopen( $fname, 'rb' ); |
||
| 138 | if ( $handle ) { |
||
| 139 | $ok = true; |
||
| 140 | fseek( $handle, $range[0] ); |
||
| 141 | $remaining = $range[2]; |
||
| 142 | while ( $remaining > 0 && $ok ) { |
||
| 143 | $bytes = min( $remaining, 8 * 1024 ); |
||
| 144 | $data = fread( $handle, $bytes ); |
||
| 145 | $remaining -= $bytes; |
||
| 146 | $ok = ( $data !== false ); |
||
| 147 | print $data; |
||
| 148 | } |
||
| 149 | } else { |
||
| 150 | return false; |
||
| 151 | } |
||
| 152 | } else { |
||
| 153 | return readfile( $fname ) !== false; // faster |
||
| 154 | } |
||
| 155 | |||
| 156 | return true; |
||
| 157 | } |
||
| 158 | |||
| 276 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.