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 |
||
| 9 | class SS_TemplateManifest { |
||
| 10 | |||
| 11 | const TEMPLATES_DIR = 'templates'; |
||
| 12 | |||
| 13 | protected $base; |
||
| 14 | protected $tests; |
||
| 15 | protected $cache; |
||
| 16 | protected $cacheKey; |
||
| 17 | protected $project; |
||
| 18 | protected $inited; |
||
| 19 | protected $templates = array(); |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Constructs a new template manifest. The manifest is not actually built |
||
| 23 | * or loaded from cache until needed. |
||
| 24 | * |
||
| 25 | * @param string $base The base path. |
||
| 26 | * @param string $project Path to application code |
||
| 27 | * |
||
| 28 | * @param bool $includeTests Include tests in the manifest. |
||
| 29 | * @param bool $forceRegen Force the manifest to be regenerated. |
||
| 30 | */ |
||
| 31 | public function __construct($base, $project, $includeTests = false, $forceRegen = false) { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @return string |
||
| 49 | */ |
||
| 50 | public function getBase() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Generate a unique cache key to avoid manifest cache collisions. |
||
| 56 | * We compartmentalise based on the base path, the given project, and whether |
||
| 57 | * or not we intend to include tests. |
||
| 58 | * @param boolean $includeTests |
||
| 59 | * @return string |
||
| 60 | */ |
||
| 61 | public function getCacheKey($includeTests = false) { |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Returns a map of all template information. The map is in the following |
||
| 73 | * format: |
||
| 74 | * |
||
| 75 | * <code> |
||
| 76 | * array( |
||
| 77 | * 'moduletemplate' => array( |
||
| 78 | * 'main' => '/path/to/module/templates/Main.ss' |
||
| 79 | * ), |
||
| 80 | * 'include' => array( |
||
| 81 | * 'include' => '/path/to/module/templates/Includes/Include.ss' |
||
| 82 | * ), |
||
| 83 | * 'page' => array( |
||
| 84 | * 'themes' => array( |
||
| 85 | * 'simple' => array( |
||
| 86 | * 'main' => '/path/to/theme/Page.ss' |
||
| 87 | * 'Layout' => '/path/to/theme/Layout/Page.ss' |
||
| 88 | * ) |
||
| 89 | * ) |
||
| 90 | * ) |
||
| 91 | * ) |
||
| 92 | * </code> |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | public function getTemplates() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Returns a set of possible candidate templates that match a certain |
||
| 106 | * template name. |
||
| 107 | * |
||
| 108 | * This is the same as extracting an individual array element from |
||
| 109 | * {@link SS_TemplateManifest::getTemplates()}. |
||
| 110 | * |
||
| 111 | * @param string $name |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function getTemplate($name) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns the correct candidate template. In order of importance, application |
||
| 130 | * project code, current theme and finally modules. |
||
| 131 | * |
||
| 132 | * @param string $name |
||
| 133 | * @param string $theme - theme name |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function getCandidateTemplate($name, $theme = null) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Regenerates the manifest by scanning the base path. |
||
| 160 | * |
||
| 161 | * @param bool $cache |
||
| 162 | */ |
||
| 163 | public function regenerate($cache = true) { |
||
| 180 | |||
| 181 | public function handleFile($basename, $pathname, $depth) |
||
| 262 | |||
| 263 | protected function init() { |
||
| 271 | } |
||
| 272 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: