Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BagOStuff often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use BagOStuff, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
47 | abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface { |
||
48 | /** @var array[] Lock tracking */ |
||
49 | protected $locks = []; |
||
50 | /** @var integer ERR_* class constant */ |
||
51 | protected $lastError = self::ERR_NONE; |
||
52 | /** @var string */ |
||
53 | protected $keyspace = 'local'; |
||
54 | /** @var LoggerInterface */ |
||
55 | protected $logger; |
||
56 | /** @var callback|null */ |
||
57 | protected $asyncHandler; |
||
58 | /** @var integer Seconds */ |
||
59 | protected $syncTimeout; |
||
60 | |||
61 | /** @var bool */ |
||
62 | private $debugMode = false; |
||
63 | /** @var array */ |
||
64 | private $duplicateKeyLookups = []; |
||
65 | /** @var bool */ |
||
66 | private $reportDupes = false; |
||
67 | /** @var bool */ |
||
68 | private $dupeTrackScheduled = false; |
||
69 | |||
70 | /** @var callable[] */ |
||
71 | protected $busyCallbacks = []; |
||
72 | |||
73 | /** @var integer[] Map of (ATTR_* class constant => QOS_* class constant) */ |
||
74 | protected $attrMap = []; |
||
75 | |||
76 | /** Possible values for getLastError() */ |
||
77 | const ERR_NONE = 0; // no error |
||
78 | const ERR_NO_RESPONSE = 1; // no response |
||
79 | const ERR_UNREACHABLE = 2; // can't connect |
||
80 | const ERR_UNEXPECTED = 3; // response gave some error |
||
81 | |||
82 | /** Bitfield constants for get()/getMulti() */ |
||
83 | const READ_LATEST = 1; // use latest data for replicated stores |
||
84 | const READ_VERIFIED = 2; // promise that caller can tell when keys are stale |
||
85 | /** Bitfield constants for set()/merge() */ |
||
86 | const WRITE_SYNC = 1; // synchronously write to all locations for replicated stores |
||
87 | const WRITE_CACHE_ONLY = 2; // Only change state of the in-memory cache |
||
88 | |||
89 | /** |
||
90 | * $params include: |
||
91 | * - logger: Psr\Log\LoggerInterface instance |
||
92 | * - keyspace: Default keyspace for $this->makeKey() |
||
93 | * - asyncHandler: Callable to use for scheduling tasks after the web request ends. |
||
94 | * In CLI mode, it should run the task immediately. |
||
95 | * - reportDupes: Whether to emit warning log messages for all keys that were |
||
96 | * requested more than once (requires an asyncHandler). |
||
97 | * - syncTimeout: How long to wait with WRITE_SYNC in seconds. |
||
98 | * @param array $params |
||
99 | */ |
||
100 | public function __construct( array $params = [] ) { |
||
121 | |||
122 | /** |
||
123 | * @param LoggerInterface $logger |
||
124 | * @return null |
||
125 | */ |
||
126 | public function setLogger( LoggerInterface $logger ) { |
||
129 | |||
130 | /** |
||
131 | * @param bool $bool |
||
132 | */ |
||
133 | public function setDebug( $bool ) { |
||
136 | |||
137 | /** |
||
138 | * Get an item with the given key, regenerating and setting it if not found |
||
139 | * |
||
140 | * If the callback returns false, then nothing is stored. |
||
141 | * |
||
142 | * @param string $key |
||
143 | * @param int $ttl Time-to-live (seconds) |
||
144 | * @param callable $callback Callback that derives the new value |
||
145 | * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional] |
||
146 | * @return mixed The cached value if found or the result of $callback otherwise |
||
147 | * @since 1.27 |
||
148 | */ |
||
149 | final public function getWithSetCallback( $key, $ttl, $callback, $flags = 0 ) { |
||
164 | |||
165 | /** |
||
166 | * Get an item with the given key |
||
167 | * |
||
168 | * If the key includes a determistic input hash (e.g. the key can only have |
||
169 | * the correct value) or complete staleness checks are handled by the caller |
||
170 | * (e.g. nothing relies on the TTL), then the READ_VERIFIED flag should be set. |
||
171 | * This lets tiered backends know they can safely upgrade a cached value to |
||
172 | * higher tiers using standard TTLs. |
||
173 | * |
||
174 | * @param string $key |
||
175 | * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional] |
||
176 | * @param integer $oldFlags [unused] |
||
177 | * @return mixed Returns false on failure and if the item does not exist |
||
178 | */ |
||
179 | public function get( $key, $flags = 0, $oldFlags = null ) { |
||
187 | |||
188 | /** |
||
189 | * Track the number of times that a given key has been used. |
||
190 | * @param string $key |
||
191 | */ |
||
192 | private function trackDuplicateKeys( $key ) { |
||
220 | |||
221 | /** |
||
222 | * @param string $key |
||
223 | * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional] |
||
224 | * @return mixed Returns false on failure and if the item does not exist |
||
225 | */ |
||
226 | abstract protected function doGet( $key, $flags = 0 ); |
||
227 | |||
228 | /** |
||
229 | * @note: This method is only needed if merge() uses mergeViaCas() |
||
230 | * |
||
231 | * @param string $key |
||
232 | * @param mixed $casToken |
||
233 | * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional] |
||
234 | * @return mixed Returns false on failure and if the item does not exist |
||
235 | * @throws Exception |
||
236 | */ |
||
237 | protected function getWithToken( $key, &$casToken, $flags = 0 ) { |
||
240 | |||
241 | /** |
||
242 | * Set an item |
||
243 | * |
||
244 | * @param string $key |
||
245 | * @param mixed $value |
||
246 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
247 | * @param int $flags Bitfield of BagOStuff::WRITE_* constants |
||
248 | * @return bool Success |
||
249 | */ |
||
250 | abstract public function set( $key, $value, $exptime = 0, $flags = 0 ); |
||
251 | |||
252 | /** |
||
253 | * Delete an item |
||
254 | * |
||
255 | * @param string $key |
||
256 | * @return bool True if the item was deleted or not found, false on failure |
||
257 | */ |
||
258 | abstract public function delete( $key ); |
||
259 | |||
260 | /** |
||
261 | * Merge changes into the existing cache value (possibly creating a new one) |
||
262 | * |
||
263 | * The callback function returns the new value given the current value |
||
264 | * (which will be false if not present), and takes the arguments: |
||
265 | * (this BagOStuff, cache key, current value, TTL). |
||
266 | * The TTL parameter is reference set to $exptime. It can be overriden in the callback. |
||
267 | * |
||
268 | * @param string $key |
||
269 | * @param callable $callback Callback method to be executed |
||
270 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
271 | * @param int $attempts The amount of times to attempt a merge in case of failure |
||
272 | * @param int $flags Bitfield of BagOStuff::WRITE_* constants |
||
273 | * @return bool Success |
||
274 | * @throws InvalidArgumentException |
||
275 | */ |
||
276 | public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) { |
||
279 | |||
280 | /** |
||
281 | * @see BagOStuff::merge() |
||
282 | * |
||
283 | * @param string $key |
||
284 | * @param callable $callback Callback method to be executed |
||
285 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
286 | * @param int $attempts The amount of times to attempt a merge in case of failure |
||
287 | * @return bool Success |
||
288 | */ |
||
289 | protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10 ) { |
||
322 | |||
323 | /** |
||
324 | * Check and set an item |
||
325 | * |
||
326 | * @param mixed $casToken |
||
327 | * @param string $key |
||
328 | * @param mixed $value |
||
329 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
330 | * @return bool Success |
||
331 | * @throws Exception |
||
332 | */ |
||
333 | protected function cas( $casToken, $key, $value, $exptime = 0 ) { |
||
336 | |||
337 | /** |
||
338 | * @see BagOStuff::merge() |
||
339 | * |
||
340 | * @param string $key |
||
341 | * @param callable $callback Callback method to be executed |
||
342 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
343 | * @param int $attempts The amount of times to attempt a merge in case of failure |
||
344 | * @param int $flags Bitfield of BagOStuff::WRITE_* constants |
||
345 | * @return bool Success |
||
346 | */ |
||
347 | protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) { |
||
377 | |||
378 | /** |
||
379 | * Reset the TTL on a key if it exists |
||
380 | * |
||
381 | * @param string $key |
||
382 | * @param int $expiry |
||
383 | * @return bool Success Returns false if there is no key |
||
384 | * @since 1.28 |
||
385 | */ |
||
386 | public function changeTTL( $key, $expiry = 0 ) { |
||
391 | |||
392 | /** |
||
393 | * Acquire an advisory lock on a key string |
||
394 | * |
||
395 | * Note that if reentry is enabled, duplicate calls ignore $expiry |
||
396 | * |
||
397 | * @param string $key |
||
398 | * @param int $timeout Lock wait timeout; 0 for non-blocking [optional] |
||
399 | * @param int $expiry Lock expiry [optional]; 1 day maximum |
||
400 | * @param string $rclass Allow reentry if set and the current lock used this value |
||
401 | * @return bool Success |
||
402 | */ |
||
403 | public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) { |
||
436 | |||
437 | /** |
||
438 | * Release an advisory lock on a key string |
||
439 | * |
||
440 | * @param string $key |
||
441 | * @return bool Success |
||
442 | */ |
||
443 | public function unlock( $key ) { |
||
452 | |||
453 | /** |
||
454 | * Get a lightweight exclusive self-unlocking lock |
||
455 | * |
||
456 | * Note that the same lock cannot be acquired twice. |
||
457 | * |
||
458 | * This is useful for task de-duplication or to avoid obtrusive |
||
459 | * (though non-corrupting) DB errors like INSERT key conflicts |
||
460 | * or deadlocks when using LOCK IN SHARE MODE. |
||
461 | * |
||
462 | * @param string $key |
||
463 | * @param int $timeout Lock wait timeout; 0 for non-blocking [optional] |
||
464 | * @param int $expiry Lock expiry [optional]; 1 day maximum |
||
465 | * @param string $rclass Allow reentry if set and the current lock used this value |
||
466 | * @return ScopedCallback|null Returns null on failure |
||
467 | * @since 1.26 |
||
468 | */ |
||
469 | final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) { |
||
488 | |||
489 | /** |
||
490 | * Delete all objects expiring before a certain date. |
||
491 | * @param string $date The reference date in MW format |
||
492 | * @param callable|bool $progressCallback Optional, a function which will be called |
||
493 | * regularly during long-running operations with the percentage progress |
||
494 | * as the first parameter. |
||
495 | * |
||
496 | * @return bool Success, false if unimplemented |
||
497 | */ |
||
498 | public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) { |
||
502 | |||
503 | /** |
||
504 | * Get an associative array containing the item for each of the keys that have items. |
||
505 | * @param array $keys List of strings |
||
506 | * @param integer $flags Bitfield; supports READ_LATEST [optional] |
||
507 | * @return array |
||
508 | */ |
||
509 | public function getMulti( array $keys, $flags = 0 ) { |
||
519 | |||
520 | /** |
||
521 | * Batch insertion |
||
522 | * @param array $data $key => $value assoc array |
||
523 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
524 | * @return bool Success |
||
525 | * @since 1.24 |
||
526 | */ |
||
527 | public function setMulti( array $data, $exptime = 0 ) { |
||
536 | |||
537 | /** |
||
538 | * @param string $key |
||
539 | * @param mixed $value |
||
540 | * @param int $exptime |
||
541 | * @return bool Success |
||
542 | */ |
||
543 | public function add( $key, $value, $exptime = 0 ) { |
||
549 | |||
550 | /** |
||
551 | * Increase stored value of $key by $value while preserving its TTL |
||
552 | * @param string $key Key to increase |
||
553 | * @param int $value Value to add to $key (Default 1) |
||
554 | * @return int|bool New value or false on failure |
||
555 | */ |
||
556 | public function incr( $key, $value = 1 ) { |
||
571 | |||
572 | /** |
||
573 | * Decrease stored value of $key by $value while preserving its TTL |
||
574 | * @param string $key |
||
575 | * @param int $value |
||
576 | * @return int|bool New value or false on failure |
||
577 | */ |
||
578 | public function decr( $key, $value = 1 ) { |
||
581 | |||
582 | /** |
||
583 | * Increase stored value of $key by $value while preserving its TTL |
||
584 | * |
||
585 | * This will create the key with value $init and TTL $ttl instead if not present |
||
586 | * |
||
587 | * @param string $key |
||
588 | * @param int $ttl |
||
589 | * @param int $value |
||
590 | * @param int $init |
||
591 | * @return int|bool New value or false on failure |
||
592 | * @since 1.24 |
||
593 | */ |
||
594 | public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) { |
||
607 | |||
608 | /** |
||
609 | * Get the "last error" registered; clearLastError() should be called manually |
||
610 | * @return int ERR_* constant for the "last error" registry |
||
611 | * @since 1.23 |
||
612 | */ |
||
613 | public function getLastError() { |
||
616 | |||
617 | /** |
||
618 | * Clear the "last error" registry |
||
619 | * @since 1.23 |
||
620 | */ |
||
621 | public function clearLastError() { |
||
624 | |||
625 | /** |
||
626 | * Set the "last error" registry |
||
627 | * @param int $err ERR_* constant |
||
628 | * @since 1.23 |
||
629 | */ |
||
630 | protected function setLastError( $err ) { |
||
633 | |||
634 | /** |
||
635 | * Let a callback be run to avoid wasting time on special blocking calls |
||
636 | * |
||
637 | * The callbacks may or may not be called ever, in any particular order. |
||
638 | * They are likely to be invoked when something WRITE_SYNC is used used. |
||
639 | * They should follow a caching pattern as shown below, so that any code |
||
640 | * using the word will get it's result no matter what happens. |
||
641 | * @code |
||
642 | * $result = null; |
||
643 | * $workCallback = function () use ( &$result ) { |
||
644 | * if ( !$result ) { |
||
645 | * $result = .... |
||
646 | * } |
||
647 | * return $result; |
||
648 | * } |
||
649 | * @endcode |
||
650 | * |
||
651 | * @param callable $workCallback |
||
652 | * @since 1.28 |
||
653 | */ |
||
654 | public function addBusyCallback( callable $workCallback ) { |
||
657 | |||
658 | /** |
||
659 | * Modify a cache update operation array for EventRelayer::notify() |
||
660 | * |
||
661 | * This is used for relayed writes, e.g. for broadcasting a change |
||
662 | * to multiple data-centers. If the array contains a 'val' field |
||
663 | * then the command involves setting a key to that value. Note that |
||
664 | * for simplicity, 'val' is always a simple scalar value. This method |
||
665 | * is used to possibly serialize the value and add any cache-specific |
||
666 | * key/values needed for the relayer daemon (e.g. memcached flags). |
||
667 | * |
||
668 | * @param array $event |
||
669 | * @return array |
||
670 | * @since 1.26 |
||
671 | */ |
||
672 | public function modifySimpleRelayEvent( array $event ) { |
||
675 | |||
676 | /** |
||
677 | * @param string $text |
||
678 | */ |
||
679 | protected function debug( $text ) { |
||
686 | |||
687 | /** |
||
688 | * Convert an optionally relative time to an absolute time |
||
689 | * @param int $exptime |
||
690 | * @return int |
||
691 | */ |
||
692 | protected function convertExpiry( $exptime ) { |
||
699 | |||
700 | /** |
||
701 | * Convert an optionally absolute expiry time to a relative time. If an |
||
702 | * absolute time is specified which is in the past, use a short expiry time. |
||
703 | * |
||
704 | * @param int $exptime |
||
705 | * @return int |
||
706 | */ |
||
707 | protected function convertToRelative( $exptime ) { |
||
718 | |||
719 | /** |
||
720 | * Check if a value is an integer |
||
721 | * |
||
722 | * @param mixed $value |
||
723 | * @return bool |
||
724 | */ |
||
725 | protected function isInteger( $value ) { |
||
728 | |||
729 | /** |
||
730 | * Construct a cache key. |
||
731 | * |
||
732 | * @since 1.27 |
||
733 | * @param string $keyspace |
||
734 | * @param array $args |
||
735 | * @return string |
||
736 | */ |
||
737 | public function makeKeyInternal( $keyspace, $args ) { |
||
745 | |||
746 | /** |
||
747 | * Make a global cache key. |
||
748 | * |
||
749 | * @since 1.27 |
||
750 | * @param string ... Key component (variadic) |
||
751 | * @return string |
||
752 | */ |
||
753 | public function makeGlobalKey() { |
||
756 | |||
757 | /** |
||
758 | * Make a cache key, scoped to this instance's keyspace. |
||
759 | * |
||
760 | * @since 1.27 |
||
761 | * @param string ... Key component (variadic) |
||
762 | * @return string |
||
763 | */ |
||
764 | public function makeKey() { |
||
767 | |||
768 | /** |
||
769 | * @param integer $flag ATTR_* class constant |
||
770 | * @return integer QOS_* class constant |
||
771 | * @since 1.28 |
||
772 | */ |
||
773 | public function getQoS( $flag ) { |
||
776 | |||
777 | /** |
||
778 | * Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map |
||
779 | * |
||
780 | * @param BagOStuff[] $bags |
||
781 | * @return integer[] Resulting flag map (class ATTR_* constant => class QOS_* constant) |
||
782 | */ |
||
783 | protected function mergeFlagMaps( array $bags ) { |
||
797 | } |
||
798 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: