Issues (14)

Developers/Dir.php (10 issues)

1
<?php
2
3
/*
4
    ***********************************************
5
   Dir .PHP -
6
   Class responsible for managing files, folders and subfolders within your project.
7
   Classe responsável para gerenciar arquivos, pastas e subpastas dentro do seu projeto.
8
9
   With this class you can create or remove files, folders and subfolders.
10
   Com esta classe você pode criar ou remover arquivos, pastas e subpastas.
11
    ***********************************************
12
13
    Copyright (c) 2020, Jeferson L. Souza INTERLIG SOLUÇÕES INTELIGENTES
14
    E-mail: [email protected]
15
    Site: http://interligsolucoes.com.br/
16
*/
17
namespace Developers;
18
19
20
class Dir
21
{
22
    /**
23
     * Method for Reading Files - Presenting Document Information on the Screen.
24
     * Método Para Leitura de Arquivos - Apresentando as Informações do Documento na Tela.
25
     * @param $file
26
     */
27
    public function FileRead($file){
28
        $fopen = fopen($file, "r");
29
        while($showData = fgets($fopen)){
0 ignored issues
show
It seems like $fopen can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        while($showData = fgets(/** @scrutinizer ignore-type */ $fopen)){
Loading history...
30
            echo $showData ."<br>";
31
        }
32
        fclose($fopen);
0 ignored issues
show
It seems like $fopen can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
        fclose(/** @scrutinizer ignore-type */ $fopen);
Loading history...
33
    }
34
35
    /**
36
     * Method for Creating Archive and Adding Content
37
     * Método Para Criar Arquivo e Adicionar Conteúdos
38
     * @param $file
39
     * @param $text
40
     * @return bool
41
     */
42
    public function FileWrite($file, $text){
43
        $fopen = fopen($file, "w");
44
        fwrite($fopen, $text);
0 ignored issues
show
It seems like $fopen can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        fwrite(/** @scrutinizer ignore-type */ $fopen, $text);
Loading history...
45
        fclose($fopen);
0 ignored issues
show
It seems like $fopen can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        fclose(/** @scrutinizer ignore-type */ $fopen);
Loading history...
46
47
        return true;
48
    }
49
50
    /**
51
     * Method for Reading Files and Adding New Content
52
     * Método Para Leitura de Arquivos e Adicionar Novo Conteúdo
53
     * @param $file
54
     * @param $text
55
     * @return bool
56
     */
57
    public function FileReadWrite($file, $text){
58
        $fopen = fopen($file, "a+");
59
        fwrite($fopen, $text.PHP_EOL);
0 ignored issues
show
It seems like $fopen can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
        fwrite(/** @scrutinizer ignore-type */ $fopen, $text.PHP_EOL);
Loading history...
60
        fclose($fopen);
0 ignored issues
show
It seems like $fopen can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        fclose(/** @scrutinizer ignore-type */ $fopen);
Loading history...
61
62
        return true;
63
    }
64
65
    /**
66
     * Create Folder Within the Project Directory
67
     * Criar Pasta Dentro do Diretório do Projeto
68
     * @param $dir
69
     * @return bool
70
     */
71
    public function CreateDir ($dir) {
72
        $dirVerify = str_replace("\\", "/", $dir);
73
74
        if(!is_dir($dirVerify) && !file_exists($dirVerify)){
75
            mkdir($dirVerify, "0755");
76
            return true;
77
        }else{
78
            return false;
79
        }
80
    }
81
82
    /**
83
     * Create Folder and File Within the Folder
84
     * Criar Pasta e Arquivo Dentro da Pasta
85
     * @param $dir
86
     * @param $folder
87
     * @param string $file
88
     * @param string $text
89
     * @return bool|null
90
     */
91
    public function CreateFileDir ($dir, $folder, $file = "", $text = ""){
92
        $dirVerify = str_replace("\\", "/", $dir);
93
94
        //Create the folder
95
        //Criar a pasta
96
        if(!is_dir($dirVerify) && !file_exists($dirVerify)) {
97
            mkdir($dirVerify, "0755");
98
        }
99
100
        //Create the subfolder
101
        //Criar a subpasta
102
        if(!is_dir($dirVerify . $folder) && !file_exists($dirVerify . $folder)){
103
            mkdir($dirVerify . $folder, "0755");
104
        }
105
106
        //Create the file with the text
107
        //Criar o arquivo com o texto
108
        if($file != '' || $file != null) {
109
            $fopen = file_put_contents($dirVerify . $folder . $file, $text . "\n", FILE_APPEND);
0 ignored issues
show
The assignment to $fopen is dead and can be removed.
Loading history...
110
        }
111
112
        //Checks whether the folder, subfolder and file exist
113
        //Verifica se a pasta, subpasta e arquivo existem
114
        if(file_exists($dirVerify . $folder . $file)){
115
            return true;
116
        }else{
117
            return false;
118
        }
119
    }
120
121
    /**
122
     * Remove Specific Files Within a Directory
123
     * Remover Arquivos Específico Dentro de um Diretório
124
     * @param $dir
125
     * @return bool|null
126
     */
127
    public function RemoveFile ($dir){
128
        $dirVerify = str_replace("\\", "/", $dir);
129
130
        if(file_exists($dirVerify)){
131
            unlink($dirVerify);
132
            return true;
133
        }else{
134
            return false;
135
        }
136
    }
137
138
    /**
139
     * Removing Multiple Files Within a Directory
140
     * Remover Multiplos Arquivos Dentro de Um Diretório
141
     * @param string $dir
142
     * @param array $files
143
     * @return string
144
     */
145
    public function RemoveFileMultiple (string $dir, array $files){
146
        $dirVerify = str_replace("\\", "/", $dir);
147
148
        if(is_dir($dirVerify) && file_exists($dirVerify)){
149
150
            for($a = 0; $a < count($files); $a++){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
151
                if(is_dir("{$dirVerify}/{$files[$a]}") && !file_exists("{$dirVerify}/{$files[$a]}")){
152
153
                }else{
154
                    if(file_exists($dirVerify . $files[$a])) {
155
                        unlink("{$dirVerify}/{$files[$a]}");
156
                    }else{
157
                        echo "Não foi possível remover um ou mais arquivo(s)!";
158
                        return;
159
                    }
160
                }
161
            }
162
            echo "Arquivo(s) removido(s) com sucesso!";
163
        }else{
164
            echo "Erro: Não foi possível localizar o(s) arquivo(s)!";
165
        }
166
    }
167
168
    /** Removing a Directory with PHP
169
     * Remover Um Diretório com o PHP
170
     * @param $dir
171
     * @return bool
172
     */
173
    public function RemoveDir ($dir){
174
        $dirVerify = str_replace("\\", "/", $dir);
175
        //Lista os arquivos e remove os pontos da listagem do scandir com array_diff
176
        $files = array_diff(scandir($dirVerify), array(".", ".."));
0 ignored issues
show
It seems like scandir($dirVerify) can also be of type false; however, parameter $array1 of array_diff() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

176
        $files = array_diff(/** @scrutinizer ignore-type */ scandir($dirVerify), array(".", ".."));
Loading history...
177
178
        if(is_dir($dirVerify) && file_exists($dirVerify)){
179
            foreach($files as $file){
180
                if(is_dir("{$dirVerify}/{$file}")){
181
                    delTree("{$dirVerify}/{$file}");
0 ignored issues
show
The function delTree was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

181
                    /** @scrutinizer ignore-call */ delTree("{$dirVerify}/{$file}");
Loading history...
182
                }else{
183
                    unlink("{$dirVerify}/{$file}");
184
                }
185
            }
186
            rmdir($dir);
187
            return true;
188
        }else{
189
            return false;
190
        }
191
    }
192
}