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 ModelAsController extends Controller implements NestedController { |
||
| 10 | private static $extensions = array('OldPageRedirector'); |
||
|
|
|||
| 11 | |||
| 12 | /** |
||
| 13 | * Get the appropriate {@link ContentController} for handling a {@link SiteTree} object, link it to the object and |
||
| 14 | * return it. |
||
| 15 | * |
||
| 16 | * @param SiteTree $sitetree |
||
| 17 | * @param string $action |
||
| 18 | * @return ContentController |
||
| 19 | */ |
||
| 20 | public static function controller_for(SiteTree $sitetree, $action = null) { |
||
| 21 | if ($sitetree->class == 'SiteTree') { |
||
| 22 | $controller = "ContentController"; |
||
| 23 | } else { |
||
| 24 | $ancestry = ClassInfo::ancestry($sitetree->class); |
||
| 25 | while ($class = array_pop($ancestry)) { |
||
| 26 | if (class_exists($class . "_Controller")) break; |
||
| 27 | } |
||
| 28 | $controller = ($class !== null) ? "{$class}_Controller" : "ContentController"; |
||
| 29 | } |
||
| 30 | |||
| 31 | if($action && class_exists($controller . '_' . ucfirst($action))) { |
||
| 32 | $controller = $controller . '_' . ucfirst($action); |
||
| 33 | } |
||
| 34 | |||
| 35 | return class_exists($controller) ? Injector::inst()->create($controller, $sitetree) : $sitetree; |
||
| 36 | } |
||
| 37 | |||
| 38 | public function init() { |
||
| 42 | |||
| 43 | View Code Duplication | protected function beforeHandleRequest(SS_HTTPRequest $request, DataModel $model) { |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @uses ModelAsController::getNestedController() |
||
| 59 | * @param SS_HTTPRequest $request |
||
| 60 | * @param DataModel $model |
||
| 61 | * @return SS_HTTPResponse |
||
| 62 | */ |
||
| 63 | public function handleRequest(SS_HTTPRequest $request, DataModel $model) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return ContentController |
||
| 99 | * @throws Exception If URLSegment not passed in as a request parameter. |
||
| 100 | */ |
||
| 101 | public function getNestedController() { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @deprecated 4.0 Use OldPageRedirector::find_old_page instead |
||
| 138 | * |
||
| 139 | * @param string $URLSegment A subset of the url. i.e in /home/contact/ home and contact are URLSegment. |
||
| 140 | * @param int $parent The ID of the parent of the page the URLSegment belongs to. |
||
| 141 | * @param bool $ignoreNestedURLs |
||
| 142 | * @return SiteTree |
||
| 143 | */ |
||
| 144 | static public function find_old_page($URLSegment, $parent = null, $ignoreNestedURLs = false) { |
||
| 152 | } |
||
| 153 |