| Total Complexity | 63 |
| Total Lines | 889 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Diagnoser 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.
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 Diagnoser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Diagnoser |
||
| 15 | { |
||
| 16 | const STATUS_OK = 1; |
||
| 17 | const STATUS_WARNING = 2; |
||
| 18 | const STATUS_ERROR = 3; |
||
| 19 | const STATUS_INFORMATION = 4; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Contructor. |
||
| 23 | */ |
||
| 24 | public function __construct() |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Show html table. |
||
| 30 | */ |
||
| 31 | public function show_html() |
||
| 32 | { |
||
| 33 | $sections = [ |
||
| 34 | 'chamilo' => [ |
||
| 35 | 'lang' => 'Chamilo', |
||
| 36 | 'info' => 'State of Chamilo requirements', |
||
| 37 | ], |
||
| 38 | 'php' => [ |
||
| 39 | 'lang' => 'PHP', |
||
| 40 | 'info' => 'State of PHP settings on the server', |
||
| 41 | ], |
||
| 42 | 'database' => [ |
||
| 43 | 'lang' => 'Database', |
||
| 44 | 'info' => 'Configuration settings of the database server. To check the database consistency after an upgrade, if you have access to the command line, you can use "php bin/doctrine.php orm:schema-tool:update --dump-sql". This will print a list of database changes that should be applied to your system in order to get the right structure. Index name changes can be ignored. Use "--force" instead of "--dump" to try and execute them in order.', |
||
| 45 | ], |
||
| 46 | 'webserver' => [ |
||
| 47 | 'lang' => get_lang('WebServer'), |
||
| 48 | 'info' => 'Information about your webserver\'s configuration ', |
||
| 49 | ], |
||
| 50 | 'paths' => [ |
||
| 51 | 'lang' => 'Paths', |
||
| 52 | 'info' => 'The following paths are called by their constant throughout Chamilo\'s code using the api_get_path() function. Here is a list of these paths and what they would be translated to on this portal.', |
||
| 53 | ], |
||
| 54 | ]; |
||
| 55 | $currentSection = isset($_GET['section']) ? $_GET['section'] : ''; |
||
| 56 | if (!in_array(trim($currentSection), array_keys($sections))) { |
||
| 57 | $currentSection = 'chamilo'; |
||
| 58 | } |
||
| 59 | |||
| 60 | $html = '<div class="tabbable"><ul class="nav nav-tabs">'; |
||
| 61 | |||
| 62 | foreach ($sections as $section => $details) { |
||
| 63 | if ($currentSection === $section) { |
||
| 64 | $html .= '<li class="active">'; |
||
| 65 | } else { |
||
| 66 | $html .= '<li>'; |
||
| 67 | } |
||
| 68 | $params['section'] = $section; |
||
| 69 | $html .= '<a href="system_status.php?section='.$section.'">'.$details['lang'].'</a></li>'; |
||
| 70 | } |
||
| 71 | |||
| 72 | $html .= '</ul><div class="tab-pane">'; |
||
| 73 | |||
| 74 | $data = call_user_func([$this, 'get_'.$currentSection.'_data']); |
||
| 75 | echo $html; |
||
| 76 | |||
| 77 | if ($currentSection != 'paths') { |
||
| 78 | echo '<br />'; |
||
| 79 | echo Display::return_message($sections[$currentSection]['info'], 'normal'); |
||
| 80 | |||
| 81 | $table = new SortableTableFromArray($data, 1, 100); |
||
| 82 | $table->set_header(0, '', false); |
||
| 83 | $table->set_header(1, get_lang('Section'), false); |
||
| 84 | $table->set_header(2, get_lang('Setting'), false); |
||
| 85 | $table->set_header(3, get_lang('Current'), false); |
||
| 86 | $table->set_header(4, get_lang('Expected'), false); |
||
| 87 | $table->set_header(5, get_lang('Comment'), false); |
||
| 88 | |||
| 89 | $table->display(); |
||
| 90 | } else { |
||
| 91 | echo '<br />'; |
||
| 92 | echo Display::return_message($sections[$currentSection]['info'], 'normal'); |
||
| 93 | |||
| 94 | $headers = $data['headers']; |
||
| 95 | $results = $data['data']; |
||
| 96 | $table = new HTML_Table(['class' => 'data_table']); |
||
| 97 | |||
| 98 | $column = 0; |
||
| 99 | foreach ($headers as $header) { |
||
| 100 | $table->setHeaderContents(0, $column, $header); |
||
| 101 | $column++; |
||
| 102 | } |
||
| 103 | $row = 1; |
||
| 104 | foreach ($results as $index => $rowData) { |
||
| 105 | $table->setCellContents( |
||
| 106 | $row, |
||
| 107 | 0, |
||
| 108 | $rowData |
||
| 109 | ); |
||
| 110 | $table->setCellContents( |
||
| 111 | $row, |
||
| 112 | 1, |
||
| 113 | $index |
||
| 114 | ); |
||
| 115 | $row++; |
||
| 116 | } |
||
| 117 | |||
| 118 | $table->display(); |
||
| 119 | } |
||
| 120 | echo '</div></div>'; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | public function get_paths_data() |
||
| 136 | ]; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Functions to get the data for the chamilo diagnostics. |
||
| 141 | * |
||
| 142 | * @return array of data |
||
| 143 | */ |
||
| 144 | public function get_chamilo_data() |
||
| 145 | { |
||
| 146 | $array = []; |
||
| 147 | $writable_folders = [ |
||
| 148 | api_get_path(SYS_APP_PATH).'cache', |
||
| 149 | api_get_path(SYS_COURSE_PATH), |
||
| 150 | api_get_path(SYS_APP_PATH).'upload/users/', |
||
| 151 | ]; |
||
| 152 | foreach ($writable_folders as $index => $folder) { |
||
| 153 | $writable = is_writable($folder); |
||
| 154 | $status = $writable ? self::STATUS_OK : self::STATUS_ERROR; |
||
| 155 | $array[] = $this->build_setting( |
||
| 156 | $status, |
||
| 157 | '[FILES]', |
||
| 158 | get_lang('IsWritable').': '.$folder, |
||
| 159 | 'http://be2.php.net/manual/en/function.is-writable.php', |
||
| 160 | $writable, |
||
| 161 | 1, |
||
| 162 | 'yes_no', |
||
| 163 | get_lang('DirectoryMustBeWritable') |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | |||
| 167 | $exists = file_exists(api_get_path(SYS_CODE_PATH).'install'); |
||
| 168 | $status = $exists ? self::STATUS_WARNING : self::STATUS_OK; |
||
| 169 | $array[] = $this->build_setting( |
||
| 170 | $status, |
||
| 171 | '[FILES]', |
||
| 172 | get_lang('DirectoryExists').': /install', |
||
| 173 | 'http://be2.php.net/file_exists', |
||
| 174 | $exists, |
||
| 175 | 0, |
||
| 176 | 'yes_no', |
||
| 177 | get_lang('DirectoryShouldBeRemoved') |
||
| 178 | ); |
||
| 179 | |||
| 180 | $app_version = api_get_setting('chamilo_database_version'); |
||
| 181 | $array[] = $this->build_setting( |
||
| 182 | self::STATUS_INFORMATION, |
||
| 183 | '[DB]', |
||
| 184 | 'chamilo_database_version', |
||
| 185 | '#', |
||
| 186 | $app_version, |
||
| 187 | 0, |
||
| 188 | null, |
||
| 189 | 'Chamilo DB version' |
||
| 190 | ); |
||
| 191 | |||
| 192 | $access_url_id = api_get_current_access_url_id(); |
||
| 193 | |||
| 194 | if ($access_url_id === 1) { |
||
| 195 | $size = '-'; |
||
| 196 | global $_configuration; |
||
| 197 | $message2 = ''; |
||
| 198 | if ($access_url_id === 1) { |
||
| 199 | if (api_is_windows_os()) { |
||
| 200 | $message2 .= get_lang('SpaceUsedOnSystemCannotBeMeasuredOnWindows'); |
||
| 201 | } else { |
||
| 202 | $dir = api_get_path(SYS_PATH); |
||
| 203 | $du = exec('du -sh '.$dir, $err); |
||
| 204 | list($size, $none) = explode("\t", $du); |
||
| 205 | unset($none); |
||
| 206 | $limit = 0; |
||
| 207 | if (isset($_configuration[$access_url_id])) { |
||
| 208 | if (isset($_configuration[$access_url_id]['hosting_limit_disk_space'])) { |
||
| 209 | $limit = $_configuration[$access_url_id]['hosting_limit_disk_space']; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | $message2 .= sprintf(get_lang('TotalSpaceUsedByPortalXLimitIsYMB'), $size, $limit); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | $array[] = $this->build_setting( |
||
| 217 | self::STATUS_OK, |
||
| 218 | '[FILES]', |
||
| 219 | 'hosting_limit_disk_space', |
||
| 220 | '#', |
||
| 221 | $size, |
||
| 222 | 0, |
||
| 223 | null, |
||
| 224 | $message2 |
||
| 225 | ); |
||
| 226 | } |
||
| 227 | $new_version = '-'; |
||
| 228 | $new_version_status = ''; |
||
| 229 | $file = api_get_path(SYS_CODE_PATH).'install/version.php'; |
||
| 230 | if (is_file($file)) { |
||
| 231 | @include($file); |
||
| 232 | } |
||
| 233 | $array[] = $this->build_setting( |
||
| 234 | self::STATUS_INFORMATION, |
||
| 235 | '[CONFIG]', |
||
| 236 | get_lang('VersionFromVersionFile'), |
||
| 237 | '#', |
||
| 238 | $new_version.' '.$new_version_status, |
||
| 239 | '-', |
||
| 240 | null, |
||
| 241 | get_lang('TheVersionFromTheVersionFileIsUpdatedWithEachVersionIfMainInstallDirectoryIsPresent') |
||
| 242 | ); |
||
| 243 | $array[] = $this->build_setting( |
||
| 244 | self::STATUS_INFORMATION, |
||
| 245 | '[CONFIG]', |
||
| 246 | get_lang('VersionFromConfigFile'), |
||
| 247 | '#', |
||
| 248 | api_get_configuration_value('system_version'), |
||
| 249 | $new_version, |
||
| 250 | null, |
||
| 251 | get_lang('TheVersionFromTheConfigurationFileShowsOnTheAdminPageButHasToBeChangedManuallyOnUpgrade') |
||
| 252 | ); |
||
| 253 | |||
| 254 | return $array; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Functions to get the data for the php diagnostics. |
||
| 259 | * |
||
| 260 | * @return array of data |
||
| 261 | */ |
||
| 262 | public function get_php_data() |
||
| 622 | } |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Functions to get the data for the mysql diagnostics. |
||
| 626 | * |
||
| 627 | * @return array of data |
||
| 628 | */ |
||
| 629 | public function get_database_data() |
||
| 630 | { |
||
| 631 | $array = []; |
||
| 632 | $em = Database::getManager(); |
||
| 633 | $connection = $em->getConnection(); |
||
| 634 | $host = $connection->getHost(); |
||
| 635 | $db = $connection->getDatabase(); |
||
| 636 | $port = $connection->getPort(); |
||
| 637 | $driver = $connection->getDriver()->getName(); |
||
| 638 | |||
| 639 | $array[] = $this->build_setting( |
||
| 640 | self::STATUS_INFORMATION, |
||
| 641 | '[Database]', |
||
| 642 | 'driver', |
||
| 643 | '', |
||
| 644 | $driver, |
||
| 645 | null, |
||
| 646 | null, |
||
| 647 | get_lang('Driver') |
||
| 648 | ); |
||
| 649 | |||
| 650 | $array[] = $this->build_setting( |
||
| 651 | self::STATUS_INFORMATION, |
||
| 652 | '[Database]', |
||
| 653 | 'host', |
||
| 654 | '', |
||
| 655 | $host, |
||
| 656 | null, |
||
| 657 | null, |
||
| 658 | get_lang('MysqlHostInfo') |
||
| 659 | ); |
||
| 660 | |||
| 661 | $array[] = $this->build_setting( |
||
| 662 | self::STATUS_INFORMATION, |
||
| 663 | '[Database]', |
||
| 664 | 'port', |
||
| 665 | '', |
||
| 666 | $port, |
||
| 667 | null, |
||
| 668 | null, |
||
| 669 | get_lang('Port') |
||
| 670 | ); |
||
| 671 | |||
| 672 | $array[] = $this->build_setting( |
||
| 673 | self::STATUS_INFORMATION, |
||
| 674 | '[Database]', |
||
| 675 | 'Database name', |
||
| 676 | '', |
||
| 677 | $db, |
||
| 678 | null, |
||
| 679 | null, |
||
| 680 | get_lang('Name') |
||
| 681 | ); |
||
| 682 | |||
| 683 | return $array; |
||
| 684 | } |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Functions to get the data for the webserver diagnostics. |
||
| 688 | * |
||
| 689 | * @return array of data |
||
| 690 | */ |
||
| 691 | public function get_webserver_data() |
||
| 692 | { |
||
| 693 | $array = []; |
||
| 694 | |||
| 695 | $array[] = $this->build_setting( |
||
| 696 | self::STATUS_INFORMATION, |
||
| 697 | '[SERVER]', |
||
| 698 | '$_SERVER["SERVER_NAME"]', |
||
| 699 | 'http://be.php.net/reserved.variables.server', |
||
| 700 | $_SERVER["SERVER_NAME"], |
||
| 701 | null, |
||
| 702 | null, |
||
| 703 | get_lang('ServerNameInfo') |
||
| 704 | ); |
||
| 705 | $array[] = $this->build_setting( |
||
| 706 | self::STATUS_INFORMATION, |
||
| 707 | '[SERVER]', |
||
| 708 | '$_SERVER["SERVER_ADDR"]', |
||
| 709 | 'http://be.php.net/reserved.variables.server', |
||
| 710 | $_SERVER["SERVER_ADDR"], |
||
| 711 | null, |
||
| 712 | null, |
||
| 713 | get_lang('ServerAddessInfo') |
||
| 714 | ); |
||
| 715 | $array[] = $this->build_setting( |
||
| 716 | self::STATUS_INFORMATION, |
||
| 717 | '[SERVER]', |
||
| 718 | '$_SERVER["SERVER_PORT"]', |
||
| 719 | 'http://be.php.net/reserved.variables.server', |
||
| 720 | $_SERVER["SERVER_PORT"], |
||
| 721 | null, |
||
| 722 | null, |
||
| 723 | get_lang('ServerPortInfo') |
||
| 724 | ); |
||
| 725 | $array[] = $this->build_setting( |
||
| 726 | self::STATUS_INFORMATION, |
||
| 727 | '[SERVER]', |
||
| 728 | '$_SERVER["SERVER_SOFTWARE"]', |
||
| 729 | 'http://be.php.net/reserved.variables.server', |
||
| 730 | $_SERVER["SERVER_SOFTWARE"], |
||
| 731 | null, |
||
| 732 | null, |
||
| 733 | get_lang('ServerSoftwareInfo') |
||
| 734 | ); |
||
| 735 | $array[] = $this->build_setting( |
||
| 736 | self::STATUS_INFORMATION, |
||
| 737 | '[SERVER]', |
||
| 738 | '$_SERVER["REMOTE_ADDR"]', |
||
| 739 | 'http://be.php.net/reserved.variables.server', |
||
| 740 | $_SERVER["REMOTE_ADDR"], |
||
| 741 | null, |
||
| 742 | null, |
||
| 743 | get_lang('ServerRemoteInfo') |
||
| 744 | ); |
||
| 745 | $array[] = $this->build_setting( |
||
| 746 | self::STATUS_INFORMATION, |
||
| 747 | '[SERVER]', |
||
| 748 | '$_SERVER["HTTP_USER_AGENT"]', |
||
| 749 | 'http://be.php.net/reserved.variables.server', |
||
| 750 | $_SERVER["HTTP_USER_AGENT"], |
||
| 751 | null, |
||
| 752 | null, |
||
| 753 | get_lang('ServerUserAgentInfo') |
||
| 754 | ); |
||
| 755 | $array[] = $this->build_setting( |
||
| 756 | self::STATUS_INFORMATION, |
||
| 757 | '[SERVER]', |
||
| 758 | '$_SERVER["SERVER_PROTOCOL"]', |
||
| 759 | 'http://be.php.net/reserved.variables.server', |
||
| 760 | $_SERVER["SERVER_PROTOCOL"], |
||
| 761 | null, |
||
| 762 | null, |
||
| 763 | get_lang('ServerProtocolInfo') |
||
| 764 | ); |
||
| 765 | $array[] = $this->build_setting( |
||
| 766 | self::STATUS_INFORMATION, |
||
| 767 | '[SERVER]', |
||
| 768 | 'php_uname()', |
||
| 769 | 'http://be2.php.net/php_uname', |
||
| 770 | php_uname(), |
||
| 771 | null, |
||
| 772 | null, |
||
| 773 | get_lang('UnameInfo') |
||
| 774 | ); |
||
| 775 | $array[] = $this->build_setting( |
||
| 776 | self::STATUS_INFORMATION, |
||
| 777 | '[SERVER]', |
||
| 778 | '$_SERVER["HTTP_X_FORWARDED_FOR"]', |
||
| 779 | 'http://be.php.net/reserved.variables.server', |
||
| 780 | (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : ''), |
||
| 781 | null, |
||
| 782 | null, |
||
| 783 | get_lang('ServerXForwardedForInfo') |
||
| 784 | ); |
||
| 785 | |||
| 786 | return $array; |
||
| 787 | } |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Additional functions needed for fast integration. |
||
| 791 | * @param int $status Status constant defining which icon to use to illustrate the info |
||
| 792 | * @param string $section The name of the section this setting is included in |
||
| 793 | * @param string $title The name of the setting (usually a translated string) |
||
| 794 | * @param string $url A URL to point the user to regarding this setting, or '#' otherwise |
||
| 795 | * @param mixed $current_value The current value for this setting |
||
| 796 | * @param mixed $expected_value The expected value for this setting |
||
| 797 | * @param string $formatter If this setting is expressed in some kind of format, which format to use |
||
| 798 | * @param string $comment A translated string explaining what this setting represents |
||
| 799 | * @return array A list of elements to show in an array's row |
||
| 800 | */ |
||
| 801 | public function build_setting( |
||
| 802 | $status, |
||
| 803 | $section, |
||
| 804 | $title, |
||
| 805 | $url, |
||
| 806 | $current_value, |
||
| 807 | $expected_value, |
||
| 808 | $formatter, |
||
| 809 | $comment |
||
| 810 | ) { |
||
| 811 | switch ($status) { |
||
| 812 | case self::STATUS_OK: |
||
| 813 | $img = 'bullet_green.png'; |
||
| 814 | break; |
||
| 815 | case self::STATUS_WARNING: |
||
| 816 | $img = 'bullet_orange.png'; |
||
| 817 | break; |
||
| 818 | case self::STATUS_ERROR: |
||
| 819 | $img = 'bullet_red.png'; |
||
| 820 | break; |
||
| 821 | case self::STATUS_INFORMATION: |
||
| 822 | default: |
||
| 823 | $img = 'bullet_blue.png'; |
||
| 824 | break; |
||
| 825 | } |
||
| 826 | |||
| 827 | $image = Display::return_icon($img, $status); |
||
| 828 | $url = $this->get_link($title, $url); |
||
| 829 | |||
| 830 | $formatted_current_value = $current_value; |
||
| 831 | $formatted_expected_value = $expected_value; |
||
| 832 | |||
| 833 | if ($formatter) { |
||
| 834 | if (method_exists($this, 'format_'.$formatter)) { |
||
| 835 | $formatted_current_value = call_user_func([$this, 'format_'.$formatter], $current_value); |
||
| 836 | $formatted_expected_value = call_user_func([$this, 'format_'.$formatter], $expected_value); |
||
| 837 | } |
||
| 838 | } |
||
| 839 | |||
| 840 | return [$image, $section, $url, $formatted_current_value, $formatted_expected_value, $comment]; |
||
| 841 | } |
||
| 842 | |||
| 843 | /** |
||
| 844 | * Create a link with a url and a title. |
||
| 845 | * |
||
| 846 | * @param $title |
||
| 847 | * @param $url |
||
| 848 | * |
||
| 849 | * @return string the url |
||
| 850 | */ |
||
| 851 | public function get_link($title, $url) |
||
| 854 | } |
||
| 855 | |||
| 856 | /** |
||
| 857 | * @param int $value |
||
| 858 | * |
||
| 859 | * @return string |
||
| 860 | */ |
||
| 861 | public function format_yes_no_optional($value) |
||
| 877 | } |
||
| 878 | |||
| 879 | /** |
||
| 880 | * @param $value |
||
| 881 | * |
||
| 882 | * @return string |
||
| 883 | */ |
||
| 884 | public function format_yes_no($value) |
||
| 885 | { |
||
| 886 | return $value ? get_lang('Yes') : get_lang('No'); |
||
| 887 | } |
||
| 888 | |||
| 889 | /** |
||
| 890 | * @param int $value |
||
| 891 | * |
||
| 892 | * @return string |
||
| 893 | */ |
||
| 894 | public function format_on_off($value) |
||
| 903 | } |
||
| 904 | } |
||
| 905 |