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) { |
|
| 44 | parent::beforeHandleRequest($request, $model); |
||
| 45 | // If the database has not yet been created, redirect to the build page. |
||
| 46 | if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) { |
||
| 47 | $this->getResponse()->redirect(Controller::join_links( |
||
| 48 | Director::absoluteBaseURL(), |
||
| 49 | 'dev/build', |
||
| 50 | '?' . array( |
||
| 51 | 'returnURL' => isset($_GET['url']) ? $_GET['url'] : null, |
||
| 52 | ) |
||
| 53 | )); |
||
| 54 | } |
||
| 55 | } |
||
| 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) { |
||
| 64 | $this->beforeHandleRequest($request, $model); |
||
| 65 | |||
| 66 | // If we had a redirection or something, halt processing. |
||
| 67 | if($this->getResponse()->isFinished()) { |
||
| 68 | $this->popCurrent(); |
||
| 69 | return $this->getResponse(); |
||
| 70 | } |
||
| 71 | |||
| 72 | // If the database has not yet been created, redirect to the build page. |
||
| 73 | View Code Duplication | if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) { |
|
| 74 | $this->getResponse()->redirect(Director::absoluteBaseURL() . 'dev/build?returnURL=' . (isset($_GET['url']) ? urlencode($_GET['url']) : null)); |
||
| 75 | $this->popCurrent(); |
||
| 76 | |||
| 77 | return $this->getResponse(); |
||
| 78 | } |
||
| 79 | |||
| 80 | try { |
||
| 81 | $result = $this->getNestedController(); |
||
| 82 | |||
| 83 | if($result instanceof RequestHandler) { |
||
| 84 | $result = $result->handleRequest($this->getRequest(), $model); |
||
| 85 | } else if(!($result instanceof SS_HTTPResponse)) { |
||
| 86 | user_error("ModelAsController::getNestedController() returned bad object type '" . |
||
| 87 | get_class($result)."'", E_USER_WARNING); |
||
| 88 | } |
||
| 89 | } catch(SS_HTTPResponse_Exception $responseException) { |
||
| 90 | $result = $responseException->getResponse(); |
||
| 91 | } |
||
| 92 | |||
| 93 | $this->popCurrent(); |
||
| 94 | return $result; |
||
| 95 | } |
||
| 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 |