Completed
Push — master ( 5d15e5...79c368 )
by Shcherbak
11:35 queued 07:36
created

FileFilter::isValid()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 12
1
<?php
2
3
  namespace Funivan\Cs\Fs;
4
5
  /**
6
   * @author Ivan Shcherbak <[email protected]> 2016
7
   */
8
  class FileFilter {
9
10
    /**
11
     * @var callable[]
12
     */
13
    private $callback = [];
14
15
16
    /**
17
     * @param string $type
18
     * @return $this
19
     */
20
    public function mimeType($type) {
21
      $this->callback[] = function (File $file) use ($type) {
22
        return (strpos($file->getMimeType(), $type) === 0);
23
      };
24
      return $this;
25
    }
26
27
28
    /**
29
     * @param string|array $regex
30
     * @return $this
31
     */
32
    public function path($regex) {
33
      $this->callback[] = function (File $file) use ($regex) {
34
        $regex = (array) $regex;
35
        $path = $file->getPath();
36
37
        foreach ($regex as $reg) {
38
          if (preg_match($reg, $path)) {
39
            return true;
40
          }
41
        }
42
        return false;
43
      };
44
45
      return $this;
46
    }
47
48
49
    /**
50
     * @param string[] $regex
51
     * @return $this
52
     */
53
    public function name(array $regex) {
54
      $this->callback[] = function (File $file) use ($regex) {
55
        $path = $file->getName();
56
57
        foreach ($regex as $reg) {
58
          if (preg_match($reg, $path)) {
59
            return true;
60
          }
61
        }
62
        return false;
63
      };
64
      return $this;
65
    }
66
67
68
    /**
69
     * @param string[] $ext
70
     * @return $this
71
     */
72
    public function extension(array $ext) {
73
      $this->callback[] = function (File $file) use ($ext) {
74
        return (in_array($file->getExtension(), $ext));
75
      };
76
      return $this;
77
    }
78
79
80
    /**
81
     * @param array|string $status
82
     * @return $this
83
     */
84
    public function status($status) {
85
      $this->callback[] = function (File $file) use ($status) {
86
        $status = (array) $status;
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $status, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
87
        return in_array($file->getStatus(), $status);
88
      };
89
    }
90
91
92
    /**
93
     * @return $this
94
     */
95
    public function notDeleted() {
96
      $this->status([
97
        File::STATUS_ADDED,
98
        File::STATUS_COPIED,
99
        File::STATUS_RENAMED,
100
        File::STATUS_MODIFIED,
101
        File::STATUS_UNKNOWN,
102
      ]);
103
      return $this;
104
    }
105
106
107
    /**
108
     * @param File $fileInfo
109
     * @return bool
110
     */
111
    public function isValid(File $fileInfo) {
112
113
      foreach ($this->callback as $callback) {
114
        if ($callback($fileInfo) === false) {
115
          return false;
116
        }
117
      }
118
119
      return true;
120
    }
121
122
  }