Complex classes like IdpList 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 IdpList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 72 | class IdpList |
||
| 73 | { |
||
| 74 | |||
| 75 | // Set the constants to correspond to your particular set up. |
||
| 76 | /** |
||
| 77 | * @var string DEFAULTIDPFILENAME The full path/filename of the |
||
| 78 | * generated list of IdPs in JSON format |
||
| 79 | */ |
||
| 80 | const DEFAULTIDPFILENAME = '/var/www/html/include/idplist.json'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string DEFAULTINCOMMONFILENAME The full path/filename of the |
||
| 84 | * InCommon metadata XML file. |
||
| 85 | */ |
||
| 86 | const DEFAULTINCOMMONFILENAME = |
||
| 87 | '/var/cache/shibboleth/InCommon-metadata.xml'; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var string TESTIDPFILENAME The fill path/filename of the XML file |
||
| 91 | * containing test IdPs. |
||
| 92 | */ |
||
| 93 | const TESTIDPFILENAME = '/var/www/html/include/testidplist.xml'; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var DOMDocument $idpdom A DOMDocument which holds the list of IdP |
||
| 97 | * entityIDs and their corresponding attributes. |
||
| 98 | */ |
||
| 99 | protected $idpdom = null; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var mixed $idparray An array version of $idpdom. It is used |
||
| 103 | * primarily since searching an array is faster than xpath query. |
||
| 104 | */ |
||
| 105 | protected $idparray = null; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var string $idpfilename The name of the IdP list in JSON format. |
||
| 109 | * Defaults to DEFAULTIDPFILENAME. |
||
| 110 | */ |
||
| 111 | protected $idpfilename; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var string $incommonfilename The name of the InCommon metadata XML |
||
| 115 | * file. Defaults to DEFAULTINCOMMONFILENAME. |
||
| 116 | */ |
||
| 117 | protected $incommonfilename; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * __construct |
||
| 121 | * |
||
| 122 | * Default constructor. This method first attempts to read in an |
||
| 123 | * existing idplist from a file and store it in the idparray / |
||
| 124 | * idpdom. If a valid idplist file cannot be read and |
||
| 125 | * $createfile is true, neW idparray / idpdom is created and |
||
| 126 | * written to file. |
||
| 127 | * |
||
| 128 | * @param string $idpfilename (Optional) The name of the idplist file to |
||
| 129 | * read/write. Defaults to DEFAULTIDPFILENAME. |
||
| 130 | * @param string $incommonfilename (Optional) The name of the InCommon |
||
| 131 | * metadata file to read. Defaults to DEFAULTINCOMMONFILENAME. |
||
| 132 | * @param bool $createfile (Optional) Create idplist file if it doesn't |
||
| 133 | * exist? Defaults to true. |
||
| 134 | * @param string $filetype (Optional) The type of file to read/write, |
||
| 135 | * one of 'xml' or 'json'. Defaults to 'json'. |
||
| 136 | */ |
||
| 137 | public function __construct( |
||
| 151 | |||
| 152 | /** |
||
| 153 | * read |
||
| 154 | * |
||
| 155 | * This reads in the idplixt file based on the input filetype. |
||
| 156 | * Defaults to reading in a JSON file. |
||
| 157 | * |
||
| 158 | * @param string $filetype (Optional) Type type of file to read, either |
||
| 159 | * 'xml' or 'json'. Defaults to 'json'. |
||
| 160 | * @return bool True if the idplist was read from file. False otherwise. |
||
| 161 | */ |
||
| 162 | public function read($filetype = 'json') |
||
| 170 | |||
| 171 | /** |
||
| 172 | * readXML |
||
| 173 | * |
||
| 174 | * This method attempts to read in an existing idplist XML file and |
||
| 175 | * store its contents in the class $idpdom DOMDocument. It also |
||
| 176 | * converts the $idpdom to the internal $idparray if not already |
||
| 177 | * set. |
||
| 178 | * |
||
| 179 | * @return bool True if the idplist file was read in correctly. |
||
| 180 | * False otherwise. |
||
| 181 | */ |
||
| 182 | public function readXML() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * readJSON |
||
| 206 | * |
||
| 207 | * This method attempts to read in an existing idplist file |
||
| 208 | * (containing JSON) and store its contents in the class $idparray. |
||
| 209 | * Note that this does not update the internal $idpdom. |
||
| 210 | * |
||
| 211 | * @return bool True if the idplist file was read in correctly. |
||
| 212 | * False otherwise. |
||
| 213 | */ |
||
| 214 | public function readJSON() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * write |
||
| 232 | * |
||
| 233 | * This writes out the idplixt file based on the input filetype. |
||
| 234 | * Defaults to writing a JSON file. |
||
| 235 | * |
||
| 236 | * @param string $filetype (Optional) Type type of file to write, either |
||
| 237 | * 'xml' or 'json'. Defaults to 'json'. |
||
| 238 | * @return bool True if the idplist was written to file. False |
||
| 239 | * otherwise. |
||
| 240 | */ |
||
| 241 | public function write($filetype = 'json') |
||
| 249 | |||
| 250 | /** |
||
| 251 | * writeXML |
||
| 252 | * |
||
| 253 | * This method writes the class $idpdom to an XML file. It does |
||
| 254 | * this by first writing to a temporary file in /tmp, then renaming |
||
| 255 | * the temp file to the final idplist XML filename. Note that if |
||
| 256 | * the internal $idpdom does not exist, it attempts to first |
||
| 257 | * convert the internal $idparray to DOM and then write it. |
||
| 258 | * |
||
| 259 | * @return bool True if the idpdom was written to the idplist XML |
||
| 260 | * file. False otherwise. |
||
| 261 | */ |
||
| 262 | public function writeXML() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * writeJSON |
||
| 290 | * |
||
| 291 | * This method writes the class $idparray to a JSON file |
||
| 292 | * It does this by first writing to a temporary file in /tmp, |
||
| 293 | * then renaming the temp file to the final idplist JSON filename. |
||
| 294 | * |
||
| 295 | * @return bool True if the idparray was written to the idplist |
||
| 296 | * JSON file. False otherwise. |
||
| 297 | */ |
||
| 298 | public function writeJSON() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * addNode |
||
| 320 | * |
||
| 321 | * This is a convenience method used by create() to add a new |
||
| 322 | * child node (such as 'Organization_Name') to a parent idp node. |
||
| 323 | * It also adds elements to the internal $idparray, thus creating |
||
| 324 | * the internal idparray at the same time as the idpdom. |
||
| 325 | * |
||
| 326 | * @param \DOMDocument $dom A DOMDocument object |
||
| 327 | * @param \DOMElement $idpnode A pointer to a parent <idp> DOMElement |
||
| 328 | * @param string $nodename The name of the new child node DOMElement |
||
| 329 | * @param string $nodevalue The value of the new child node DOMElement |
||
| 330 | */ |
||
| 331 | private function addNode($dom, $idpnode, $nodename, $nodevalue) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * sortDOM |
||
| 346 | * |
||
| 347 | * This method is called by create() to sort the newly created |
||
| 348 | * DOMDocument <idp> nodes by Display_Name. It uses an XSL |
||
| 349 | * transformation to do the work. A new DOMDocument is created |
||
| 350 | * and returned. |
||
| 351 | * |
||
| 352 | * @param DOMDocument $dom A DOMDocument to be sorted by Display_Name |
||
| 353 | * @return DOMDocument A new DOMDocument with the <idp> elements sorted by |
||
| 354 | * Display_Name. |
||
| 355 | */ |
||
| 356 | private function sortDOM($dom) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * create |
||
| 384 | * |
||
| 385 | * This method is used to populate the class $idpdom DOMDocument |
||
| 386 | * using information from the InCommon metadata file. Note that |
||
| 387 | * method updates $idpdom and $idparray. If you want to save either |
||
| 388 | * to a file, be sure to call write() afterwards. |
||
| 389 | * |
||
| 390 | * @return bool True upon successful extraction of IdP information |
||
| 391 | * from the InCommon metadata file into the class |
||
| 392 | * $idpdom DOMDocument. False otherwise. |
||
| 393 | */ |
||
| 394 | public function create() |
||
| 791 | |||
| 792 | /** |
||
| 793 | * getFilename |
||
| 794 | * |
||
| 795 | * This function returns a string of the full path of the IdP list |
||
| 796 | * filename. See also setFilename(). |
||
| 797 | * |
||
| 798 | * @return string The IdP list filename. |
||
| 799 | */ |
||
| 800 | public function getFilename() |
||
| 804 | |||
| 805 | /** |
||
| 806 | * setFilename |
||
| 807 | * |
||
| 808 | * This function sets the string of the full path of the IdP list |
||
| 809 | * filename. See also getFilename(). |
||
| 810 | * |
||
| 811 | * @param string $filename he new name of the IdP list filename. |
||
| 812 | */ |
||
| 813 | public function setFilename($filename) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * getInCommonFilename |
||
| 820 | * |
||
| 821 | * This function returns a string of the full path of the InCommon |
||
| 822 | * metadata filename. See also setInCommonFilename(). |
||
| 823 | * |
||
| 824 | * @return string The InCommon metadata filename. |
||
| 825 | */ |
||
| 826 | public function getInCommonFilename() |
||
| 830 | |||
| 831 | /** |
||
| 832 | * setInCommonFilename |
||
| 833 | * |
||
| 834 | * This function sets the string of the full path of the InCommon |
||
| 835 | * metadata filename. See also getInCommonFilename(). |
||
| 836 | * |
||
| 837 | * @param string $filename The new name of the InCommon metadata filename. |
||
| 838 | */ |
||
| 839 | public function setInCommonFilename($filename) |
||
| 843 | |||
| 844 | /** |
||
| 845 | * getEntityIDs |
||
| 846 | * |
||
| 847 | * This method returns the entityIDs of the idplist as an array. |
||
| 848 | * |
||
| 849 | * @return array An array of the entityIDs |
||
| 850 | */ |
||
| 851 | public function getEntityIDs() |
||
| 859 | |||
| 860 | /** |
||
| 861 | * getOrganizationName |
||
| 862 | * |
||
| 863 | * This function returns the Organization_Name of the selected |
||
| 864 | * $entityID. |
||
| 865 | * |
||
| 866 | * @param string $entityID The entityID to search for |
||
| 867 | * @return string The Organization_Name for the $entityID. Return |
||
| 868 | * string is empty if no matching $entityID found. |
||
| 869 | */ |
||
| 870 | public function getOrganizationName($entityID) |
||
| 878 | |||
| 879 | /** |
||
| 880 | * getDisplayName |
||
| 881 | * |
||
| 882 | * This function returns the Display_Name of the selected |
||
| 883 | * $entityID. |
||
| 884 | * |
||
| 885 | * @param string $entityID The entityID to search for |
||
| 886 | * @return string The Display_Name for the $entityID. Return |
||
| 887 | * string is empty if no matching $entityID found. |
||
| 888 | */ |
||
| 889 | public function getDisplayName($entityID) |
||
| 897 | |||
| 898 | /** |
||
| 899 | * getLogout |
||
| 900 | * |
||
| 901 | * This function returns the Logout URL of the selected $entityID. |
||
| 902 | * |
||
| 903 | * @param string $entityID The entityID to search for |
||
| 904 | * @return string The Logout URLfor the $entityID. Return |
||
| 905 | * string is empty if no matching $entityID found. |
||
| 906 | */ |
||
| 907 | public function getLogout($entityID) |
||
| 915 | |||
| 916 | /** |
||
| 917 | * entityIDExists |
||
| 918 | * |
||
| 919 | * This function searchs for the given idp entityID. |
||
| 920 | * |
||
| 921 | * @param string $entityID The entityID to search for |
||
| 922 | * @return bool True if the given entityID is found. False otherwise. |
||
| 923 | */ |
||
| 924 | public function entityIDExists($entityID) |
||
| 928 | |||
| 929 | /** |
||
| 930 | * exists |
||
| 931 | * |
||
| 932 | * This is simply a convenience function for entityIDExists. |
||
| 933 | * |
||
| 934 | * @param string $entityID The enityID to search for |
||
| 935 | * @return bool True if the given entityID is found. False otherwise. |
||
| 936 | */ |
||
| 937 | public function exists($entityID) |
||
| 941 | |||
| 942 | /** |
||
| 943 | * isAttributeSet |
||
| 944 | * |
||
| 945 | * This function checks if the passed-in $attr is set to '1' for |
||
| 946 | * the entityID, and returns true if so. |
||
| 947 | * |
||
| 948 | * @param string $entityID The enityID to search for. |
||
| 949 | * @param string $attr The attribute in question. |
||
| 950 | * @return bool True if the given attribute is '1' for the entityID. |
||
| 951 | * False otherwise. |
||
| 952 | */ |
||
| 953 | public function isAttributeSet($entityID, $attr) |
||
| 958 | |||
| 959 | /** |
||
| 960 | * isWhitelisted |
||
| 961 | * |
||
| 962 | * This method searches for the given entityID and checks if the |
||
| 963 | *'Whitelisted' entry has been set to '1'. |
||
| 964 | * |
||
| 965 | * @param string $entityID The enityID to search for |
||
| 966 | * @return bool True if the given entityID is marked 'Whitelisted'. |
||
| 967 | * False otherwise. |
||
| 968 | */ |
||
| 969 | public function isWhitelisted($entityID) |
||
| 973 | |||
| 974 | /** |
||
| 975 | * isSilver |
||
| 976 | * |
||
| 977 | * This method searches for the given entityID and checks if the |
||
| 978 | *'Silver' entry has been set to '1'. |
||
| 979 | * |
||
| 980 | * @param string $entityID The enityID to search for |
||
| 981 | * @return bool True if the given entityID is certified 'Silver'. |
||
| 982 | * False otherwise. |
||
| 983 | */ |
||
| 984 | public function isSilver($entityID) |
||
| 988 | |||
| 989 | /** |
||
| 990 | * isBronze |
||
| 991 | * |
||
| 992 | * This method searches for the given entityID and checks if the |
||
| 993 | *'Bronze' entry has been set to '1'. |
||
| 994 | * |
||
| 995 | * @param string $entityID The enityID to search for |
||
| 996 | * @return bool True if the given entityID is certified 'Bronze'. |
||
| 997 | * False otherwise. |
||
| 998 | */ |
||
| 999 | public function isBronze($entityID) |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * isRandS |
||
| 1006 | * |
||
| 1007 | * This method searches for the given entityID and checks if the |
||
| 1008 | *'RandS' entry has been set to '1'. |
||
| 1009 | * |
||
| 1010 | * @param string $entityID The enityID to search for |
||
| 1011 | * @return bool True if the given entityID is listed as 'RandS' |
||
| 1012 | * (research-and-scholarship). False otherwise. |
||
| 1013 | */ |
||
| 1014 | public function isRandS($entityID) |
||
| 1018 | |||
| 1019 | /** |
||
| 1020 | * isInCommonRandS |
||
| 1021 | * |
||
| 1022 | * This method searches for the given entityID and checks if the |
||
| 1023 | *'InCommon_RandS' entry has been set to '1'. |
||
| 1024 | * |
||
| 1025 | * @param string $entityID The enityID to search for |
||
| 1026 | * @return bool True if the given entityID is listed as |
||
| 1027 | * 'InCommon_RandS'. False otherwise. |
||
| 1028 | */ |
||
| 1029 | public function isInCommonRandS($entityID) |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * isREFEDSRandS |
||
| 1036 | * |
||
| 1037 | * This method searches for the given entityID and checks if the |
||
| 1038 | *'REFEDS_RandS' entry has been set to '1'. |
||
| 1039 | * |
||
| 1040 | * @param string $entityID The enityID to search for |
||
| 1041 | * @return bool True if the given entityID is listed as |
||
| 1042 | * 'REFEDS_RandS'. False otherwise. |
||
| 1043 | */ |
||
| 1044 | public function isREFEDSRandS($entityID) |
||
| 1048 | |||
| 1049 | /** |
||
| 1050 | * isRegisteredByInCommon |
||
| 1051 | * |
||
| 1052 | * This method searches for the given entityID and checks if the |
||
| 1053 | *'Registered_By_InCommon' entry has been set to '1'. |
||
| 1054 | * |
||
| 1055 | * @param string $entityID The enityID to search for |
||
| 1056 | * @return bool True if the given entityID is listed as |
||
| 1057 | * 'Registered_By_InCommon'. False otherwise. |
||
| 1058 | */ |
||
| 1059 | public function isRegisteredByInCommon($entityID) |
||
| 1063 | |||
| 1064 | /** |
||
| 1065 | * isSIRTFI |
||
| 1066 | * |
||
| 1067 | * This method searches for the given entityID and checks if the |
||
| 1068 | *'SIRTFI' entry has been set to '1'. |
||
| 1069 | * |
||
| 1070 | * @param string $entityID The enityID to search for |
||
| 1071 | * @return bool True if the given entityID is listed as |
||
| 1072 | * SIRTFI. False otherwise. |
||
| 1073 | */ |
||
| 1074 | public function isSIRTFI($entityID) |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * getInCommonIdPs |
||
| 1081 | * |
||
| 1082 | * This method returns a two-dimensional array of InCommon IdPs. |
||
| 1083 | * The primary key of the array is the entityID, the secondary key is |
||
| 1084 | * either 'Organization_Name' (corresponds to OrganizationDisplayName) |
||
| 1085 | * or 'Display_Name' (corresponds to mdui:DisplayName). |
||
| 1086 | * If a non-null parameter is passed in it returns a subset of the |
||
| 1087 | * InCommon IdPs. 0 means list only non-whitelisted IdPs, 1 means list |
||
| 1088 | * only whitelisted IdPs, 2 means list only R&S IdPs. |
||
| 1089 | * |
||
| 1090 | * @param int $filter |
||
| 1091 | * null => all InCommonIdPs |
||
| 1092 | * 0 => non-whitelisted InCommon IdPs |
||
| 1093 | * 1 => whitelisted InCommon IdPs |
||
| 1094 | * 2 => R&S InCommon IdPs |
||
| 1095 | * $return array An array of InCommon IdP Organization Names and Display |
||
| 1096 | * Names, possibly filtered by whitelisted / non-whitelisted / R&S. |
||
| 1097 | */ |
||
| 1098 | public function getInCommonIdPs($filter = null) |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * getNonWhitelistedIdPs |
||
| 1121 | * |
||
| 1122 | * This method returns an array of non-whitelisted IdPs where the |
||
| 1123 | * keys of the array are the entityIDs and the values are the |
||
| 1124 | * pretty print Organization Names. |
||
| 1125 | * |
||
| 1126 | * @return array An array of non-whitelisted IdPs. |
||
| 1127 | */ |
||
| 1128 | public function getNonWhitelistedIdPs() |
||
| 1132 | |||
| 1133 | /** |
||
| 1134 | * getWhitelistedIdPs |
||
| 1135 | * |
||
| 1136 | * This method returns an array of whitelisted IdPs where the keys |
||
| 1137 | * of the array are the entityIDs and the values are the |
||
| 1138 | * pretty print Organization Names. |
||
| 1139 | * |
||
| 1140 | * @return array An array of whitelisted IdPs. |
||
| 1141 | */ |
||
| 1142 | public function getWhitelistedIdPs() |
||
| 1146 | |||
| 1147 | /** |
||
| 1148 | * getRandSIdPs |
||
| 1149 | * |
||
| 1150 | * This method returns an array of R&S IdPs where the keys |
||
| 1151 | * of the array are the entityIDs and the values are the |
||
| 1152 | * pretty print Organization Names. |
||
| 1153 | * |
||
| 1154 | * @return array An array of Research and Scholarship (R&S) IdPs. |
||
| 1155 | */ |
||
| 1156 | public function getRandSIdPs() |
||
| 1160 | |||
| 1161 | /** |
||
| 1162 | * getShibInfo |
||
| 1163 | * |
||
| 1164 | * This function returns an array with two types of Shibboleth |
||
| 1165 | * information. The first set of info is specific to the user's |
||
| 1166 | * current Shibboleth session, such as REMOTE_USER. The second set |
||
| 1167 | * of info reads info from the passed-in metadata file specific to |
||
| 1168 | * the IdP, such as the pretty-print name of the IdP. |
||
| 1169 | * |
||
| 1170 | * @param string $entityID (Optional) The entityID to search for in |
||
| 1171 | * the InCommon metadata. Defaults to the HTTP header |
||
| 1172 | * HTTP_SHIB_IDENTITY_PROVIDER. |
||
| 1173 | * @return array An array containing the various shibboleth |
||
| 1174 | * attributes for the current Shibboleth session. The |
||
| 1175 | * keys of the array are 'pretty print' names of the |
||
| 1176 | * various attribute value names (such as |
||
| 1177 | * 'User Identifier' for REMOTE_USER) and the values |
||
| 1178 | * of the array are the actual Shibboleth session values. |
||
| 1179 | */ |
||
| 1180 | public function getShibInfo($entityID = '') |
||
| 1244 | |||
| 1245 | /** |
||
| 1246 | * DOM2Array |
||
| 1247 | * |
||
| 1248 | * This function sorts the passed-in DOM corresponding to |
||
| 1249 | * idplist.xml and returns a 2D array where the keys are entityIDs |
||
| 1250 | * and the values are arrays of attributes for each IdP. |
||
| 1251 | * |
||
| 1252 | * @param DOMDocument The DOM containing the list of IdPs to convert to |
||
| 1253 | * an array. Returns null on error. |
||
| 1254 | * @return array An array corresponding to the DOM of the IdPs. |
||
| 1255 | */ |
||
| 1256 | public function DOM2Array($dom) |
||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * array2DOM |
||
| 1281 | * |
||
| 1282 | * This function takes an array of IdPs (such as idparray) and |
||
| 1283 | * returns a corresponding DOM which can be written to XML. |
||
| 1284 | * |
||
| 1285 | * @param array $arr An array corresponding to the idplist. |
||
| 1286 | * @return DOMDocument A DOM for the idplist which can be written to XML. |
||
| 1287 | */ |
||
| 1288 | public function array2DOM($arr) |
||
| 1310 | } |
||
| 1311 |
If you suppress an error, we recommend checking for the error condition explicitly: