Complex classes like Tools 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 Tools, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class Tools |
||
35 | { |
||
36 | /** |
||
37 | * config class |
||
38 | * @var \stdClass |
||
39 | */ |
||
40 | public $config; |
||
41 | /** |
||
42 | * Path to storage folder |
||
43 | * @var string |
||
44 | */ |
||
45 | public $pathwsfiles = ''; |
||
46 | /** |
||
47 | * Path to schemes folder |
||
48 | * @var string |
||
49 | */ |
||
50 | public $pathschemes = ''; |
||
51 | /** |
||
52 | * ambiente |
||
53 | * @var string |
||
54 | */ |
||
55 | public $ambiente = 'homologacao'; |
||
56 | /** |
||
57 | * Environment |
||
58 | * @var int |
||
59 | */ |
||
60 | public $tpAmb = 2; |
||
61 | /** |
||
62 | * contingency class |
||
63 | * @var Contingency |
||
64 | */ |
||
65 | public $contingency; |
||
66 | /** |
||
67 | * soap class |
||
68 | * @var SoapInterface |
||
69 | */ |
||
70 | public $soap; |
||
71 | /** |
||
72 | * Application version |
||
73 | * @var string |
||
74 | */ |
||
75 | public $verAplic = ''; |
||
76 | /** |
||
77 | * last soap request |
||
78 | * @var string |
||
79 | */ |
||
80 | public $lastRequest = ''; |
||
81 | /** |
||
82 | * last soap response |
||
83 | * @var string |
||
84 | */ |
||
85 | public $lastResponse = ''; |
||
86 | /** |
||
87 | * certificate class |
||
88 | * @var Certificate |
||
89 | */ |
||
90 | protected $certificate; |
||
91 | /** |
||
92 | * Sign algorithm from OPENSSL |
||
93 | * @var int |
||
94 | */ |
||
95 | protected $algorithm = OPENSSL_ALGO_SHA1; |
||
96 | /** |
||
97 | * Canonical conversion options |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $canonical = [true,false,null,null]; |
||
101 | /** |
||
102 | * Model of BPe 63 |
||
103 | * @var int |
||
104 | */ |
||
105 | protected $modelo = 63; |
||
106 | /** |
||
107 | * Version of layout |
||
108 | * @var string |
||
109 | */ |
||
110 | protected $versao = '1.00'; |
||
111 | /** |
||
112 | * urlPortal |
||
113 | * Inst�ncia do WebService |
||
114 | * |
||
115 | * @var string |
||
116 | */ |
||
117 | protected $urlPortal = 'http://www.portalfiscal.inf.br/bpe'; |
||
118 | /** |
||
119 | * urlcUF |
||
120 | * @var string |
||
121 | */ |
||
122 | protected $urlcUF = ''; |
||
123 | /** |
||
124 | * urlVersion |
||
125 | * @var string |
||
126 | */ |
||
127 | protected $urlVersion = ''; |
||
128 | /** |
||
129 | * urlService |
||
130 | * @var string |
||
131 | */ |
||
132 | protected $urlService = ''; |
||
133 | /** |
||
134 | * @var string |
||
135 | */ |
||
136 | protected $urlMethod = ''; |
||
137 | /** |
||
138 | * @var string |
||
139 | */ |
||
140 | protected $urlOperation = ''; |
||
141 | /** |
||
142 | * @var string |
||
143 | */ |
||
144 | protected $urlNamespace = ''; |
||
145 | /** |
||
146 | * @var string |
||
147 | */ |
||
148 | protected $urlAction = ''; |
||
149 | /** |
||
150 | * @var \SOAPHeader |
||
151 | */ |
||
152 | protected $objHeader; |
||
153 | /** |
||
154 | * @var string |
||
155 | */ |
||
156 | protected $urlHeader = ''; |
||
157 | /** |
||
158 | * @var bool |
||
159 | */ |
||
160 | protected $allowSignatureInQRCode = false; |
||
161 | /** |
||
162 | * @var array |
||
163 | */ |
||
164 | protected $soapnamespaces = [ |
||
165 | 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", |
||
166 | 'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema", |
||
167 | 'xmlns:soap' => "http://www.w3.org/2003/05/soap-envelope" |
||
168 | ]; |
||
169 | /** |
||
170 | * @var array |
||
171 | */ |
||
172 | protected $availableVersions = [ |
||
173 | '1.00' => 'PL_BPe_100b' |
||
174 | ]; |
||
175 | |||
176 | /** |
||
177 | * Constructor |
||
178 | * load configurations, |
||
179 | * load Digital Certificate, |
||
180 | * map all paths, |
||
181 | * set timezone and |
||
182 | * and instanciate Contingency::class |
||
183 | * @param string $configJson content of config in json format |
||
184 | * @param Certificate $certificate |
||
185 | */ |
||
186 | public function __construct($configJson, Certificate $certificate) |
||
197 | |||
198 | /** |
||
199 | * Sets environment time zone |
||
200 | * @param string $acronym (ou seja a sigla do estado) |
||
201 | * @return void |
||
202 | */ |
||
203 | public function setEnvironmentTimeZone($acronym) |
||
207 | |||
208 | /** |
||
209 | * Set application version |
||
210 | * @param string $ver |
||
211 | */ |
||
212 | public function setVerAplic($ver) |
||
216 | |||
217 | /** |
||
218 | * Set for inclusion of sign in qrcode string |
||
219 | * @param bool $flag |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function setSignatureInQRCode($flag = false) |
||
226 | |||
227 | /** |
||
228 | * Load Soap Class |
||
229 | * Soap Class may be \NFePHP\Common\Soap\SoapNative |
||
230 | * or \NFePHP\Common\Soap\SoapCurl |
||
231 | * @param SoapInterface $soap |
||
232 | * @return void |
||
233 | */ |
||
234 | public function loadSoapClass(SoapInterface $soap) |
||
239 | |||
240 | /** |
||
241 | * Set OPENSSL Algorithm using OPENSSL constants |
||
242 | * @param int $algorithm |
||
243 | * @return void |
||
244 | */ |
||
245 | public function setSignAlgorithm($algorithm = OPENSSL_ALGO_SHA1) |
||
249 | |||
250 | /** |
||
251 | * Set or get model of document BPe = 63 |
||
252 | * @param int $model |
||
253 | * @return int modelo class parameter |
||
254 | */ |
||
255 | public function model($model = null) |
||
262 | |||
263 | /** |
||
264 | * Set or get parameter layout version |
||
265 | * @param string $version |
||
266 | * @return string |
||
267 | * @throws InvalidArgumentException |
||
268 | */ |
||
269 | public function version($version = '') |
||
283 | |||
284 | /** |
||
285 | * Recover cUF number from state acronym |
||
286 | * @param string $acronym Sigla do estado |
||
287 | * @return int number cUF |
||
288 | */ |
||
289 | public function getcUF($acronym) |
||
293 | |||
294 | /** |
||
295 | * Recover state acronym from cUF number |
||
296 | * @param int $cUF |
||
297 | * @return string acronym sigla |
||
298 | */ |
||
299 | public function getAcronym($cUF) |
||
303 | |||
304 | /** |
||
305 | * Validate cUF from the key content and returns the state acronym |
||
306 | * @param string $chave |
||
307 | * @return string |
||
308 | * @throws InvalidArgumentException |
||
309 | */ |
||
310 | public function validKeyByUF($chave) |
||
320 | |||
321 | /** |
||
322 | * Sign BPe |
||
323 | * @param string $xml BPe xml content |
||
324 | * @return string singed BPe xml |
||
325 | * @throws RuntimeException |
||
326 | */ |
||
327 | public function signBPe($xml) |
||
351 | |||
352 | protected function addQRCode($signed) |
||
410 | |||
411 | /** |
||
412 | * Performs xml validation with its respective |
||
413 | * XSD structure definition document |
||
414 | * NOTE: if dont exists the XSD file will return true |
||
415 | * @param string $version layout version |
||
416 | * @param string $body |
||
417 | * @param string $method |
||
418 | * @return boolean |
||
419 | */ |
||
420 | protected function isValid($version, $body, $method) |
||
431 | |||
432 | /** |
||
433 | * Alter environment from "homologacao" to "producao" and vice-versa |
||
434 | * @param int $tpAmb |
||
435 | * @return void |
||
436 | */ |
||
437 | public function setEnvironment($tpAmb = 2) |
||
444 | |||
445 | /** |
||
446 | * Set option for canonical transformation see C14n |
||
447 | * @param array $opt |
||
448 | * @return array |
||
449 | */ |
||
450 | public function canonicalOptions($opt = [true,false,null,null]) |
||
457 | |||
458 | /** |
||
459 | * Assembles all the necessary parameters for soap communication |
||
460 | * @param string $service |
||
461 | * @param string $uf |
||
462 | * @param string $tpAmb |
||
463 | * @param bool $ignoreContingency |
||
464 | * @return void |
||
465 | */ |
||
466 | protected function servico( |
||
511 | |||
512 | /** |
||
513 | * Send request message to webservice |
||
514 | * @param array $parameters |
||
515 | * @param string $request |
||
516 | * @return string |
||
517 | */ |
||
518 | protected function sendRequest($request, array $parameters = []) |
||
531 | |||
532 | /** |
||
533 | * Recover path to xml data base with list of soap services |
||
534 | * @return string |
||
535 | */ |
||
536 | protected function getXmlUrlPath() |
||
546 | |||
547 | /** |
||
548 | * Verify if SOAP class is loaded, if not, force load SoapCurl |
||
549 | */ |
||
550 | protected function checkSoap() |
||
556 | } |
||
557 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.