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 | private $directory = null; |
||
| 16 | protected $uploadPrepared = false; |
||
| 17 | private $oldName = null; |
||
| 18 | protected static $maxSize = 10; |
||
| 19 | protected static $validExtensions = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'csv', 'doc', 'docx', 'odt', 'pdf', 'txt', 'md', 'mp3', 'wav', 'mpeg']; |
||
| 20 | |||
| 21 | /* Construtor */ |
||
| 22 | |||
| 23 | public function __construct($name = '') { |
||
| 33 | |||
| 34 | /* Metodos de acesso */ |
||
| 35 | |||
| 36 | public function getName() { |
||
| 39 | |||
| 40 | public function getTempName() { |
||
| 43 | |||
| 44 | public function getExtension() { |
||
| 50 | |||
| 51 | public function getSize() { |
||
| 54 | |||
| 55 | public function getDirectory() { |
||
| 58 | |||
| 59 | public function getOldName() { |
||
| 62 | |||
| 63 | public function setName($name) { |
||
| 66 | |||
| 67 | public function setTempName($tempName) { |
||
| 70 | |||
| 71 | /** @param string $directory */ |
||
| 72 | public function setDirectory($directory) { |
||
| 75 | |||
| 76 | public function setOldName($oldName) { |
||
| 79 | |||
| 80 | public function getFullName() { |
||
| 83 | |||
| 84 | public function __toString() { |
||
| 91 | |||
| 92 | /* Metodos */ |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Recebe o Arquivo temporário |
||
| 96 | * @param array $files $_FILES['arquivo'] |
||
| 97 | * @example $obj->receiveFiles($_FILES['arquivo']); |
||
| 98 | */ |
||
| 99 | function receiveFiles($files) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Recebe o Arquivo temporário |
||
| 113 | * @param string $fileName $_POST['arquivo'] |
||
| 114 | * @example $obj->receivePost($_POST['arquivo']); |
||
| 115 | */ |
||
| 116 | public function receivePost($fileName) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Realiza o envio para a pasta, reduzindo o tamanho e com um nome aleatório |
||
| 130 | * @param string $newName opcional, escolhe o nome que será salvo |
||
| 131 | * @return string Retorna algum erro ou NULL |
||
| 132 | */ |
||
| 133 | function upload($newName = '') { |
||
| 167 | |||
| 168 | /** Retorna true se arquivo existe */ |
||
| 169 | public function exists() { |
||
| 172 | |||
| 173 | /** Move o arquivo de $temp para $diretorio atual */ |
||
| 174 | public function move() { |
||
| 181 | |||
| 182 | /** Exclui o arquivo no diretorio */ |
||
| 183 | public function remove() { |
||
| 188 | |||
| 189 | /** Exclui o arquivo que existia antes de enviar */ |
||
| 190 | public function removeOld() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Retorna a extension pelo nome |
||
| 199 | * @param string $name |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | protected static function getExtensionByName($name) { |
||
| 206 | |||
| 207 | } |
||
| 208 |
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.