Conditions | 17 |
Paths | 54 |
Total Lines | 138 |
Code Lines | 55 |
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 |
||
112 | public function getItem($key) |
||
113 | { |
||
114 | if (\is_string($key)) { |
||
|
|||
115 | $item = null; |
||
116 | |||
117 | /** |
||
118 | * Replace array_key_exists by isset |
||
119 | * due to performance issue on huge |
||
120 | * loop dispatching operations |
||
121 | */ |
||
122 | if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) { |
||
123 | if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) { |
||
124 | throw new PhpfastcacheInvalidArgumentException( |
||
125 | 'Unsupported key character detected: "' . $matches[1] . '". Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers' |
||
126 | ); |
||
127 | } |
||
128 | |||
129 | $cacheSlamsSpendSeconds = 0; |
||
130 | $class = $this->getClassNamespace() . '\Item'; |
||
131 | /** @var $item ExtendedCacheItemInterface */ |
||
132 | $item = new $class($this, $key); |
||
133 | $item->setEventManager($this->eventManager); |
||
134 | |||
135 | getItemDriverRead: |
||
136 | { |
||
137 | $driverArray = $this->driverRead($item); |
||
138 | |||
139 | if ($driverArray) { |
||
140 | if (!\is_array($driverArray)) { |
||
141 | throw new PhpfastcacheCoreException( |
||
142 | sprintf( |
||
143 | 'The driverRead method returned an unexpected variable type: %s', |
||
144 | \gettype($driverArray) |
||
145 | ) |
||
146 | ); |
||
147 | } |
||
148 | $driverData = $this->driverUnwrapData($driverArray); |
||
149 | |||
150 | if ($this->getConfig()['preventCacheSlams']) { |
||
151 | while ($driverData instanceof ItemBatch) { |
||
152 | if ($driverData->getItemDate()->getTimestamp() + $this->getConfig()->getCacheSlamsTimeout() < \time()) { |
||
153 | /** |
||
154 | * The timeout has been reached |
||
155 | * Consider that the batch has |
||
156 | * failed and serve an empty item |
||
157 | * to avoid to get stuck with a |
||
158 | * batch item stored in driver |
||
159 | */ |
||
160 | goto getItemDriverExpired; |
||
161 | } |
||
162 | /** |
||
163 | * @eventName CacheGetItem |
||
164 | * @param $this ExtendedCacheItemPoolInterface |
||
165 | * @param $driverData ItemBatch |
||
166 | * @param $cacheSlamsSpendSeconds int |
||
167 | */ |
||
168 | $this->eventManager->dispatch('CacheGetItemInSlamBatch', $this, $driverData, $cacheSlamsSpendSeconds); |
||
169 | |||
170 | /** |
||
171 | * Wait for a second before |
||
172 | * attempting to get exit |
||
173 | * the current batch process |
||
174 | */ |
||
175 | \sleep(1); |
||
176 | $cacheSlamsSpendSeconds++; |
||
177 | goto getItemDriverRead; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | $item->set($driverData); |
||
182 | $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
||
183 | |||
184 | if ($this->getConfig()->isItemDetailedDate()) { |
||
185 | /** |
||
186 | * If the itemDetailedDate has been |
||
187 | * set after caching, we MUST inject |
||
188 | * a new DateTime object on the fly |
||
189 | */ |
||
190 | $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
||
191 | $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
||
192 | } |
||
193 | |||
194 | $item->setTags($this->driverUnwrapTags($driverArray)); |
||
195 | |||
196 | getItemDriverExpired: |
||
197 | if ($item->isExpired()) { |
||
198 | /** |
||
199 | * Using driverDelete() instead of delete() |
||
200 | * to avoid infinite loop caused by |
||
201 | * getItem() call in delete() method |
||
202 | * As we MUST return an item in any |
||
203 | * way, we do not de-register here |
||
204 | */ |
||
205 | $this->driverDelete($item); |
||
206 | |||
207 | /** |
||
208 | * Reset the Item |
||
209 | */ |
||
210 | $item->set(null) |
||
211 | ->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])) |
||
212 | ->setHit(false) |
||
213 | ->setTags([]); |
||
214 | if ($this->getConfig()->isItemDetailedDate()) { |
||
215 | /** |
||
216 | * If the itemDetailedDate has been |
||
217 | * set after caching, we MUST inject |
||
218 | * a new DateTime object on the fly |
||
219 | */ |
||
220 | $item->setCreationDate(new DateTime()); |
||
221 | $item->setModificationDate(new DateTime()); |
||
222 | } |
||
223 | } else { |
||
224 | $item->setHit(true); |
||
225 | } |
||
226 | } else { |
||
227 | $item->expiresAfter(abs((int)$this->getConfig()['defaultTtl'])); |
||
228 | } |
||
229 | } |
||
230 | }else{ |
||
231 | $item = $this->itemInstances[$key]; |
||
232 | } |
||
233 | |||
234 | |||
235 | if($item !== null){ |
||
236 | /** |
||
237 | * @eventName CacheGetItem |
||
238 | * @param $this ExtendedCacheItemPoolInterface |
||
239 | * @param $this ExtendedCacheItemInterface |
||
240 | */ |
||
241 | $this->eventManager->dispatch('CacheGetItem', $this, $item); |
||
242 | |||
243 | $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
||
244 | |||
245 | return $item; |
||
246 | } |
||
247 | throw new PhpfastcacheInvalidArgumentException(\sprintf('Item %s was not build due to an unknown error', \gettype($key))); |
||
248 | } |
||
249 | throw new PhpfastcacheInvalidArgumentException(\sprintf('$key must be a string, got type "%s" instead.', \gettype($key))); |
||
250 | } |
||
452 |