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 | abstract class CompilerFacade extends ValuesCompiler |
||
| 10 | { |
||
| 11 | protected static $mixinBlocks = array(); |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Record a closure as a mixin block during execution jade template time. |
||
| 15 | * |
||
| 16 | * @param string mixin name |
||
| 17 | * @param string mixin block treatment |
||
| 18 | */ |
||
| 19 | public static function recordMixinBlock($name, $func = null) |
||
| 20 | { |
||
| 21 | if (!isset(static::$mixinBlocks[$name])) { |
||
| 22 | static::$mixinBlocks[$name] = array(); |
||
| 23 | } |
||
| 24 | array_push(static::$mixinBlocks[$name], $func); |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Record a closure as a mixin block during execution jade template time. |
||
| 29 | * |
||
| 30 | * @param string mixin name |
||
| 31 | * @param string mixin block treatment |
||
| 32 | */ |
||
| 33 | public static function callMixinBlock($name, $attributes = array()) |
||
| 34 | { |
||
| 35 | if (isset(static::$mixinBlocks[$name]) && is_array($mixinBlocks = static::$mixinBlocks[$name])) { |
||
| 36 | $func = end($mixinBlocks); |
||
| 37 | if (is_callable($func)) { |
||
| 38 | $func($attributes); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Record a closure as a mixin block during execution jade template time |
||
| 45 | * and propagate variables. |
||
| 46 | * |
||
| 47 | * @param string mixin name |
||
| 48 | * @param &array variables handler propagated from parent scope |
||
| 49 | * @param string mixin block treatment |
||
| 50 | */ |
||
| 51 | public static function callMixinBlockWithVars($name, &$varHandler, $attributes = array()) |
||
| 52 | { |
||
| 53 | if (isset(static::$mixinBlocks[$name]) && is_array($mixinBlocks = static::$mixinBlocks[$name])) { |
||
| 54 | $func = end($mixinBlocks); |
||
| 55 | if (is_callable($func)) { |
||
| 56 | $func($varHandler, $attributes); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * End of the record of the mixin block. |
||
| 63 | * |
||
| 64 | * @param string mixin name |
||
| 65 | */ |
||
| 66 | public static function terminateMixinBlock($name) |
||
| 67 | { |
||
| 68 | if (isset(static::$mixinBlocks[$name])) { |
||
| 69 | array_pop(static::$mixinBlocks); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get property from object. |
||
| 75 | * |
||
| 76 | * @param object $object source object |
||
| 77 | * @param mixed $key key to retrive from the object or the array |
||
| 78 | * |
||
| 79 | * @return mixed |
||
| 80 | */ |
||
| 81 | public static function getPropertyFromObject($anything, $key) |
||
| 82 | { |
||
| 83 | return isset($anything->$key) |
||
| 84 | ? $anything->$key |
||
| 85 | : (method_exists($anything, $method = 'get' . ucfirst($key)) |
||
| 86 | ? $anything->$method() |
||
| 87 | : (method_exists($anything, $key) |
||
| 88 | ? array($anything, $key) |
||
| 89 | : null |
||
| 90 | ) |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get property from object or entry from array. |
||
| 96 | * |
||
| 97 | * @param object|array $anything source |
||
| 98 | * @param mixed $key key to retrive from the object or the array |
||
| 99 | * |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | public static function getPropertyFromAnything($anything, $key) |
||
| 103 | { |
||
| 104 | return is_array($anything) |
||
| 105 | ? (isset($anything[$key]) |
||
| 106 | ? $anything[$key] |
||
| 107 | : null |
||
| 108 | ) : (is_object($anything) |
||
| 109 | ? static::getPropertyFromObject($anything, $key) |
||
| 110 | : null |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Merge given attributes such as tag attributes with mixin attributes. |
||
| 116 | * |
||
| 117 | * @param array $attributes |
||
| 118 | * @param array $mixinAttributes |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public static function withMixinAttributes($attributes, $mixinAttributes) |
||
| 123 | { |
||
| 124 | foreach ($mixinAttributes as $attribute) { |
||
| 125 | if ($attribute['name'] === 'class') { |
||
| 126 | $value = static::joinAny($attribute['value']); |
||
| 127 | $attributes['class'] = empty($attributes['class']) |
||
| 128 | ? $value |
||
| 129 | : static::joinAny($attributes['class']) . ' ' . $value; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | if (isset($attributes['class'])) { |
||
| 133 | $attributes['class'] = implode(' ', array_unique(explode(' ', $attributes['class']))); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $attributes; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Display a list of attributes with the given quote character in HTML. |
||
| 141 | * |
||
| 142 | * @param array $attributes |
||
| 143 | * @param string $quote |
||
| 144 | */ |
||
| 145 | public static function displayAttributes($attributes, $quote, $terse) |
||
| 146 | { |
||
| 147 | if (is_array($attributes) || $attributes instanceof Traversable) { |
||
| 148 | foreach ($attributes as $key => $value) { |
||
| 149 | if ($key !== 'class' && $value !== false && $value !== 'null') { |
||
| 150 | if ($value === true) { |
||
| 151 | echo ' ' . $key . ($terse ? '' : '=' . $quote . $key . $quote); |
||
| 152 | continue; |
||
| 153 | } |
||
| 154 | echo ' ' . $key . '=' . $quote . htmlspecialchars($value) . $quote; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Return true if the given value can be display |
||
| 162 | * (null or false should not be displayed in the output HTML). |
||
| 163 | * |
||
| 164 | * @param $value |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public static function isDisplayable($value) |
||
| 169 | { |
||
| 170 | return !is_null($value) && $value !== false; |
||
| 171 | } |
||
| 172 | } |
||
| 173 |