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 |
||
27 | class ModelAsController extends Controller implements NestedController { |
||
28 | |||
29 | private static $extensions = array('SilverStripe\\CMS\\Controllers\\OldPageRedirector'); |
||
30 | |||
31 | /** |
||
32 | * Get the appropriate {@link ContentController} for handling a {@link SiteTree} object, link it to the object and |
||
33 | * return it. |
||
34 | * |
||
35 | * @param SiteTree $sitetree |
||
36 | * @param string $action |
||
37 | * @return ContentController |
||
38 | */ |
||
39 | public static function controller_for(SiteTree $sitetree, $action = null) { |
||
40 | if ($sitetree->class == 'SilverStripe\\CMS\\Model\\SiteTree') { |
||
41 | $controller = "SilverStripe\\CMS\\Controllers\\ContentController"; |
||
42 | } else { |
||
43 | $ancestry = ClassInfo::ancestry($sitetree->class); |
||
44 | while ($class = array_pop($ancestry)) { |
||
45 | if (class_exists($class . "_Controller")) { |
||
46 | break; |
||
47 | } |
||
48 | } |
||
49 | $controller = ($class !== null) |
||
50 | ? "{$class}_Controller" |
||
51 | : "SilverStripe\\CMS\\Controllers\\ContentController"; |
||
52 | } |
||
53 | |||
54 | if($action && class_exists($controller . '_' . ucfirst($action))) { |
||
|
|||
55 | $controller = $controller . '_' . ucfirst($action); |
||
56 | } |
||
57 | |||
58 | return class_exists($controller) ? Injector::inst()->create($controller, $sitetree) : $sitetree; |
||
59 | } |
||
60 | |||
61 | public function init() { |
||
65 | |||
66 | View Code Duplication | protected function beforeHandleRequest(SS_HTTPRequest $request, DataModel $model) { |
|
67 | parent::beforeHandleRequest($request, $model); |
||
68 | // If the database has not yet been created, redirect to the build page. |
||
69 | /** @skipUpgrade */ |
||
70 | if(!DB::is_active() || !ClassInfo::hasTable('SiteTree')) { |
||
71 | $this->getResponse()->redirect(Controller::join_links( |
||
72 | Director::absoluteBaseURL(), |
||
73 | 'dev/build', |
||
74 | '?' . http_build_query(array( |
||
75 | 'returnURL' => isset($_GET['url']) ? $_GET['url'] : null, |
||
76 | )) |
||
77 | )); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @uses ModelAsController::getNestedController() |
||
83 | * @param SS_HTTPRequest $request |
||
84 | * @param DataModel $model |
||
85 | * @return SS_HTTPResponse |
||
86 | */ |
||
87 | public function handleRequest(SS_HTTPRequest $request, DataModel $model) { |
||
121 | |||
122 | /** |
||
123 | * @return ContentController |
||
124 | * @throws Exception If URLSegment not passed in as a request parameter. |
||
125 | */ |
||
126 | public function getNestedController() { |
||
161 | |||
162 | /** |
||
163 | * @deprecated 4.0 Use OldPageRedirector::find_old_page instead |
||
164 | * |
||
165 | * @param string $URLSegment A subset of the url. i.e in /home/contact/ home and contact are URLSegment. |
||
166 | * @param int $parent The ID of the parent of the page the URLSegment belongs to. |
||
167 | * @param bool $ignoreNestedURLs |
||
168 | * @return SiteTree |
||
169 | */ |
||
170 | static public function find_old_page($URLSegment, $parent = null, $ignoreNestedURLs = false) { |
||
178 | } |
||
179 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: