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 |
||
| 26 | abstract class QuickTemplate { |
||
| 27 | |||
| 28 | /** @var Config $config */ |
||
| 29 | protected $config; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param Config $config |
||
| 33 | */ |
||
| 34 | function __construct( Config $config = null ) { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Sets the value $value to $name |
||
| 46 | * @param string $name |
||
| 47 | * @param mixed $value |
||
| 48 | */ |
||
| 49 | public function set( $name, $value ) { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * extends the value of data with name $name with the value $value |
||
| 55 | * @since 1.25 |
||
| 56 | * @param string $name |
||
| 57 | * @param mixed $value |
||
| 58 | */ |
||
| 59 | public function extend( $name, $value ) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Gets the template data requested |
||
| 69 | * @since 1.22 |
||
| 70 | * @param string $name Key for the data |
||
| 71 | * @param mixed $default Optional default (or null) |
||
| 72 | * @return mixed The value of the data requested or the deafult |
||
| 73 | */ |
||
| 74 | View Code Duplication | public function get( $name, $default = null ) { |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $name |
||
| 84 | * @param mixed $value |
||
| 85 | */ |
||
| 86 | public function setRef( $name, &$value ) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param MediaWikiI18N $t |
||
| 92 | */ |
||
| 93 | public function setTranslator( &$t ) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Main function, used by classes that subclass QuickTemplate |
||
| 99 | * to show the actual HTML output |
||
| 100 | */ |
||
| 101 | abstract public function execute(); |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @private |
||
| 105 | * @param string $str |
||
| 106 | */ |
||
| 107 | function text( $str ) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @private |
||
| 113 | * @param string $str |
||
| 114 | */ |
||
| 115 | function html( $str ) { |
||
| 116 | echo $this->data[$str]; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @private |
||
| 121 | * @param string $str |
||
| 122 | */ |
||
| 123 | function msg( $str ) { |
||
| 124 | echo htmlspecialchars( $this->translator->translate( $str ) ); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @private |
||
| 129 | * @param string $str |
||
| 130 | */ |
||
| 131 | function msgHtml( $str ) { |
||
| 132 | echo $this->translator->translate( $str ); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * An ugly, ugly hack. |
||
| 137 | * @private |
||
| 138 | * @param string $str |
||
| 139 | */ |
||
| 140 | function msgWiki( $str ) { |
||
| 141 | global $wgOut; |
||
| 142 | |||
| 143 | $text = $this->translator->translate( $str ); |
||
| 144 | echo $wgOut->parse( $text ); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @private |
||
| 149 | * @param string $str |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | function haveData( $str ) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @private |
||
| 158 | * |
||
| 159 | * @param string $str |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | function haveMsg( $str ) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get the Skin object related to this object |
||
| 169 | * |
||
| 170 | * @return Skin |
||
| 171 | */ |
||
| 172 | public function getSkin() { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Fetch the output of a QuickTemplate and return it |
||
| 178 | * |
||
| 179 | * @since 1.23 |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | public function getHTML() { |
||
| 189 | } |
||
| 190 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: