| Conditions | 22 |
| Paths | 461 |
| Total Lines | 107 |
| Code Lines | 69 |
| 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 |
||
| 69 | public function stream( |
||
| 70 | $headers = [], $sendErrors = true, $optHeaders = [], $flags = 0 |
||
| 71 | ) { |
||
| 72 | // Don't stream it out as text/html if there was a PHP error |
||
| 73 | if ( ( ( $flags & self::STREAM_HEADLESS ) == 0 || $headers ) && headers_sent() ) { |
||
| 74 | echo "Headers already sent, terminating.\n"; |
||
| 75 | return false; |
||
| 76 | } |
||
| 77 | |||
| 78 | $headerFunc = ( $flags & self::STREAM_HEADLESS ) |
||
| 79 | ? function ( $header ) { |
||
| 80 | // no-op |
||
| 81 | } |
||
| 82 | : function ( $header ) { |
||
| 83 | is_int( $header ) ? HttpStatus::header( $header ) : header( $header ); |
||
| 84 | }; |
||
| 85 | |||
| 86 | MediaWiki\suppressWarnings(); |
||
| 87 | $info = stat( $this->path ); |
||
| 88 | MediaWiki\restoreWarnings(); |
||
| 89 | |||
| 90 | if ( !is_array( $info ) ) { |
||
| 91 | if ( $sendErrors ) { |
||
| 92 | self::send404Message( $this->path, $flags ); |
||
| 93 | } |
||
| 94 | return false; |
||
| 95 | } |
||
| 96 | |||
| 97 | // Send Last-Modified HTTP header for client-side caching |
||
| 98 | $mtimeCT = new ConvertibleTimestamp( $info['mtime'] ); |
||
| 99 | $headerFunc( 'Last-Modified: ' . $mtimeCT->getTimestamp( TS_RFC2822 ) ); |
||
| 100 | |||
| 101 | if ( ( $flags & self::STREAM_ALLOW_OB ) == 0 ) { |
||
| 102 | call_user_func( $this->obResetFunc ); |
||
| 103 | } |
||
| 104 | |||
| 105 | $type = call_user_func( $this->streamMimeFunc, $this->path ); |
||
| 106 | if ( $type && $type != 'unknown/unknown' ) { |
||
| 107 | $headerFunc( "Content-type: $type" ); |
||
| 108 | } else { |
||
| 109 | // Send a content type which is not known to Internet Explorer, to |
||
| 110 | // avoid triggering IE's content type detection. Sending a standard |
||
| 111 | // unknown content type here essentially gives IE license to apply |
||
| 112 | // whatever content type it likes. |
||
| 113 | $headerFunc( 'Content-type: application/x-wiki' ); |
||
| 114 | } |
||
| 115 | |||
| 116 | // Don't send if client has up to date cache |
||
| 117 | if ( isset( $optHeaders['if-modified-since'] ) ) { |
||
| 118 | $modsince = preg_replace( '/;.*$/', '', $optHeaders['if-modified-since'] ); |
||
| 119 | if ( $mtimeCT->getTimestamp( TS_UNIX ) <= strtotime( $modsince ) ) { |
||
| 120 | ini_set( 'zlib.output_compression', 0 ); |
||
| 121 | $headerFunc( 304 ); |
||
| 122 | return true; // ok |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | // Send additional headers |
||
| 127 | foreach ( $headers as $header ) { |
||
| 128 | header( $header ); // always use header(); specifically requested |
||
| 129 | } |
||
| 130 | |||
| 131 | if ( isset( $optHeaders['range'] ) ) { |
||
| 132 | $range = self::parseRange( $optHeaders['range'], $info['size'] ); |
||
| 133 | if ( is_array( $range ) ) { |
||
| 134 | $headerFunc( 206 ); |
||
| 135 | $headerFunc( 'Content-Length: ' . $range[2] ); |
||
| 136 | $headerFunc( "Content-Range: bytes {$range[0]}-{$range[1]}/{$info['size']}" ); |
||
| 137 | } elseif ( $range === 'invalid' ) { |
||
| 138 | if ( $sendErrors ) { |
||
| 139 | $headerFunc( 416 ); |
||
| 140 | $headerFunc( 'Cache-Control: no-cache' ); |
||
| 141 | $headerFunc( 'Content-Type: text/html; charset=utf-8' ); |
||
| 142 | $headerFunc( 'Content-Range: bytes */' . $info['size'] ); |
||
| 143 | } |
||
| 144 | return false; |
||
| 145 | } else { // unsupported Range request (e.g. multiple ranges) |
||
| 146 | $range = null; |
||
| 147 | $headerFunc( 'Content-Length: ' . $info['size'] ); |
||
| 148 | } |
||
| 149 | } else { |
||
| 150 | $range = null; |
||
| 151 | $headerFunc( 'Content-Length: ' . $info['size'] ); |
||
| 152 | } |
||
| 153 | |||
| 154 | if ( is_array( $range ) ) { |
||
| 155 | $handle = fopen( $this->path, 'rb' ); |
||
| 156 | if ( $handle ) { |
||
| 157 | $ok = true; |
||
| 158 | fseek( $handle, $range[0] ); |
||
| 159 | $remaining = $range[2]; |
||
| 160 | while ( $remaining > 0 && $ok ) { |
||
| 161 | $bytes = min( $remaining, 8 * 1024 ); |
||
| 162 | $data = fread( $handle, $bytes ); |
||
| 163 | $remaining -= $bytes; |
||
| 164 | $ok = ( $data !== false ); |
||
| 165 | print $data; |
||
| 166 | } |
||
| 167 | } else { |
||
| 168 | return false; |
||
| 169 | } |
||
| 170 | } else { |
||
| 171 | return readfile( $this->path ) !== false; // faster |
||
| 172 | } |
||
| 173 | |||
| 174 | return true; |
||
| 175 | } |
||
| 176 | |||
| 269 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: