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 |
||
| 109 | class TestOfMonthWeekdaysBuild extends TestOfMonthWeekdays |
||
| 110 | { |
||
| 111 | /** |
||
| 112 | * TestOfMonthWeekdaysBuild constructor. |
||
| 113 | */ |
||
| 114 | public function __construct() |
||
| 115 | { |
||
| 116 | $this->UnitTestCase('Test of Month_Weekdays::build()'); |
||
| 117 | } |
||
| 118 | |||
| 119 | public function testSize() |
||
| 120 | { |
||
| 121 | $this->cal->build(); |
||
| 122 | $this->assertEqual(35, $this->cal->size()); |
||
| 123 | } |
||
| 124 | |||
| 125 | public function testFetch() |
||
| 126 | { |
||
| 127 | $this->cal->build(); |
||
| 128 | $i = 0; |
||
| 129 | while ($Child = $this->cal->fetch()) { |
||
| 130 | ++$i; |
||
| 131 | } |
||
| 132 | $this->assertEqual(35, $i); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function testFetchAll() |
||
| 136 | { |
||
| 137 | $this->cal->build(); |
||
| 138 | $children = []; |
||
| 139 | $i = 1; |
||
| 140 | while ($Child = $this->cal->fetch()) { |
||
| 141 | $children[$i] = $Child; |
||
| 142 | ++$i; |
||
| 143 | } |
||
| 144 | $this->assertEqual($children, $this->cal->fetchAll()); |
||
| 145 | } |
||
| 146 | |||
| 147 | public function testSelection() |
||
| 148 | { |
||
| 149 | require_once CALENDAR_ROOT . 'Day.php'; |
||
| 150 | $selection = [new Calendar_Day(2003, 10, 25)]; |
||
| 151 | $this->cal->build($selection); |
||
| 152 | $daysInPrevMonth = (0 == CALENDAR_FIRST_DAY_OF_WEEK) ? 3 : 2; |
||
| 153 | $end = 25 + $daysInPrevMonth; |
||
| 154 | $i = 1; |
||
| 155 | while ($Child = $this->cal->fetch()) { |
||
| 156 | if ($i == $end) { |
||
| 157 | break; |
||
| 158 | } |
||
| 159 | ++$i; |
||
| 160 | } |
||
| 161 | $this->assertTrue($Child->isSelected()); |
||
| 162 | $this->assertEqual(25, $Child->day); |
||
| 163 | } |
||
| 164 | |||
| 165 | public function testEmptyCount() |
||
| 166 | { |
||
| 167 | $this->cal->build(); |
||
| 168 | $empty = 0; |
||
| 169 | while ($Child = $this->cal->fetch()) { |
||
| 170 | if ($Child->isEmpty()) { |
||
| 171 | ++$empty; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | $this->assertEqual(4, $empty); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function testEmptyCount2() |
||
| 178 | { |
||
| 179 | $this->cal = new Calendar_Month_Weekdays(2010, 3); |
||
| 180 | $this->cal->build(); |
||
| 181 | $empty = 0; |
||
| 182 | while ($Child = $this->cal->fetch()) { |
||
| 183 | if ($Child->isEmpty()) { |
||
| 184 | ++$empty; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | $this->assertEqual(4, $empty); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function testEmptyCount3() |
||
| 191 | { |
||
| 192 | $this->cal = new Calendar_Month_Weekdays(2010, 6); |
||
| 193 | $this->cal->build(); |
||
| 194 | $empty = 0; |
||
| 195 | while ($Child = $this->cal->fetch()) { |
||
| 196 | if ($Child->isEmpty()) { |
||
| 197 | ++$empty; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | $this->assertEqual(5, $empty); |
||
| 201 | } |
||
| 202 | |||
| 203 | public function testEmptyDaysBefore_AfterAdjust() |
||
| 204 | { |
||
| 205 | $this->cal = new Calendar_Month_Weekdays(2004, 0); |
||
| 206 | $this->cal->build(); |
||
| 207 | $expected = (CALENDAR_FIRST_DAY_OF_WEEK == 0) ? 1 : 0; |
||
| 208 | $this->assertEqual($expected, $this->cal->tableHelper->getEmptyDaysBefore()); |
||
| 209 | } |
||
| 210 | |||
| 211 | public function testEmptyDaysBefore() |
||
| 217 | } |
||
| 218 | |||
| 219 | public function testEmptyDaysBefore2() |
||
| 225 | } |
||
| 226 | |||
| 227 | public function testEmptyDaysAfter() |
||
| 228 | { |
||
| 229 | $this->cal = new Calendar_Month_Weekdays(2010, 3); |
||
| 230 | $this->cal->build(); |
||
| 231 | $expected = (CALENDAR_FIRST_DAY_OF_WEEK == 0) ? 30 : 31; |
||
| 232 | $this->assertEqual($expected, $this->cal->tableHelper->getEmptyDaysAfter()); |
||
| 233 | } |
||
| 234 | |||
| 235 | public function testEmptyDaysAfter2() |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | if (!defined('TEST_RUNNING')) { |
||
| 245 | define('TEST_RUNNING', true); |
||
| 246 | $test = new TestOfMonthWeekdays(); |
||
| 247 | $test->run(new HtmlReporter()); |
||
| 248 | $test = new TestOfMonthWeekdaysBuild(); |
||
| 251 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.