| Conditions | 4 |
| Paths | 20 |
| Total Lines | 89 |
| Code Lines | 53 |
| Lines | 10 |
| Ratio | 11.24 % |
| 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 |
||
| 36 | public function run() { |
||
| 37 | $scope = RequestContext::importScopedSession( $this->params['session'] ); |
||
| 38 | $this->addTeardownCallback( function () use ( &$scope ) { |
||
| 39 | ScopedCallback::consume( $scope ); // T126450 |
||
| 40 | } ); |
||
| 41 | |||
| 42 | $context = RequestContext::getMain(); |
||
| 43 | $user = $context->getUser(); |
||
| 44 | try { |
||
| 45 | if ( !$user->isLoggedIn() ) { |
||
| 46 | $this->setLastError( "Could not load the author user from session." ); |
||
| 47 | |||
| 48 | return false; |
||
| 49 | } |
||
| 50 | |||
| 51 | UploadBase::setSessionStatus( |
||
| 52 | $user, |
||
| 53 | $this->params['filekey'], |
||
| 54 | [ 'result' => 'Poll', 'stage' => 'assembling', 'status' => Status::newGood() ] |
||
| 55 | ); |
||
| 56 | |||
| 57 | $upload = new UploadFromChunks( $user ); |
||
| 58 | $upload->continueChunks( |
||
| 59 | $this->params['filename'], |
||
| 60 | $this->params['filekey'], |
||
| 61 | new WebRequestUpload( $context->getRequest(), 'null' ) |
||
| 62 | ); |
||
| 63 | |||
| 64 | // Combine all of the chunks into a local file and upload that to a new stash file |
||
| 65 | $status = $upload->concatenateChunks(); |
||
| 66 | View Code Duplication | if ( !$status->isGood() ) { |
|
| 67 | UploadBase::setSessionStatus( |
||
| 68 | $user, |
||
| 69 | $this->params['filekey'], |
||
| 70 | [ 'result' => 'Failure', 'stage' => 'assembling', 'status' => $status ] |
||
| 71 | ); |
||
| 72 | $this->setLastError( $status->getWikiText( false, false, 'en' ) ); |
||
| 73 | |||
| 74 | return false; |
||
| 75 | } |
||
| 76 | |||
| 77 | // We can only get warnings like 'duplicate' after concatenating the chunks |
||
| 78 | $status = Status::newGood(); |
||
| 79 | $status->value = [ 'warnings' => $upload->checkWarnings() ]; |
||
| 80 | |||
| 81 | // We have a new filekey for the fully concatenated file |
||
| 82 | $newFileKey = $upload->getStashFile()->getFileKey(); |
||
| 83 | |||
| 84 | // Remove the old stash file row and first chunk file |
||
| 85 | $upload->stash->removeFileNoAuth( $this->params['filekey'] ); |
||
| 86 | |||
| 87 | // Build the image info array while we have the local reference handy |
||
| 88 | $apiMain = new ApiMain(); // dummy object (XXX) |
||
| 89 | $imageInfo = $upload->getImageInfo( $apiMain->getResult() ); |
||
| 90 | |||
| 91 | // Cleanup any temporary local file |
||
| 92 | $upload->cleanupTempFile(); |
||
| 93 | |||
| 94 | // Cache the info so the user doesn't have to wait forever to get the final info |
||
| 95 | UploadBase::setSessionStatus( |
||
| 96 | $user, |
||
| 97 | $this->params['filekey'], |
||
| 98 | [ |
||
| 99 | 'result' => 'Success', |
||
| 100 | 'stage' => 'assembling', |
||
| 101 | 'filekey' => $newFileKey, |
||
| 102 | 'imageinfo' => $imageInfo, |
||
| 103 | 'status' => $status |
||
| 104 | ] |
||
| 105 | ); |
||
| 106 | } catch ( Exception $e ) { |
||
| 107 | UploadBase::setSessionStatus( |
||
| 108 | $user, |
||
| 109 | $this->params['filekey'], |
||
| 110 | [ |
||
| 111 | 'result' => 'Failure', |
||
| 112 | 'stage' => 'assembling', |
||
| 113 | 'status' => Status::newFatal( 'api-error-stashfailed' ) |
||
| 114 | ] |
||
| 115 | ); |
||
| 116 | $this->setLastError( get_class( $e ) . ": " . $e->getMessage() ); |
||
| 117 | // To be extra robust. |
||
| 118 | MWExceptionHandler::rollbackMasterChangesAndLog( $e ); |
||
| 119 | |||
| 120 | return false; |
||
| 121 | } |
||
| 122 | |||
| 123 | return true; |
||
| 124 | } |
||
| 125 | |||
| 139 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: