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 NFe 55 or 65 |
||
103 | * @var int |
||
104 | */ |
||
105 | protected $modelo = 55; |
||
106 | /** |
||
107 | * Version of layout |
||
108 | * @var string |
||
109 | */ |
||
110 | protected $versao = '4.00'; |
||
111 | /** |
||
112 | * urlPortal |
||
113 | * Instância do WebService |
||
114 | * |
||
115 | * @var string |
||
116 | */ |
||
117 | protected $urlPortal = 'http://www.portalfiscal.inf.br/nfe'; |
||
118 | /** |
||
119 | * urlcUF |
||
120 | * @var int |
||
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 | null |
||
151 | */ |
||
152 | protected $objHeader = null; |
||
153 | /** |
||
154 | * @var string |
||
155 | */ |
||
156 | protected $urlHeader = ''; |
||
157 | /** |
||
158 | * @var array |
||
159 | */ |
||
160 | protected $soapnamespaces = [ |
||
161 | 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", |
||
162 | 'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema", |
||
163 | 'xmlns:soap' => "http://www.w3.org/2003/05/soap-envelope" |
||
164 | ]; |
||
165 | /** |
||
166 | * @var array |
||
167 | */ |
||
168 | protected $availableVersions = ['4.00' => 'PL_009_V4']; |
||
169 | /** |
||
170 | * @var string |
||
171 | */ |
||
172 | protected $typePerson = 'J'; |
||
173 | /** |
||
174 | * @var string |
||
175 | */ |
||
176 | protected $timezone; |
||
177 | |||
178 | /** |
||
179 | * Loads configurations and Digital Certificate, map all paths, set timezone and instanciate Contingency::class |
||
180 | * @param string $configJson content of config in json format |
||
181 | * @param Certificate $certificate |
||
182 | * @param Contingency|null $contingency |
||
183 | */ |
||
184 | 58 | public function __construct($configJson, Certificate $certificate, Contingency $contingency = null) |
|
185 | { |
||
186 | 58 | $this->pathwsfiles = realpath(__DIR__ . '/../../storage') . '/'; |
|
187 | //valid config json string |
||
188 | 58 | $this->config = Config::validate($configJson); |
|
189 | 58 | $this->version($this->config->versao); |
|
190 | 58 | $this->setEnvironmentTimeZone($this->config->siglaUF); |
|
191 | 58 | $this->certificate = $certificate; |
|
192 | 58 | $this->typePerson = $this->getTypeOfPersonFromCertificate(); |
|
193 | 58 | $this->setEnvironment($this->config->tpAmb); |
|
194 | 58 | if (empty($contingency)) { |
|
195 | 58 | $this->contingency = new Contingency(); |
|
196 | } |
||
197 | 58 | } |
|
198 | |||
199 | /** |
||
200 | * Sets environment time zone |
||
201 | * @param string $acronym (ou seja a sigla do estado) |
||
202 | * @return void |
||
203 | */ |
||
204 | 58 | public function setEnvironmentTimeZone($acronym) |
|
208 | |||
209 | /** |
||
210 | * Return J or F from existing type in ASN.1 certificate |
||
211 | * J - pessoa juridica (CNPJ) |
||
212 | * F - pessoa física (CPF) |
||
213 | * @return string |
||
214 | */ |
||
215 | 58 | public function getTypeOfPersonFromCertificate() |
|
232 | |||
233 | /** |
||
234 | * Set application version |
||
235 | * @param string $ver |
||
236 | */ |
||
237 | public function setVerAplic($ver) |
||
241 | |||
242 | /** |
||
243 | * Load Soap Class |
||
244 | * Soap Class may be \NFePHP\Common\Soap\SoapNative |
||
245 | * or \NFePHP\Common\Soap\SoapCurl |
||
246 | * @param SoapInterface $soap |
||
247 | * @return void |
||
248 | */ |
||
249 | public function loadSoapClass(SoapInterface $soap) |
||
254 | |||
255 | /** |
||
256 | * Set OPENSSL Algorithm using OPENSSL constants |
||
257 | * @param int $algorithm |
||
258 | * @return void |
||
259 | */ |
||
260 | public function setSignAlgorithm($algorithm = OPENSSL_ALGO_SHA1) |
||
264 | |||
265 | /** |
||
266 | * Set or get model of document NFe = 55 or NFCe = 65 |
||
267 | * @param int $model |
||
268 | * @return int modelo class parameter |
||
269 | */ |
||
270 | public function model($model = null) |
||
277 | |||
278 | /** |
||
279 | * Set or get parameter layout version |
||
280 | * @param string $version |
||
281 | * @return string |
||
282 | * @throws InvalidArgumentException |
||
283 | */ |
||
284 | 58 | public function version($version = null) |
|
302 | |||
303 | /** |
||
304 | * Recover cUF number from state acronym |
||
305 | * @param string $acronym Sigla do estado |
||
306 | * @return int number cUF |
||
307 | */ |
||
308 | public function getcUF($acronym) |
||
312 | |||
313 | /** |
||
314 | * Recover state acronym from cUF number |
||
315 | * @param int $cUF |
||
316 | * @return string acronym sigla |
||
317 | */ |
||
318 | public function getAcronym($cUF) |
||
322 | |||
323 | /** |
||
324 | * Validate cUF from the key content and returns the state acronym |
||
325 | * @param string $chave |
||
326 | * @return string |
||
327 | * @throws InvalidArgumentException |
||
328 | */ |
||
329 | public function validKeyByUF($chave) |
||
339 | |||
340 | /** |
||
341 | * Sign NFe or NFCe |
||
342 | * @param string $xml NFe xml content |
||
343 | * @return string signed NFe xml |
||
344 | * @throws RuntimeException |
||
345 | */ |
||
346 | public function signNFe($xml) |
||
377 | |||
378 | /** |
||
379 | * Corrects NFe fields when in contingency mode |
||
380 | * @param string $xml NFe xml content |
||
381 | * @return string |
||
382 | */ |
||
383 | protected function correctNFeForContingencyMode($xml) |
||
387 | |||
388 | /** |
||
389 | * Performs xml validation with its respective XSD structure definition document |
||
390 | * NOTE: if don't exists the XSD file will return true |
||
391 | * @param string $version layout version |
||
392 | * @param string $body |
||
393 | * @param string $method |
||
394 | * @return bool |
||
395 | */ |
||
396 | protected function isValid($version, $body, $method) |
||
404 | |||
405 | /** |
||
406 | * Verifies the existence of the service |
||
407 | * @param string $service |
||
408 | * @throws RuntimeException |
||
409 | */ |
||
410 | protected function checkContingencyForWebServices($service) |
||
442 | |||
443 | /** |
||
444 | * Alter environment from "homologacao" to "producao" and vice-versa |
||
445 | * @param int $tpAmb |
||
446 | * @return void |
||
447 | */ |
||
448 | 58 | public function setEnvironment($tpAmb = 2) |
|
455 | |||
456 | /** |
||
457 | * Set option for canonical transformation see C14n |
||
458 | * @param array $opt |
||
459 | * @return array |
||
460 | */ |
||
461 | public function canonicalOptions(array $opt = [true, false, null, null]) |
||
468 | |||
469 | /** |
||
470 | * Assembles all the necessary parameters for soap communication |
||
471 | * @param string $service |
||
472 | * @param string $uf |
||
473 | * @param int $tpAmb 1-Production or 2-Homologation |
||
474 | * @param bool $ignoreContingency |
||
475 | * @throws RuntimeException |
||
476 | * @return void |
||
477 | */ |
||
478 | protected function servico($service, $uf, $tpAmb, $ignoreContingency = false) |
||
505 | |||
506 | /** |
||
507 | * Send request message to webservice |
||
508 | * @param string $request |
||
509 | * @param array $parameters |
||
510 | * @return string |
||
511 | * @throws RuntimeException |
||
512 | */ |
||
513 | 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() |
||
549 | |||
550 | /** |
||
551 | * Add QRCode Tag to signed XML from a NFCe |
||
552 | * @param DOMDocument $dom |
||
553 | * @return string |
||
554 | */ |
||
555 | protected function addQRCode(DOMDocument $dom) |
||
577 | |||
578 | /** |
||
579 | * Get URI for search NFCe by key (chave) |
||
580 | * @param string $uf Abbreviation of the UF |
||
581 | * @param string $tpAmb SEFAZ environment, 1-Production or 2-Homologation |
||
582 | * @return string |
||
583 | */ |
||
584 | 54 | protected function getURIConsultaNFCe($uf, $tpAmb) |
|
590 | |||
591 | /** |
||
592 | * Verify if SOAP class is loaded, if not, force load SoapCurl |
||
593 | */ |
||
594 | protected function checkSoap() |
||
600 | |||
601 | /** |
||
602 | * Verify if xml model is equal as modelo property |
||
603 | * @param string $xml |
||
604 | * @return bool |
||
605 | */ |
||
606 | protected function checkModelFromXml($xml) |
||
613 | } |
||
614 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..