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:
| 1 | <?php |
||
| 29 | class PageProps { |
||
|
|
|||
| 30 | |||
| 31 | /** |
||
| 32 | * @var PageProps |
||
| 33 | */ |
||
| 34 | private static $instance; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Overrides the default instance of this class |
||
| 38 | * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined. |
||
| 39 | * |
||
| 40 | * If this method is used it MUST also be called with null after a test to ensure a new |
||
| 41 | * default instance is created next time getInstance is called. |
||
| 42 | * |
||
| 43 | * @since 1.27 |
||
| 44 | * |
||
| 45 | * @param PageProps|null $store |
||
| 46 | * |
||
| 47 | * @return ScopedCallback to reset the overridden value |
||
| 48 | * @throws MWException |
||
| 49 | */ |
||
| 50 | View Code Duplication | public static function overrideInstance( PageProps $store = null ) { |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @return PageProps |
||
| 65 | */ |
||
| 66 | public static function getInstance() { |
||
| 72 | |||
| 73 | /** Cache parameters */ |
||
| 74 | const CACHE_TTL = 10; // integer; TTL in seconds |
||
| 75 | const CACHE_SIZE = 100; // integer; max cached pages |
||
| 76 | |||
| 77 | /** Property cache */ |
||
| 78 | private $cache = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Create a PageProps object |
||
| 82 | */ |
||
| 83 | private function __construct() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Given one or more Titles and one or more names of properties, |
||
| 89 | * returns an associative array mapping page ID to property value. |
||
| 90 | * Pages in the provided set of Titles that do not have a value for |
||
| 91 | * the given properties will not appear in the returned array. If a |
||
| 92 | * single Title is provided, it does not need to be passed in an array, |
||
| 93 | * but an array will always be returned. If a single property name is |
||
| 94 | * provided, it does not need to be passed in an array. In that case, |
||
| 95 | * an associtive array mapping page ID to property value will be |
||
| 96 | * returned; otherwise, an associative array mapping page ID to |
||
| 97 | * an associative array mapping property name to property value will be |
||
| 98 | * returned. An empty array will be returned if no matching properties |
||
| 99 | * were found. |
||
| 100 | * |
||
| 101 | * @param Title[]|Title $titles |
||
| 102 | * @param string[]|string $propertyNames |
||
| 103 | * @return array associative array mapping page ID to property value |
||
| 104 | */ |
||
| 105 | public function getProperties( $titles, $propertyNames ) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get all page property values. |
||
| 166 | * Given one or more Titles, returns an associative array mapping page |
||
| 167 | * ID to an associative array mapping property names to property |
||
| 168 | * values. Pages in the provided set of Titles that do not have any |
||
| 169 | * properties will not appear in the returned array. If a single Title |
||
| 170 | * is provided, it does not need to be passed in an array, but an array |
||
| 171 | * will always be returned. An empty array will be returned if no |
||
| 172 | * matching properties were found. |
||
| 173 | * |
||
| 174 | * @param Title[]|Title $titles |
||
| 175 | * @return array associative array mapping page ID to property value array |
||
| 176 | */ |
||
| 177 | public function getAllProperties( $titles ) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param Title[]|Title $titles |
||
| 230 | * @return array array of good page IDs |
||
| 231 | */ |
||
| 232 | private function getGoodIDs( $titles ) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get a property from the cache. |
||
| 252 | * |
||
| 253 | * @param int $pageID page ID of page being queried |
||
| 254 | * @param string $propertyName name of property being queried |
||
| 255 | * @return string|bool property value array or false if not found |
||
| 256 | */ |
||
| 257 | private function getCachedProperty( $pageID, $propertyName ) { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Get properties from the cache. |
||
| 272 | * |
||
| 273 | * @param int $pageID page ID of page being queried |
||
| 274 | * @return string|bool property value array or false if not found |
||
| 275 | */ |
||
| 276 | private function getCachedProperties( $pageID ) { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Save a property to the cache. |
||
| 285 | * |
||
| 286 | * @param int $pageID page ID of page being cached |
||
| 287 | * @param string $propertyName name of property being cached |
||
| 288 | * @param mixed $propertyValue value of property |
||
| 289 | */ |
||
| 290 | private function cacheProperty( $pageID, $propertyName, $propertyValue ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Save properties to the cache. |
||
| 296 | * |
||
| 297 | * @param int $pageID page ID of page being cached |
||
| 298 | * @param string[] $pageProperties associative array of page properties to be cached |
||
| 299 | */ |
||
| 300 | private function cacheProperties( $pageID, $pageProperties ) { |
||
| 304 | } |
||
| 305 |