Passed
Push — master ( fe1bfe...d29151 )
by Giuliano
02:24
created

FileSearch   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 100
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A files() 0 7 1
A __construct() 0 3 1
A add() 0 3 1
A path() 0 3 1
A result() 0 3 1
A scan() 0 14 5
A setPath() 0 8 2
1
<?php
2
3
namespace Maestriam\FileSystem\Foundation;
4
5
use Exception;
6
7
class FileSearch
8
{
9
    private string $path;
10
11
    private array $result = [];
12
13
    /**
14
     * Regras de negócio para pesquisa de arquivos dentro diretório
15
     *
16
     * @param string $path
17
     */
18
    public function __construct(string $path)
19
    {
20
        $this->setPath($path);
21
    }
22
23
    /**
24
     * Retorna a lista de todos os arquivos encontrados em diretório e
25
     * seus sub-diretórios.  
26
     *
27
     * @return array
28
     */
29
    public function files(string $pattern = null) : array
30
    {
31
        $pattern = "/". $pattern ."/";
32
33
        $this->scan($this->path(), $pattern);
34
35
        return $this->result();
36
    }
37
38
    /**
39
     * Faz a busca de arquivos de dentro de diretórios e subdiretórios
40
     *
41
     * @param string $path
42
     * @return void
43
     */
44
    private function scan(string $path, string $pattern = null)
45
    {
46
        $items = array_diff(scandir($path), array('.', '..'));
47
48
        foreach ($items as $item) { 
49
50
            $item = "$path/$item";
51
52
            if (is_dir($item)) { 
53
                return $this->scan($item, $pattern);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->scan($item, $pattern) targeting Maestriam\FileSystem\Foundation\FileSearch::scan() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
54
            }            
55
56
            if ($pattern == null || preg_match($pattern, $item)) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $pattern of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
57
                $this->add($item); 
58
            }
59
        }
60
    }
61
62
    /**
63
     * Adiciona o caminho de um arquivo para a lista de resultado
64
     *
65
     * @param string $file
66
     * @return int
67
     */
68
    private function add(string $file) : int
69
    {
70
        return array_push($this->result, $file);
71
    }
72
73
    /**
74
     * Retorna a lista de arquivos encontrados
75
     *
76
     * @return array
77
     */
78
    public function result() : array
79
    {
80
        return $this->result;
81
    }
82
83
    /**
84
     * Retorna o caminho utilizado para a pesquisa
85
     *
86
     * @return string
87
     */
88
    private function path() : string
89
    {
90
        return $this->path;
91
    }
92
93
    /**
94
     * Define o caminho para a pesquisa
95
     *
96
     * @param string $path
97
     * @return FileSearch
98
     */
99
    private function setPath(string $path) : FileSearch
100
    {
101
        if (! is_dir($path)) {
102
            throw new Exception('Path not found.');
103
        }
104
105
        $this->path = $path;
106
        return $this;
107
    }
108
}