| Conditions | 13 |
| Paths | 22 |
| Total Lines | 56 |
| Code Lines | 34 |
| 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 |
||
| 104 | public function execute( $skipcache = false ) { |
||
| 105 | if ( $this->cacheable && !$skipcache ) { |
||
| 106 | $status = $this->poolCounter->acquireForAnyone(); |
||
| 107 | } else { |
||
| 108 | $status = $this->poolCounter->acquireForMe(); |
||
| 109 | } |
||
| 110 | |||
| 111 | if ( !$status->isOK() ) { |
||
| 112 | // Respond gracefully to complete server breakage: just log it and do the work |
||
| 113 | $this->logError( $status ); |
||
| 114 | return $this->doWork(); |
||
| 115 | } |
||
| 116 | |||
| 117 | switch ( $status->value ) { |
||
| 118 | case PoolCounter::LOCK_HELD: |
||
| 119 | // Better to ignore nesting pool counter limits than to fail. |
||
| 120 | // Assume that the outer pool limiting is reasonable enough. |
||
| 121 | /* no break */ |
||
| 122 | case PoolCounter::LOCKED: |
||
| 123 | $result = $this->doWork(); |
||
| 124 | $this->poolCounter->release(); |
||
| 125 | return $result; |
||
| 126 | |||
| 127 | case PoolCounter::DONE: |
||
| 128 | $result = $this->getCachedWork(); |
||
| 129 | if ( $result === false ) { |
||
| 130 | /* That someone else work didn't serve us. |
||
| 131 | * Acquire the lock for me |
||
| 132 | */ |
||
| 133 | return $this->execute( true ); |
||
| 134 | } |
||
| 135 | return $result; |
||
| 136 | |||
| 137 | case PoolCounter::QUEUE_FULL: |
||
| 138 | case PoolCounter::TIMEOUT: |
||
| 139 | $result = $this->fallback(); |
||
| 140 | |||
| 141 | if ( $result !== false ) { |
||
| 142 | return $result; |
||
| 143 | } |
||
| 144 | /* no break */ |
||
| 145 | |||
| 146 | /* These two cases should never be hit... */ |
||
| 147 | case PoolCounter::ERROR: |
||
| 148 | default: |
||
| 149 | $errors = [ |
||
| 150 | PoolCounter::QUEUE_FULL => 'pool-queuefull', |
||
| 151 | PoolCounter::TIMEOUT => 'pool-timeout' ]; |
||
| 152 | |||
| 153 | $status = Status::newFatal( isset( $errors[$status->value] ) |
||
| 154 | ? $errors[$status->value] |
||
| 155 | : 'pool-errorunknown' ); |
||
| 156 | $this->logError( $status ); |
||
| 157 | return $this->error( $status ); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: