| Conditions | 20 |
| Paths | 248 |
| Total Lines | 112 |
| Code Lines | 68 |
| Lines | 10 |
| Ratio | 8.93 % |
| 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 |
||
| 83 | protected function getServerStates( array $serverIndexes, $domain ) { |
||
| 84 | $writerIndex = $this->parent->getWriterIndex(); |
||
| 85 | if ( count( $serverIndexes ) == 1 && reset( $serverIndexes ) == $writerIndex ) { |
||
| 86 | # Single server only, just return zero without caching |
||
| 87 | return [ |
||
| 88 | 'lagTimes' => [ $writerIndex => 0 ], |
||
| 89 | 'weightScales' => [ $writerIndex => 1.0 ] |
||
| 90 | ]; |
||
| 91 | } |
||
| 92 | |||
| 93 | $key = $this->getCacheKey( $serverIndexes ); |
||
| 94 | # Randomize TTLs to reduce stampedes (4.0 - 5.0 sec) |
||
| 95 | $ttl = mt_rand( 4e6, 5e6 ) / 1e6; |
||
| 96 | # Keep keys around longer as fallbacks |
||
| 97 | $staleTTL = 60; |
||
| 98 | |||
| 99 | # (a) Check the local APC cache |
||
| 100 | $value = $this->srvCache->get( $key ); |
||
| 101 | if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) { |
||
| 102 | $this->replLogger->debug( __METHOD__ . ": got lag times ($key) from local cache" ); |
||
| 103 | return $value; // cache hit |
||
| 104 | } |
||
| 105 | $staleValue = $value ?: false; |
||
| 106 | |||
| 107 | # (b) Check the shared cache and backfill APC |
||
| 108 | $value = $this->mainCache->get( $key ); |
||
| 109 | if ( $value && $value['timestamp'] > ( microtime( true ) - $ttl ) ) { |
||
| 110 | $this->srvCache->set( $key, $value, $staleTTL ); |
||
| 111 | $this->replLogger->debug( __METHOD__ . ": got lag times ($key) from main cache" ); |
||
| 112 | |||
| 113 | return $value; // cache hit |
||
| 114 | } |
||
| 115 | $staleValue = $value ?: $staleValue; |
||
| 116 | |||
| 117 | # (c) Cache key missing or expired; regenerate and backfill |
||
| 118 | if ( $this->mainCache->lock( $key, 0, 10 ) ) { |
||
| 119 | # Let this process alone update the cache value |
||
| 120 | $cache = $this->mainCache; |
||
| 121 | /** @noinspection PhpUnusedLocalVariableInspection */ |
||
| 122 | $unlocker = new ScopedCallback( function () use ( $cache, $key ) { |
||
| 123 | $cache->unlock( $key ); |
||
| 124 | } ); |
||
| 125 | } elseif ( $staleValue ) { |
||
| 126 | # Could not acquire lock but an old cache exists, so use it |
||
| 127 | return $staleValue; |
||
| 128 | } |
||
| 129 | |||
| 130 | $lagTimes = []; |
||
| 131 | $weightScales = []; |
||
| 132 | $movAveRatio = $this->movingAveRatio; |
||
| 133 | foreach ( $serverIndexes as $i ) { |
||
| 134 | if ( $i == $this->parent->getWriterIndex() ) { |
||
| 135 | $lagTimes[$i] = 0; // master always has no lag |
||
| 136 | $weightScales[$i] = 1.0; // nominal weight |
||
| 137 | continue; |
||
| 138 | } |
||
| 139 | |||
| 140 | $conn = $this->parent->getAnyOpenConnection( $i ); |
||
| 141 | if ( $conn ) { |
||
| 142 | $close = false; // already open |
||
| 143 | } else { |
||
| 144 | $conn = $this->parent->openConnection( $i, $domain ); |
||
| 145 | $close = true; // new connection |
||
| 146 | } |
||
| 147 | |||
| 148 | $lastWeight = isset( $staleValue['weightScales'][$i] ) |
||
| 149 | ? $staleValue['weightScales'][$i] |
||
| 150 | : 1.0; |
||
| 151 | $coefficient = $this->getWeightScale( $i, $conn ?: null ); |
||
| 152 | $newWeight = $movAveRatio * $coefficient + ( 1 - $movAveRatio ) * $lastWeight; |
||
| 153 | |||
| 154 | // Scale from 10% to 100% of nominal weight |
||
| 155 | $weightScales[$i] = max( $newWeight, .10 ); |
||
| 156 | |||
| 157 | View Code Duplication | if ( !$conn ) { |
|
| 158 | $lagTimes[$i] = false; |
||
| 159 | $host = $this->parent->getServerName( $i ); |
||
| 160 | $this->replLogger->error( __METHOD__ . ": host $host is unreachable" ); |
||
| 161 | continue; |
||
| 162 | } |
||
| 163 | |||
| 164 | if ( $conn->getLBInfo( 'is static' ) ) { |
||
| 165 | $lagTimes[$i] = 0; |
||
| 166 | } else { |
||
| 167 | $lagTimes[$i] = $conn->getLag(); |
||
| 168 | View Code Duplication | if ( $lagTimes[$i] === false ) { |
|
| 169 | $host = $this->parent->getServerName( $i ); |
||
| 170 | $this->replLogger->error( __METHOD__ . ": host $host is not replicating?" ); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | if ( $close ) { |
||
| 175 | # Close the connection to avoid sleeper connections piling up. |
||
| 176 | # Note that the caller will pick one of these DBs and reconnect, |
||
| 177 | # which is slightly inefficient, but this only matters for the lag |
||
| 178 | # time cache miss cache, which is far less common that cache hits. |
||
| 179 | $this->parent->closeConnection( $conn ); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | # Add a timestamp key so we know when it was cached |
||
| 184 | $value = [ |
||
| 185 | 'lagTimes' => $lagTimes, |
||
| 186 | 'weightScales' => $weightScales, |
||
| 187 | 'timestamp' => microtime( true ) |
||
| 188 | ]; |
||
| 189 | $this->mainCache->set( $key, $value, $staleTTL ); |
||
| 190 | $this->srvCache->set( $key, $value, $staleTTL ); |
||
| 191 | $this->replLogger->info( __METHOD__ . ": re-calculated lag times ($key)" ); |
||
| 192 | |||
| 193 | return $value; |
||
| 194 | } |
||
| 195 | |||
| 216 |
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: