Complex classes like Design 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 Design, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | class Design |
||
47 | { |
||
48 | const CACHE_GROUP = 'str/design'; |
||
49 | const CACHE_AVATAR_GROUP = 'str/design/avatar/'; // We put a slash for after creating a directory for each username |
||
50 | const CACHE_AVATAR_LIFETIME = 3600; |
||
51 | |||
52 | const NONE_FLAG_FILENAME = 'none.gif'; |
||
53 | const FLAG_ICON_EXT = '.gif'; |
||
54 | const AVATAR_IMG_EXT = '.svg'; |
||
55 | |||
56 | const SUCCESS_TYPE = 'success'; |
||
57 | const ERROR_TYPE = 'error'; |
||
58 | const WARNING_TYPE = 'warning'; |
||
59 | const INFO_TYPE = 'info'; |
||
60 | |||
61 | const MESSAGE_TYPES = [ |
||
62 | self::SUCCESS_TYPE, |
||
63 | self::ERROR_TYPE, |
||
64 | self::WARNING_TYPE, |
||
65 | self::INFO_TYPE |
||
66 | ]; |
||
67 | |||
68 | const FLASH_MSG = 'flash_msg'; |
||
69 | const FLASH_TYPE = 'flash_type'; |
||
70 | |||
71 | const DEFAULT_REDIRECTION_DELAY = 3; // In secs |
||
72 | const MAX_MESSAGE_LENGTH_SHOWN = 300; |
||
73 | const MAX_IP_LENGTH_SHOWN = 15; |
||
74 | |||
75 | /** @var bool */ |
||
76 | protected $bIsDiv = false; |
||
77 | |||
78 | /** @var Str */ |
||
79 | protected $oStr; |
||
80 | |||
81 | /** @var Session */ |
||
82 | protected $oSession; |
||
83 | |||
84 | /** @var HttpRequest */ |
||
85 | protected $oHttpRequest; |
||
86 | |||
87 | /** @var array */ |
||
88 | protected $aCssDir = []; |
||
89 | |||
90 | /** @var array */ |
||
91 | protected $aCssFiles = []; |
||
92 | |||
93 | /** @var array */ |
||
94 | protected $aCssMedia = []; |
||
95 | |||
96 | /** @var array */ |
||
97 | protected $aJsDir = []; |
||
98 | |||
99 | /** @var array */ |
||
100 | protected $aJsFiles = []; |
||
101 | |||
102 | /** @var array */ |
||
103 | protected $aMessages = []; |
||
104 | |||
105 | /** @var array */ |
||
106 | protected $aErrors = []; |
||
107 | |||
108 | public function __construct() |
||
115 | |||
116 | public function langList() |
||
137 | |||
138 | /** |
||
139 | * For better SEO optimization for multilingual sites. Ref: https://support.google.com/webmasters/answer/189077 |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | public function regionalUrls() |
||
157 | |||
158 | /** |
||
159 | * Set an information message. |
||
160 | * |
||
161 | * @param string $sMsg |
||
162 | * |
||
163 | * @return void |
||
164 | */ |
||
165 | public function setMessage($sMsg) |
||
169 | |||
170 | /** |
||
171 | * Display the information message. |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | public function message() |
||
201 | |||
202 | /** |
||
203 | * Set an error message. |
||
204 | * |
||
205 | * @param string $sErr |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | public function setError($sErr) |
||
213 | |||
214 | /** |
||
215 | * Display the error message. |
||
216 | * |
||
217 | * @return void |
||
218 | */ |
||
219 | public function error() |
||
242 | |||
243 | /** |
||
244 | * Redirect Page using Refresh with Header. |
||
245 | * |
||
246 | * @param string $sUrl If NULL, the URL will be the current page. Default NULL |
||
247 | * @param string $sMsg , Optional, display a message after redirect of the page. |
||
248 | * @param string $sType Type of message: "success", "info", "warning" or "error". Default: "success". |
||
249 | * @param int $iTime Optional, a time. Default: "3" seconds. |
||
250 | * |
||
251 | * @return void |
||
252 | */ |
||
253 | public function setRedirect($sUrl = null, $sMsg = null, $sType = self::SUCCESS_TYPE, $iTime = self::DEFAULT_REDIRECTION_DELAY) |
||
263 | |||
264 | /** |
||
265 | * Get stats from the benchmark. |
||
266 | * |
||
267 | * @return void HTML output. |
||
268 | */ |
||
269 | public function stat() |
||
280 | |||
281 | /** |
||
282 | * Display accurate homepage URL. |
||
283 | * |
||
284 | * @return void The homepage URL output. |
||
285 | */ |
||
286 | public function homePageUrl() |
||
302 | |||
303 | /** |
||
304 | * @param string $sModule |
||
305 | * @param string $sController |
||
306 | * @param string $sAction |
||
307 | * @param null|string $sVars |
||
308 | * @param bool $bClear |
||
309 | * |
||
310 | * @return void |
||
311 | */ |
||
312 | public function url($sModule, $sController, $sAction, $sVars = null, $bClear = true) |
||
318 | |||
319 | /** |
||
320 | * Create a link of to display a popup confirmation for a CRUD action (http://en.wikipedia.org/wiki/Create,_read,_update_and_delete). |
||
321 | * |
||
322 | * @param string $sLabel |
||
323 | * @param string $sMod |
||
324 | * @param string $sCtrl |
||
325 | * @param string $sAct |
||
326 | * @param int|string $mId Content ID |
||
327 | * @param string $sClass Add a CSS class |
||
328 | * |
||
329 | * @return void HTML output. |
||
330 | */ |
||
331 | public function popupLinkConfirm($sLabel, $sMod, $sCtrl, $sAct, $mId, $sClass = null) |
||
345 | |||
346 | /** |
||
347 | * @param string $sCountryCode The Country Code (e.g., US = United States). |
||
348 | * |
||
349 | * @return void Output the Flag Icon Url. |
||
350 | */ |
||
351 | public function getSmallFlagIcon($sCountryCode) |
||
358 | |||
359 | /** |
||
360 | * @return string |
||
361 | */ |
||
362 | final public function softwareComment() |
||
366 | |||
367 | /** |
||
368 | * Provide a "Powered By" link. |
||
369 | * |
||
370 | * @param bool $bLink To include a link to pH7CMS or pH7Framework. |
||
371 | * @param bool $bSoftwareName |
||
372 | * @param bool $bVersion To include the version being used. |
||
373 | * @param bool $bComment HTML comment. |
||
374 | * @param bool $bEmailContext Is it for email content or not. |
||
375 | * |
||
376 | * @return void |
||
377 | */ |
||
378 | final public function link($bLink = true, $bSoftwareName = true, $bVersion = false, $bComment = true, $bEmailContext = false) |
||
401 | |||
402 | /** |
||
403 | * Provide a small "Powered By" link (e.g., for sitemap.xsl.tpl). |
||
404 | * |
||
405 | * @return void |
||
406 | */ |
||
407 | final public function smallLink() |
||
411 | |||
412 | /** |
||
413 | * @return void Output the relevant link based on the client browser's language. |
||
414 | */ |
||
415 | final public function smartLink() |
||
448 | |||
449 | /** |
||
450 | * @param string $sType (js or css). |
||
451 | * @param string $sDir |
||
452 | * @param string $sFiles |
||
453 | * @param string $sCssMedia Only works for CSS files. The CSS Media type (e.g., screen,handheld,tv,projection). Default "all". Leave blank ('' or null) not to use the media attribute. |
||
454 | * |
||
455 | * @return void |
||
456 | */ |
||
457 | public function staticFiles($sType, $sDir, $sFiles, $sCssMedia = 'all') |
||
465 | |||
466 | /** |
||
467 | * @param string $sDir The CSS folder. |
||
468 | * @param string $sFiles The CSS files. |
||
469 | * @param string $sCssMedia CSS Media type (e.g., screen,handheld,tv,projection). Default "all". Leave blank ('' or null) not to use the media attribute. |
||
470 | * |
||
471 | * @return void |
||
472 | */ |
||
473 | public function addCss($sDir, $sFiles, $sCssMedia = 'all') |
||
479 | |||
480 | /** |
||
481 | * @param string $sDir The JavaScript folder. |
||
482 | * @param string $sFiles The JavaScript files. |
||
483 | * |
||
484 | * @return void |
||
485 | */ |
||
486 | public function addJs($sDir, $sFiles) |
||
491 | |||
492 | /** |
||
493 | * @return void |
||
494 | */ |
||
495 | public function css() |
||
503 | |||
504 | /** |
||
505 | * @return void |
||
506 | */ |
||
507 | public function js() |
||
515 | |||
516 | /** |
||
517 | * Set flash message. |
||
518 | * |
||
519 | * @param string $sMessage |
||
520 | * @param string $sType Type of message: "Design::SUCCESS_TYPE", "Design::INFO_TYPE", "Design::WARNING_TYPE" or "Design::ERROR_TYPE" |
||
521 | * |
||
522 | * @return void |
||
523 | */ |
||
524 | public function setFlashMsg($sMessage, $sType = self::SUCCESS_TYPE) |
||
536 | |||
537 | /** |
||
538 | * Flash displays the message defined in the method setFlash. |
||
539 | * |
||
540 | * @return void The message text with CSS layout depending on the type of message. |
||
541 | */ |
||
542 | public function flashMsg() |
||
555 | |||
556 | /** |
||
557 | * Show the user IP address with a link to get the IP information. |
||
558 | * |
||
559 | * @internal If it's an IPv6, show only the beginning, otherwise it would be too long in the template. |
||
560 | * |
||
561 | * @param string $sIp Allows to specify another IP address than the client one. |
||
562 | * @param bool $bPrint Print or Return the HTML code. Default TRUE |
||
563 | * |
||
564 | * @return void|string |
||
565 | */ |
||
566 | public function ip($sIp = null, $bPrint = true) |
||
577 | |||
578 | /** |
||
579 | * Show the geolocation of the user (with link that points to the Country controller). |
||
580 | * |
||
581 | * @param bool $bPrint Print or Return the HTML code. Default TRUE |
||
582 | * |
||
583 | * @return void|string |
||
584 | */ |
||
585 | public function geoIp($bPrint = true) |
||
604 | |||
605 | /** |
||
606 | * Pagination. |
||
607 | * |
||
608 | * @param int $iTotalPages |
||
609 | * @param int $iCurrentPage |
||
610 | * |
||
611 | * @return void The HTML pagination code. |
||
612 | */ |
||
613 | public function pagination($iTotalPages, $iCurrentPage) |
||
617 | |||
618 | /** |
||
619 | * @param string $sUsername |
||
620 | * @param string $sSex |
||
621 | * @param int $iSize |
||
622 | * @param bool $bPrint Print or Return the HTML code. |
||
623 | * |
||
624 | * @return void|string The default 150px avatar URL or the user avatar URL. |
||
625 | */ |
||
626 | public function getUserAvatar($sUsername, $sSex = '', $iSize = null, $bPrint = true) |
||
693 | |||
694 | /** |
||
695 | * Get the user profile link. |
||
696 | * |
||
697 | * @param string $sUsername |
||
698 | * @param bool $bPrint Print or Return the HTML code. |
||
699 | * |
||
700 | * @return void|string The absolute user profile link. |
||
701 | */ |
||
702 | public function getProfileLink($sUsername, $bPrint = true) |
||
714 | |||
715 | /** |
||
716 | * Get the Gravatar URL. |
||
717 | * |
||
718 | * @param string $sEmail The user email address. |
||
719 | * @param string $sType Default image type to show [ 404 | mp | identicon | monsterid | wavatar ] |
||
720 | * @param int $iSize The size of the image. Default: 80 |
||
721 | * @param string $sRating The max image rating allowed. Default: 'g' (for all) |
||
722 | * @param bool $bSecure Display avatar via HTTPS, for example if the site uses HTTPS, you should use this option to not get a warning with most Web browsers. Default: FALSE |
||
723 | * |
||
724 | * @return string The Gravatar Link. |
||
725 | */ |
||
726 | public function getGravatarUrl($sEmail, $sType = 'wavatar', $iSize = 80, $sRating = 'g', $bSecure = false) |
||
733 | |||
734 | /** |
||
735 | * Get favicon from a URL. |
||
736 | * |
||
737 | * @param string $sUrl |
||
738 | * |
||
739 | * @return void The HTML favicon image. |
||
740 | */ |
||
741 | public function favicon($sUrl) |
||
756 | |||
757 | /** |
||
758 | * Like Link. |
||
759 | * |
||
760 | * @param string $sUsername Username of member. |
||
761 | * @param string $sFirstName First name of member. |
||
762 | * @param string $sSex Sex of member. |
||
763 | * @param string $sForceUrlKey Specify a specific URL from the like. Default NULL (current URL). |
||
764 | * |
||
765 | * @return void |
||
766 | */ |
||
767 | public function like($sUsername, $sFirstName, $sSex, $sForceUrlKey = null) |
||
786 | |||
787 | /** |
||
788 | * Add Normal size Social Media Widgets. |
||
789 | * |
||
790 | * @internal AddToAny JS file will be included through 'ph7_static_files' table. |
||
791 | * |
||
792 | * @return void HTML output. |
||
793 | */ |
||
794 | public function likeApi() |
||
810 | |||
811 | /** |
||
812 | * Add Small size Social Media Widgets. |
||
813 | * |
||
814 | * @internal AddToAny JS file will be included through 'ph7_static_files' table. |
||
815 | * |
||
816 | * @return void HTML output. |
||
817 | */ |
||
818 | public function littleLikeApi() |
||
832 | |||
833 | /** |
||
834 | * Generate a Report Link. |
||
835 | * |
||
836 | * @param int $iId |
||
837 | * @param string $sUsername |
||
838 | * @param string $sFirstName |
||
839 | * @param string $sSex |
||
840 | * |
||
841 | * @internal We do not use Url::httpBuildQuery() method for the first condition otherwise the URL is distorted and it doesn't work. |
||
842 | * |
||
843 | * @return void |
||
844 | */ |
||
845 | public function report($iId, $sUsername, $sFirstName, $sSex) |
||
881 | |||
882 | /** |
||
883 | * Generate a Link tag. |
||
884 | * |
||
885 | * @param string $sLink The link. |
||
886 | * @param bool $bNoFollow Set TRUE to set the link "nofollow", FALSE otherwise. Default TRUE |
||
887 | * |
||
888 | * @return void The HTML link tag. |
||
889 | */ |
||
890 | public function urlTag($sLink, $bNoFollow = true) |
||
905 | |||
906 | /** |
||
907 | * Generate a IMG tag. |
||
908 | * |
||
909 | * @param string $sImg The image. |
||
910 | * @param string $sAlt Alternate text. |
||
911 | * @param array $aAttrs Optional. Array containing the "name" and "value" HTML attributes. Default NULL |
||
912 | * |
||
913 | * @return void The HTML image tag. |
||
914 | */ |
||
915 | public function imgTag($sImg, $sAlt, array $aAttrs = null) |
||
925 | |||
926 | /** |
||
927 | * Generate any HTML tag. |
||
928 | * |
||
929 | * @param string $sTag |
||
930 | * @param array $aAttrs Optional. Default NULL |
||
931 | * @param bool $bPair Optional. Default FALSE |
||
932 | * @param string $sText Optional. Add text, available only for pair tag. Default NULL |
||
933 | * |
||
934 | * @return string The custom HTML tag. |
||
935 | */ |
||
936 | public function htmlTag($sTag, array $aAttrs = null, $bPair = false, $sText = null) |
||
948 | |||
949 | public function htmlHeader() |
||
953 | |||
954 | /** |
||
955 | * Useful HTML Header. |
||
956 | * |
||
957 | * @param array $aMeta Default NULL |
||
958 | * @param bool $bLogo Default FALSE |
||
959 | * |
||
960 | * @return void |
||
961 | */ |
||
962 | final public function usefulHtmlHeader(array $aMeta = null, $bLogo = false) |
||
1006 | |||
1007 | public function htmlFooter() |
||
1015 | |||
1016 | /** |
||
1017 | * The XML tag doesn't work in PHP files since it is the same "<?" language tag. |
||
1018 | * So this method can introduce the XML header without causing an error by the PHP interpreter. |
||
1019 | * |
||
1020 | * @return void |
||
1021 | */ |
||
1022 | public function xmlHeader() |
||
1026 | |||
1027 | /** |
||
1028 | * Get an external CSS file. |
||
1029 | * |
||
1030 | * @param string $sFile CSS file. |
||
1031 | * @param string $sCssMedia Only works for CSS files. The CSS Media type (e.g., screen,handheld,tv,projection). Default NULL |
||
1032 | * |
||
1033 | * @return void HTML link tag. |
||
1034 | */ |
||
1035 | public function externalCssFile($sFile, $sCssMedia = null) |
||
1041 | |||
1042 | /** |
||
1043 | * Get an external JS file. |
||
1044 | * |
||
1045 | * @param string $sFile JS file. |
||
1046 | * |
||
1047 | * @return void HTML script tag. |
||
1048 | */ |
||
1049 | public function externalJsFile($sFile) |
||
1053 | } |
||
1054 |
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.