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 |
||
38 | class Episode extends Model |
||
39 | { |
||
40 | |||
41 | use Sluggable; |
||
42 | use Validation { |
||
43 | makeValidator as baseMakeValidator; |
||
44 | } |
||
45 | |||
46 | protected $table = 'cosmicradiotv_podcast_episodes'; |
||
47 | |||
48 | protected $slugs = ['slug' => 'title']; |
||
49 | |||
50 | protected $dates = ['release']; |
||
51 | |||
52 | protected $fillable = ['show_id', 'title', 'slug', 'summary', 'content', 'length', 'release', 'published']; |
||
53 | |||
54 | public $rules = [ |
||
55 | 'show_id' => ['required', 'exists:cosmicradiotv_podcast_shows,id'], |
||
56 | 'title' => ['required'], |
||
57 | // Unique rule: NULL gets replaced with ID if exists, must have all 3 for show_id based bellow |
||
58 | 'slug' => ['alpha_dash', 'unique:cosmicradiotv_podcast_episodes,slug,NULL'], |
||
59 | 'summary' => [], |
||
60 | 'content' => [], |
||
61 | 'length' => ['numeric', 'min:0'], |
||
62 | 'release' => ['date'], |
||
63 | 'published' => ['boolean'] |
||
64 | ]; |
||
65 | |||
66 | /* |
||
67 | * Relations |
||
68 | */ |
||
69 | public $belongsTo = [ |
||
70 | 'show' => ['CosmicRadioTV\Podcast\Models\Show'], |
||
71 | ]; |
||
72 | |||
73 | public $belongsToMany = [ |
||
74 | 'tags' => ['CosmicRadioTV\Podcast\Models\Tag', 'table' => 'cosmicradiotv_podcast_episodes_tags'], |
||
75 | ]; |
||
76 | |||
77 | // Needed this to properly display releases in the create/update episode form |
||
78 | public $hasMany = [ |
||
79 | 'releases' => ['CosmicRadioTV\Podcast\Models\Release'], |
||
80 | ]; |
||
81 | |||
82 | public $attachOne = [ |
||
83 | 'image' => ['System\Models\File'] |
||
84 | ]; |
||
85 | |||
86 | /** |
||
87 | * @var string Where the URL for the episode can be cached to |
||
88 | */ |
||
89 | public $url; |
||
90 | |||
91 | /** |
||
92 | * @var Array Stores the cached next episode data |
||
93 | */ |
||
94 | protected $nextEpisodeCache = [true => null, false => null]; |
||
95 | |||
96 | /** |
||
97 | * @var Array Stores the cached previous episode data |
||
98 | */ |
||
99 | protected $previousEpisodeCache = [true => null, false => null]; |
||
100 | |||
101 | /** |
||
102 | * Returns the next episode (cached for this instance) |
||
103 | * @param boolean $ofShow If true returns the next episode of the same show |
||
104 | * @return Episode The next episode |
||
105 | */ |
||
106 | public function nextCached($ofShow = false) { |
||
112 | |||
113 | /** |
||
114 | * Returns the previous episode (cached for this instance) |
||
115 | * @param boolean $ofShow If true returns the next episode of the same show |
||
116 | * @return Episode The previous episode |
||
117 | */ |
||
118 | public function previousCached($ofShow = false) { |
||
124 | |||
125 | /** |
||
126 | * Returns the next episode |
||
127 | * @param boolean $ofShow If true returns the next episode of the same show |
||
128 | * @return Episode The next episode |
||
129 | */ |
||
130 | View Code Duplication | public function next($ofShow = false) { |
|
137 | /** |
||
138 | * Returns the previous episode |
||
139 | * @param boolean $ofShow If true returns the previous episode of the same show |
||
140 | * @return Episode The previous episode |
||
141 | */ |
||
142 | View Code Duplication | public function previous($ofShow = false) { |
|
149 | |||
150 | /** |
||
151 | * Runs before an episode is deleted to remove all the releases that depend on it |
||
152 | */ |
||
153 | protected function beforeDelete() |
||
157 | |||
158 | /** |
||
159 | * Modifies validation rules so unique checks take show_id into account |
||
160 | * |
||
161 | * @param $data |
||
162 | * @param $rules |
||
163 | * @param $customMessages |
||
164 | * @param $attributeNames |
||
165 | * |
||
166 | * @return \Illuminate\Validation\Validator |
||
167 | */ |
||
168 | protected static function makeValidator($data, $rules, $customMessages, $attributeNames) |
||
184 | |||
185 | /** |
||
186 | * Modified unique slug finder to take into account the show_id |
||
187 | * |
||
188 | * @param string $name The database column name. |
||
189 | * @param string $value The desired column value. |
||
190 | * |
||
191 | * @return string A safe value that is unique. |
||
192 | */ |
||
193 | protected function getSluggableUniqueAttributeValue($name, $value) |
||
213 | |||
214 | |||
215 | } |