Complex classes like File 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 File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class File { |
||
10 | |||
11 | private $name = null; |
||
12 | private $tempName = null; |
||
13 | private $extension = null; |
||
14 | private $size = 0; |
||
15 | |||
16 | /** @var Directory */ |
||
17 | private $directory = null; |
||
18 | protected $uploadPrepared = false; |
||
19 | private $oldName = null; |
||
20 | protected static $maxSize = 10; |
||
21 | protected static $validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'csv', 'doc', 'docx', 'odt', 'pdf', 'txt', 'md', 'mp3', 'wav', 'mpeg']; |
||
22 | |||
23 | /* Construtor */ |
||
24 | |||
25 | public function __construct($name = '') { |
||
35 | |||
36 | /* Metodos de acesso */ |
||
37 | |||
38 | public function getName() { |
||
41 | |||
42 | public function getTempName() { |
||
45 | |||
46 | public function getExtension() { |
||
52 | |||
53 | public function getSize() { |
||
56 | |||
57 | public function getDirectory() { |
||
60 | |||
61 | public function getOldName() { |
||
64 | |||
65 | public function setName($name) { |
||
68 | |||
69 | public function setTempName($tempName) { |
||
72 | |||
73 | /** @param string $directory */ |
||
74 | public function setDirectory($directory) { |
||
77 | |||
78 | public function setOldName($oldName) { |
||
81 | |||
82 | public function getFullName() { |
||
85 | |||
86 | public function __toString() { |
||
93 | |||
94 | /* Metodos */ |
||
95 | |||
96 | /** |
||
97 | * Recebe o Arquivo temporário |
||
98 | * @param array $files $_FILES['arquivo'] |
||
99 | * @example $obj->receiveFiles($_FILES['arquivo']); |
||
100 | */ |
||
101 | function receiveFiles($files) { |
||
112 | |||
113 | /** |
||
114 | * Recebe o Arquivo temporário |
||
115 | * @param string $fileName $_POST['arquivo'] |
||
116 | * @example $obj->receivePost($_POST['arquivo']); |
||
117 | */ |
||
118 | public function receivePost($fileName) { |
||
129 | |||
130 | /** |
||
131 | * Realiza o envio para a pasta, reduzindo o tamanho e com um nome aleatório |
||
132 | * @param string $newName opcional, escolhe o nome que será salvo |
||
133 | * @return string Retorna algum erro ou NULL |
||
134 | */ |
||
135 | function upload($newName = '') { |
||
169 | |||
170 | /** Retorna true se arquivo existe */ |
||
171 | public function exists() { |
||
174 | |||
175 | /** Move o arquivo de $temp para $diretorio atual */ |
||
176 | public function move() { |
||
185 | |||
186 | /** Exclui o arquivo no diretorio */ |
||
187 | public function remove() { |
||
192 | |||
193 | /** Exclui o arquivo que existia antes de enviar */ |
||
194 | public function removeOld() { |
||
200 | |||
201 | /** |
||
202 | * Remove diretorio(s) por expressao regular |
||
203 | * @param string $regExp |
||
204 | * @return int quantidade de diretorios removidos |
||
205 | */ |
||
206 | public static function removeRegExp($regExp) { |
||
213 | |||
214 | /** |
||
215 | * Retorna a extension pelo nome |
||
216 | * @param string $name |
||
217 | * @return string |
||
218 | */ |
||
219 | protected static function getExtensionByName($name) { |
||
223 | |||
224 | /** |
||
225 | * Salva conteudo no arquivo |
||
226 | * @param string $content |
||
227 | * @param string $mode |
||
228 | */ |
||
229 | public function write($content = '', $mode = 'a') { |
||
237 | |||
238 | /** |
||
239 | * Retorna o conteudo do arquivo |
||
240 | * @param string $content |
||
241 | */ |
||
242 | public function read() { |
||
245 | |||
246 | } |
||
247 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.