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 static function getValidExtensions() { |
||
89 | |||
90 | public function __toString() { |
||
97 | |||
98 | /* Metodos */ |
||
99 | |||
100 | /** |
||
101 | * Recebe o Arquivo temporário |
||
102 | * @param array $files $_FILES['arquivo'] |
||
103 | * @example $obj->receiveFiles($_FILES['arquivo']); |
||
104 | */ |
||
105 | function receiveFiles($files) { |
||
116 | |||
117 | /** |
||
118 | * Recebe o Arquivo temporário |
||
119 | * @param string $fileName $_POST['arquivo'] |
||
120 | * @example $obj->receivePost($_POST['arquivo']); |
||
121 | */ |
||
122 | public function receivePost($fileName) { |
||
133 | |||
134 | /** |
||
135 | * Realiza o envio para a pasta, reduzindo o tamanho e com um nome aleatório |
||
136 | * @param string $newName opcional, escolhe o nome que será salvo |
||
137 | * @return string Retorna algum erro ou NULL |
||
138 | */ |
||
139 | function upload($newName = '') { |
||
173 | |||
174 | /** Retorna true se arquivo existe */ |
||
175 | public function exists() { |
||
178 | |||
179 | /** Move o arquivo de $temp para $diretorio atual */ |
||
180 | public function move() { |
||
189 | |||
190 | /** Exclui o arquivo no diretorio */ |
||
191 | public function remove() { |
||
196 | |||
197 | /** Exclui o arquivo que existia antes de enviar */ |
||
198 | public function removeOld() { |
||
204 | |||
205 | /** |
||
206 | * Remove diretorio(s) por expressao regular |
||
207 | * @param string $regExp |
||
208 | * @return int quantidade de diretorios removidos |
||
209 | */ |
||
210 | public static function removeRegExp($regExp) { |
||
217 | |||
218 | /** |
||
219 | * Retorna a extension pelo nome |
||
220 | * @param string $name |
||
221 | * @return string |
||
222 | */ |
||
223 | protected static function getExtensionByName($name) { |
||
227 | |||
228 | /** |
||
229 | * Salva conteudo no arquivo |
||
230 | * @param string $content |
||
231 | * @param string $mode |
||
232 | */ |
||
233 | public function write($content = '', $mode = 'a') { |
||
241 | |||
242 | /** |
||
243 | * Retorna o conteudo do arquivo |
||
244 | * @param string $content |
||
245 | */ |
||
246 | public function read() { |
||
249 | |||
250 | } |
||
251 |
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.