Complex classes like FooGallery_Pro_Video_YouTube often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FooGallery_Pro_Video_YouTube, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class FooGallery_Pro_Video_YouTube extends FooGallery_Pro_Video_Base { |
||
15 | |||
16 | // region Properties |
||
17 | |||
18 | /** |
||
19 | * The regular expression used to match a YouTube video URL. |
||
20 | * @var string |
||
21 | */ |
||
22 | public $regex_pattern; |
||
23 | |||
24 | // endregion |
||
25 | |||
26 | function __construct() { |
||
29 | |||
30 | /** |
||
31 | * Takes a URL and checks if this class handles it. |
||
32 | * |
||
33 | * @param string $url The URL to check. |
||
34 | * @param array &$matches Optional. If matches is provided, it is passed to the `preg_match` call used to check the URL. |
||
35 | * @return int Returns 1 if the URL is handled, 0 if it is not, or FALSE if an error occurred. |
||
36 | */ |
||
37 | function handles($url, &$matches = array()){ |
||
40 | |||
41 | /** |
||
42 | * Takes a string value and determines whether or not it is a YouTube video id. |
||
43 | * |
||
44 | * @param string $value The value to test. |
||
45 | * @return bool |
||
46 | * |
||
47 | * @see https://stackoverflow.com/questions/6180138/whats-the-maximum-length-of-a-youtube-video-id |
||
48 | * |
||
49 | * @description At present YouTube's video ids always have 11 characters, while this is not |
||
50 | * guaranteed it should stay that way until all 73,786,976,294,838,206,464 possible combinations |
||
51 | * are exhausted. |
||
52 | */ |
||
53 | function is_video_id($value) { |
||
56 | |||
57 | /** |
||
58 | * Takes a string value and determines whether or not it is a YouTube playlist id. |
||
59 | * |
||
60 | * @param string $value The value to test. |
||
61 | * @return bool |
||
62 | * |
||
63 | * @description At present YouTube's playlist ids always have 34 characters and begin with "PL", while |
||
64 | * this is not guaranteed it should stay that way until all possible combinations are exhausted. |
||
65 | */ |
||
66 | function is_playlist_id($value) { |
||
69 | |||
70 | /** |
||
71 | * Takes the supplied query and optional page number, determines the correct method to call and then returns its' data. |
||
72 | * |
||
73 | * @param string $query The query value to parse. |
||
74 | * @param int [$page=1] If this is a search query the page number could also be supplied. |
||
75 | * @param int [$offset=0] The number of items already retrieved for the query. |
||
76 | * @return array |
||
77 | */ |
||
78 | function query($query, $page = 1, $offset = 0) { |
||
90 | |||
91 | /** |
||
92 | * Takes the supplied YouTube url and attempts to fetch its' data. |
||
93 | * |
||
94 | * @description At present this method supports the following url patterns: |
||
95 | * |
||
96 | * - http(s)://www.youtube.com/watch?v=[ID] |
||
97 | * - http(s)://youtu.be/[ID] |
||
98 | * - http(s)://www.youtube.com/embed/[ID] |
||
99 | * - http(s)://www.youtube.com/playlist?list=[ID] |
||
100 | * |
||
101 | * @param string $url The url to fetch the data for. |
||
102 | * @return array |
||
103 | */ |
||
104 | function fetch($url) { |
||
135 | |||
136 | /** |
||
137 | * Takes the supplied YouTube playlist id and fetches its' data. |
||
138 | * |
||
139 | * @param string $id The playlist id to fetch. |
||
140 | * @return array( |
||
141 | * "mode" => "playlist", |
||
142 | * "thumbnail" => string, |
||
143 | * "title" => string, |
||
144 | * "description" => string, |
||
145 | * "videos" => array, |
||
146 | * "total" => number |
||
147 | * ) |
||
148 | * @return array( |
||
149 | * "mode" => "error", |
||
150 | * "message" => string |
||
151 | * ) |
||
152 | */ |
||
153 | function fetch_playlist($id) { |
||
209 | |||
210 | /** |
||
211 | * Takes the supplied YouTube video id and fetches its' data. |
||
212 | * |
||
213 | * @param string $id The video id to fetch. |
||
214 | * @return array( |
||
215 | * "mode" => "video", |
||
216 | * "url" => string, |
||
217 | * "thumbnail" => string, |
||
218 | * "title" => string, |
||
219 | * "description" => string |
||
220 | * ) |
||
221 | * @return array( |
||
222 | * "mode" => "error", |
||
223 | * "message" => string |
||
224 | * ) |
||
225 | */ |
||
226 | function fetch_video($id) { |
||
263 | |||
264 | /** |
||
265 | * Takes the supplied query and optional page number and performs a YouTube search. |
||
266 | * |
||
267 | * @param string $query The query to use as a search term. |
||
268 | * @param int [$page=1] The page number to retrieve. |
||
269 | * @param int [$offset=0] The number of items already retrieved for the query. |
||
270 | * @return array( |
||
271 | * "mode" => "search", |
||
272 | * "total" => number, |
||
273 | * "page" => number, |
||
274 | * "offset" => number, |
||
275 | * "nextPage" => number, |
||
276 | * "videos" => array( |
||
277 | * array( |
||
278 | * "provider" => "youtube", |
||
279 | * "id" => string, |
||
280 | * "url" => string, |
||
281 | * "thumbnail" => string, |
||
282 | * "title" => string, |
||
283 | * "description" => string |
||
284 | * ) |
||
285 | * ) |
||
286 | * ) |
||
287 | * @return array( |
||
288 | * "mode" => "error", |
||
289 | * "title" => string, |
||
290 | * "message" => string |
||
291 | * ) |
||
292 | */ |
||
293 | function search($query, $page = 1, $offset = 0) { |
||
352 | |||
353 | } |
||
354 | |||
355 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.