Complex classes like CodendiSOAP 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 CodendiSOAP, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class CodendiSOAP extends SoapClient { |
||
18 | var $sess_hash; |
||
19 | var $wsdl_string; |
||
20 | var $proxy_host; |
||
21 | var $proxy_port; |
||
22 | var $connected; |
||
23 | var $session_string; |
||
24 | var $session_file; // Configuration file for this session |
||
25 | var $session_group_id; // Default group |
||
26 | var $session_user; // Logged user name |
||
27 | var $session_user_id; // Logged user ID |
||
28 | protected $fileChunkSize; |
||
29 | protected $maxRetry; // Max number of soap call retry in case of failure |
||
30 | protected $callDelay; // Time "spacer" between 2 failing soap calls |
||
31 | /** |
||
32 | * constructor |
||
33 | */ |
||
34 | function __construct() { |
||
35 | $this->wsdl_string = ""; |
||
36 | $this->proxy_host = ""; |
||
37 | $this->proxy_port = 0; |
||
38 | $this->connected = false; |
||
39 | $this->session_string = ""; |
||
40 | $this->session_group_id = 0; // By default don't use a group |
||
41 | $this->session_user = ""; |
||
42 | $this->session_user_id = 0; |
||
43 | $this->fileChunkSize = 6000000; // ~6 Mo; |
||
44 | $this->maxRetry = 0; |
||
45 | $this->callDelay = 5; |
||
46 | |||
47 | // Try to find a dir where to put the session file |
||
48 | $session_dir = 0; |
||
49 | if (array_key_exists("HOME", $_ENV)) { |
||
50 | $session_dir = $_ENV["HOME"]."/"; |
||
51 | } else if (array_key_exists("HOMEPATH", $_ENV) && array_key_exists("HOMEDRIVE", $_ENV)) { // For Windows |
||
52 | $session_dir = $_ENV["HOMEDRIVE"]."\\".$_ENV["HOMEPATH"]."\\"; |
||
53 | |||
54 | } |
||
55 | |||
56 | $this->session_file = $session_dir.".codendirc"; |
||
57 | if (file_exists($this->session_file)) { |
||
58 | $this->readSession(); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * call - Calls a SOAP method |
||
64 | * |
||
65 | * @param string Command name |
||
66 | * @param array Parameter array |
||
67 | * @param bool Specify if we should pass the server common parameters like the session ID |
||
68 | */ |
||
69 | function call($command,$params=array(),$use_extra_params=true) { |
||
70 | global $LOG; |
||
71 | |||
72 | // checks if a session is established |
||
73 | if ($command != "login" && strlen($this->session_string) == 0) { |
||
74 | exit_error("You must start a session first using the \"login\" function"); |
||
75 | } |
||
76 | |||
77 | if (!$this->connected) { // try to connect to the server |
||
78 | $this->connect(); |
||
79 | } |
||
80 | |||
81 | // Add session parameters |
||
82 | if ($use_extra_params) { |
||
83 | if (!array_key_exists("sessionKey", $params)) { |
||
84 | //$params["sessionKey"] = $this->session_string; |
||
85 | $params = array('sessionKey' => $this->session_string) + $params; // params need to be in the right order (sessionKey first) |
||
86 | } |
||
87 | } |
||
88 | |||
89 | $nbAttempt = 0; |
||
90 | $soapCallSuccess = false; |
||
91 | do { |
||
92 | $nbAttempt++; |
||
93 | try { |
||
94 | $LOG->add("CodendiSOAP::Executing command ".$command." ..."); |
||
95 | return call_user_func_array(array($this, $command), $params); |
||
96 | } |
||
97 | catch (SoapFault $e) { |
||
98 | if (strtolower($e->faultcode) == 'http' && |
||
|
|||
99 | strtolower($e->faultstring) == 'error fetching http headers' && |
||
100 | $nbAttempt < $this->getMaxRetry()) { |
||
101 | $GLOBALS['LOG']->add('CodendiSOAP::An error occured while executing '.$command.', try again [Nb attempt: '.$nbAttempt.'/'.$GLOBALS['soap']->getMaxRetry().']. Wait for '.($nbAttempt * $this->getCallDelay()).' seconds (mitigate network congestion) ...'); |
||
102 | sleep($nbAttempt * $this->getCallDelay()); |
||
103 | } else { |
||
104 | throw $e; |
||
105 | } |
||
106 | } |
||
107 | } while ($nbAttempt < $this->getMaxRetry()); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * connect - Establish the connection to the server. This is done in the constructor |
||
112 | * of the soap_client class |
||
113 | */ |
||
114 | function connect() { |
||
115 | global $LOG; |
||
116 | |||
117 | try { |
||
118 | $log_proxy = ''; |
||
119 | if ($this->proxy_host && $this->proxy_port) { |
||
120 | $log_proxy = ', using proxy '.$this->proxy_host.':'.$this->proxy_port; |
||
121 | } |
||
122 | $LOG->add("CodendiSOAP::Connecting to the server ".$this->getWSDLString().$log_proxy."..."); |
||
123 | $options = array('trace' => true); |
||
124 | if ($this->proxy_host && $this->proxy_port) { |
||
125 | $options['proxy_host'] = $this->proxy_host; |
||
126 | $options['proxy_port'] = (int)$this->proxy_port; |
||
127 | } |
||
128 | parent::__construct($this->getWSDLString(), $options); |
||
129 | } catch (SoapFault $fault) { |
||
130 | exit_error($fault, $this->faultcode); |
||
131 | } |
||
132 | $LOG->add("CodendiSOAP::Connected!"); |
||
133 | $this->connected = true; |
||
134 | |||
135 | } |
||
136 | |||
137 | /** |
||
138 | * setSessionString - Set the session ID for future calls |
||
139 | * |
||
140 | * @param string Session string ID |
||
141 | */ |
||
142 | function setSessionString($string) { |
||
145 | |||
146 | function setSessionGroupID($group_id) { |
||
149 | |||
150 | function getSessionGroupID() { |
||
153 | |||
154 | function setSessionUser($user) { |
||
157 | |||
158 | function getSessionUser() { |
||
161 | |||
162 | function setSessionUserID($user_id) { |
||
165 | |||
166 | function getSessionUserID() { |
||
169 | |||
170 | function setWSDLString($wsdl) { |
||
173 | function getWSDLString() { |
||
174 | if (!$this->wsdl_string) { |
||
175 | if (defined("WSDL_URL")) { |
||
176 | $this->wsdl_string = WSDL_URL; |
||
177 | } else { |
||
178 | exit_error("SOAP API: URL of the WSDL is not defined. Please set your TULEAP_WSDL environment variable."); |
||
179 | } |
||
180 | } |
||
181 | return $this->wsdl_string; |
||
182 | } |
||
183 | |||
184 | function setProxy($proxy) { |
||
185 | $arr_proxy = explode(":", $proxy); |
||
186 | $this->proxy_host = $arr_proxy[0]; |
||
187 | $this->proxy_port = $arr_proxy[1]; |
||
188 | } |
||
189 | function getProxyHost() { |
||
195 | |||
196 | function getFileChunkSize() { |
||
202 | |||
203 | function getMaxRetry() { |
||
209 | |||
210 | function getCallDelay() { |
||
216 | |||
217 | function saveSession() { |
||
239 | |||
240 | function readSession() { |
||
258 | |||
259 | function endSession() { |
||
260 | if (file_exists($this->session_file) && !@unlink($this->session_file)) { |
||
261 | exit_error("Could not delete existing session file ".$this->session_file); |
||
262 | } |
||
263 | |||
269 | } |
||
270 | ?> |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.