Conditions | 14 |
Paths | 20 |
Total Lines | 125 |
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 |
||
67 | public function getItem($key) |
||
68 | { |
||
69 | if (\is_string($key)) { |
||
|
|||
70 | /** |
||
71 | * Replace array_key_exists by isset |
||
72 | * due to performance issue on huge |
||
73 | * loop dispatching operations |
||
74 | */ |
||
75 | if (!isset($this->itemInstances[$key])) { |
||
76 | if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) { |
||
77 | throw new PhpfastcacheInvalidArgumentException('Unsupported key character detected: "' . $matches[1] . '". Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers'); |
||
78 | } |
||
79 | |||
80 | CacheManager::$ReadHits++; |
||
81 | $cacheSlamsSpendSeconds = 0; |
||
82 | $class = $this->getClassNamespace() . '\Item'; |
||
83 | /** @var $item ExtendedCacheItemInterface */ |
||
84 | $item = new $class($this, $key); |
||
85 | $item->setEventManager($this->eventManager); |
||
86 | |||
87 | getItemDriverRead: |
||
88 | { |
||
89 | $driverArray = $this->driverRead($item); |
||
90 | |||
91 | if ($driverArray) { |
||
92 | if (!\is_array($driverArray)) { |
||
93 | throw new PhpfastcacheCoreException(\sprintf('The driverRead method returned an unexpected variable type: %s', |
||
94 | \gettype($driverArray))); |
||
95 | } |
||
96 | $driverData = $this->driverUnwrapData($driverArray); |
||
97 | |||
98 | if ($this->getConfig()['preventCacheSlams']) { |
||
99 | while ($driverData instanceof ItemBatch) { |
||
100 | if ($driverData->getItemDate()->getTimestamp() + $this->getConfig()->getCacheSlamsTimeout() < \time()) { |
||
101 | /** |
||
102 | * The timeout has been reached |
||
103 | * Consider that the batch has |
||
104 | * failed and serve an empty item |
||
105 | * to avoid to get stuck with a |
||
106 | * batch item stored in driver |
||
107 | */ |
||
108 | goto getItemDriverExpired; |
||
109 | } |
||
110 | /** |
||
111 | * @eventName CacheGetItem |
||
112 | * @param $this ExtendedCacheItemPoolInterface |
||
113 | * @param $driverData ItemBatch |
||
114 | * @param $cacheSlamsSpendSeconds int |
||
115 | */ |
||
116 | $this->eventManager->dispatch('CacheGetItemInSlamBatch', $this, $driverData, $cacheSlamsSpendSeconds); |
||
117 | |||
118 | /** |
||
119 | * Wait for a second before |
||
120 | * attempting to get exit |
||
121 | * the current batch process |
||
122 | */ |
||
123 | \sleep(1); |
||
124 | $cacheSlamsSpendSeconds++; |
||
125 | goto getItemDriverRead; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | $item->set($driverData); |
||
130 | $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
||
131 | |||
132 | if ($this->getConfig()->isItemDetailedDate()) { |
||
133 | /** |
||
134 | * If the itemDetailedDate has been |
||
135 | * set after caching, we MUST inject |
||
136 | * a new DateTime object on the fly |
||
137 | */ |
||
138 | $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new \DateTime()); |
||
139 | $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new \DateTime()); |
||
140 | } |
||
141 | |||
142 | $item->setTags($this->driverUnwrapTags($driverArray)); |
||
143 | |||
144 | getItemDriverExpired: |
||
145 | if ($item->isExpired()) { |
||
146 | /** |
||
147 | * Using driverDelete() instead of delete() |
||
148 | * to avoid infinite loop caused by |
||
149 | * getItem() call in delete() method |
||
150 | * As we MUST return an item in any |
||
151 | * way, we do not de-register here |
||
152 | */ |
||
153 | $this->driverDelete($item); |
||
154 | |||
155 | /** |
||
156 | * Reset the Item |
||
157 | */ |
||
158 | $item->set(null) |
||
159 | ->expiresAfter(\abs((int)$this->getConfig()['defaultTtl'])) |
||
160 | ->setHit(false) |
||
161 | ->setTags([]); |
||
162 | if ($this->getConfig()->isItemDetailedDate()) { |
||
163 | |||
164 | /** |
||
165 | * If the itemDetailedDate has been |
||
166 | * set after caching, we MUST inject |
||
167 | * a new DateTime object on the fly |
||
168 | */ |
||
169 | $item->setCreationDate(new \DateTime()); |
||
170 | $item->setModificationDate(new \DateTime()); |
||
171 | } |
||
172 | } else { |
||
173 | $item->setHit(true); |
||
174 | } |
||
175 | } else { |
||
176 | $item->expiresAfter(\abs((int)$this->getConfig()['defaultTtl'])); |
||
177 | } |
||
178 | } |
||
179 | } |
||
180 | } else { |
||
181 | throw new PhpfastcacheInvalidArgumentException(\sprintf('$key must be a string, got type "%s" instead.', \gettype($key))); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @eventName CacheGetItem |
||
186 | * @param $this ExtendedCacheItemPoolInterface |
||
187 | * @param $this ExtendedCacheItemInterface |
||
188 | */ |
||
189 | $this->eventManager->dispatch('CacheGetItem', $this, $this->itemInstances[$key]); |
||
190 | |||
191 | return $this->itemInstances[$key]; |
||
192 | } |
||
421 | } |