Reduce nesting by turning big if-else into guard clauses

Change-Id: If3d2f18af0cc7d0f8fe9f764bd4d0e4bae70e440
This commit is contained in:
Thiemo Kreuz 2019-11-08 17:19:13 +01:00
parent b777cc6b46
commit 9c6473dc1d
3 changed files with 35 additions and 39 deletions

View file

@ -386,22 +386,22 @@ class JobQueueDB extends JobQueue {
}
}
if ( $row ) { // claim the job
$dbw->update( 'job', // update by PK
[
'job_token' => $uuid,
'job_token_timestamp' => $dbw->timestamp(),
'job_attempts = job_attempts+1' ],
[ 'job_cmd' => $this->type, 'job_id' => $row->job_id, 'job_token' => '' ],
__METHOD__
);
// This might get raced out by another runner when claiming the previously
// selected row. The use of job_random should minimize this problem, however.
if ( !$dbw->affectedRows() ) {
$row = false; // raced out
}
} else {
break; // nothing to do
if ( !$row ) {
break;
}
$dbw->update( 'job', // update by PK
[
'job_token' => $uuid,
'job_token_timestamp' => $dbw->timestamp(),
'job_attempts = job_attempts+1' ],
[ 'job_cmd' => $this->type, 'job_id' => $row->job_id, 'job_token' => '' ],
__METHOD__
);
// This might get raced out by another runner when claiming the previously
// selected row. The use of job_random should minimize this problem, however.
if ( !$dbw->affectedRows() ) {
$row = false; // raced out
}
} while ( !$row );
@ -455,16 +455,17 @@ class JobQueueDB extends JobQueue {
__METHOD__
);
}
if ( !$dbw->affectedRows() ) {
break;
}
// Fetch any row that we just reserved...
if ( $dbw->affectedRows() ) {
$row = $dbw->selectRow( 'job', self::selectFields(),
[ 'job_cmd' => $this->type, 'job_token' => $uuid ], __METHOD__
);
if ( !$row ) { // raced out by duplicate job removal
wfDebug( "Row deleted as duplicate by another process." );
}
} else {
break; // nothing to do
$row = $dbw->selectRow( 'job', self::selectFields(),
[ 'job_cmd' => $this->type, 'job_token' => $uuid ], __METHOD__
);
if ( !$row ) { // raced out by duplicate job removal
wfDebug( "Row deleted as duplicate by another process." );
}
} while ( !$row );

View file

@ -207,19 +207,14 @@ class SwiftFileBackend extends FileBackendStore {
$contentHeaders[$name] = $value;
}
// By default, Swift has annoyingly low maximum header value limits
if ( isset( $contentHeaders['content-disposition'] ) ) {
$disposition = '';
// @note: assume FileBackend::makeContentDisposition() already used
foreach ( explode( ';', $contentHeaders['content-disposition'] ) as $part ) {
$part = trim( $part );
$new = ( $disposition === '' ) ? $part : "{$disposition};{$part}";
if ( strlen( $new ) <= 255 ) {
$disposition = $new;
} else {
break; // too long; sigh
}
}
$contentHeaders['content-disposition'] = $disposition;
$maxLength = 255;
// @note: assume FileBackend::makeContentDisposition() already used
$offset = $maxLength - strlen( $contentHeaders['content-disposition'] );
if ( $offset < 0 ) {
$pos = strrpos( $contentHeaders['content-disposition'], ';', $offset );
$contentHeaders['content-disposition'] = $pos === false
? ''
: trim( substr( $contentHeaders['content-disposition'], 0, $pos ) );
}
return $contentHeaders;

View file

@ -73,7 +73,7 @@ class SwiftFileBackendTest extends MediaWikiIntegrationTestCase {
],
[
'content-type' => 'image+bitmap/jpeg',
'content-disposition' => 'inline;filename=xxx',
'content-disposition' => 'inline; filename=xxx',
'content-duration' => 35.6363,
'content-custom' => 'hello',
'x-content-custom' => 'hello'