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 |
||
3 | class GalleryPage_Controller extends Page_Controller |
||
|
|||
4 | { |
||
5 | public function init() { |
||
8 | |||
9 | View Code Duplication | public function PaginatedImages() |
|
19 | |||
20 | /** |
||
21 | * Generate an image based on the provided type |
||
22 | * (either ) |
||
23 | * |
||
24 | * @param Image $image |
||
25 | * @param string $thumbnail generate a smaller image (based on thumbnail settings) |
||
26 | * @return void |
||
27 | */ |
||
28 | protected function ScaledImage(Image $image, $thumbnail = false) |
||
65 | |||
66 | protected function GalleryImage(Image $image) |
||
67 | { |
||
68 | return $this->ScaledImage($image); |
||
69 | } |
||
70 | |||
71 | protected function GalleryThumbnail(Image $image) |
||
72 | { |
||
73 | return $this->ScaledImage($image, true); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Generate an image gallery from the Gallery template, if no images are |
||
78 | * available, then return an empty string. |
||
79 | * |
||
80 | * @return string |
||
81 | */ |
||
82 | public function Gallery() |
||
83 | { |
||
84 | if ($this->Images()->exists()) { |
||
85 | |||
86 | // Create a list of images with generated gallery image and thumbnail |
||
87 | $images = ArrayList::create(); |
||
88 | foreach ($this->PaginatedImages() as $image) { |
||
89 | $image_data = $image->toMap(); |
||
90 | $image_data["GalleryImage"] = $this->GalleryImage($image); |
||
91 | $image_data["GalleryThumbnail"] = $this->GalleryThumbnail($image); |
||
92 | $images->add(ArrayData::create($image_data)); |
||
93 | } |
||
94 | |||
95 | $vars = array( |
||
96 | 'Images' => $images, |
||
97 | 'Width' => $this->ImageWidth, |
||
98 | 'Height' => $this->ImageHeight |
||
99 | ); |
||
100 | |||
101 | return $this->renderWith('Gallery',$vars); |
||
102 | } else { |
||
103 | return ""; |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.