| Conditions | 9 |
| Paths | 24 |
| Total Lines | 72 |
| Code Lines | 43 |
| 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 | protected function doGetLocksOnServer( $lockSrv, array $paths, $type ) { |
||
| 46 | $status = StatusValue::newGood(); |
||
| 47 | |||
| 48 | $db = $this->getConnection( $lockSrv ); // checked in isServerUp() |
||
| 49 | |||
| 50 | $keys = []; // list of hash keys for the paths |
||
| 51 | $data = []; // list of rows to insert |
||
| 52 | $checkEXKeys = []; // list of hash keys that this has no EX lock on |
||
| 53 | # Build up values for INSERT clause |
||
| 54 | foreach ( $paths as $path ) { |
||
| 55 | $key = $this->sha1Base36Absolute( $path ); |
||
| 56 | $keys[] = $key; |
||
| 57 | $data[] = [ 'fls_key' => $key, 'fls_session' => $this->session ]; |
||
| 58 | if ( !isset( $this->locksHeld[$path][self::LOCK_EX] ) ) { |
||
| 59 | $checkEXKeys[] = $key; // this has no EX lock on $key itself |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | # Block new writers (both EX and SH locks leave entries here)... |
||
| 64 | $db->insert( 'filelocks_shared', $data, __METHOD__, [ 'IGNORE' ] ); |
||
| 65 | # Actually do the locking queries... |
||
| 66 | if ( $type == self::LOCK_SH ) { // reader locks |
||
| 67 | # Bail if there are any existing writers... |
||
| 68 | if ( count( $checkEXKeys ) ) { |
||
| 69 | $blocked = $db->selectField( |
||
| 70 | 'filelocks_exclusive', |
||
| 71 | '1', |
||
| 72 | [ 'fle_key' => $checkEXKeys ], |
||
| 73 | __METHOD__ |
||
| 74 | ); |
||
| 75 | } else { |
||
| 76 | $blocked = false; |
||
| 77 | } |
||
| 78 | # Other prospective writers that haven't yet updated filelocks_exclusive |
||
| 79 | # will recheck filelocks_shared after doing so and bail due to this entry. |
||
| 80 | } else { // writer locks |
||
| 81 | $encSession = $db->addQuotes( $this->session ); |
||
| 82 | # Bail if there are any existing writers... |
||
| 83 | # This may detect readers, but the safe check for them is below. |
||
| 84 | # Note: if two writers come at the same time, both bail :) |
||
| 85 | $blocked = $db->selectField( |
||
| 86 | 'filelocks_shared', |
||
| 87 | '1', |
||
| 88 | [ 'fls_key' => $keys, "fls_session != $encSession" ], |
||
| 89 | __METHOD__ |
||
| 90 | ); |
||
| 91 | if ( !$blocked ) { |
||
| 92 | # Build up values for INSERT clause |
||
| 93 | $data = []; |
||
| 94 | foreach ( $keys as $key ) { |
||
| 95 | $data[] = [ 'fle_key' => $key ]; |
||
| 96 | } |
||
| 97 | # Block new readers/writers... |
||
| 98 | $db->insert( 'filelocks_exclusive', $data, __METHOD__ ); |
||
| 99 | # Bail if there are any existing readers... |
||
| 100 | $blocked = $db->selectField( |
||
| 101 | 'filelocks_shared', |
||
| 102 | '1', |
||
| 103 | [ 'fls_key' => $keys, "fls_session != $encSession" ], |
||
| 104 | __METHOD__ |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | if ( $blocked ) { |
||
| 110 | foreach ( $paths as $path ) { |
||
| 111 | $status->fatal( 'lockmanager-fail-acquirelock', $path ); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return $status; |
||
| 116 | } |
||
| 117 | |||
| 138 |