Completed
Push — master ( d7f072...e80481 )
by Shcherbak
05:37
created

FileFilter::extension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
  namespace Funivan\Cs\Filters;
4
5
  use Funivan\Cs\FileFinder\File;
6
7
  /**
8
   * @author Ivan Shcherbak <[email protected]> 2016
9
   */
10
  class FileFilter {
11
12
    /**
13
     * @var callable[]
14
     */
15
    private $callback = [];
16
17
18
    /**
19
     * @param string $type
20
     * @return $this
21
     */
22
    public function mimeType($type) {
23
      $this->callback[] = function (File $file) use ($type) {
24
        return (strpos($file->getMimeType(), $type) === 0);
25
      };
26
      return $this;
27
    }
28
29
30
    /**
31
     * @param string|array $regex
32
     * @return $this
33
     */
34
    public function path($regex) {
35
      $this->callback[] = function (File $file) use ($regex) {
36
        $regex = (array) $regex;
1 ignored issue
show
Bug introduced by
Consider using a different name than the imported variable $regex, 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...
37
        $path = $file->getPath();
38
39
        foreach ($regex as $reg) {
40
          if (preg_match($reg, $path)) {
41
            return true;
42
          }
43
        }
44
        return false;
45
      };
46
47
      return $this;
48
    }
49
50
51
    /**
52
     * @param string|array $regex
53
     * @return $this
54
     */
55
    public function name($regex) {
56
      $this->callback[] = function (File $file) use ($regex) {
57
        $regex = (array) $regex;
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $regex, 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...
58
        $path = $file->getName();
59
60
        foreach ($regex as $reg) {
61
          if (preg_match($reg, $path)) {
62
            return true;
63
          }
64
        }
65
        return false;
66
      };
67
      return $this;
68
    }
69
70
71
    /**
72
     * @param array|string $ext
73
     * @return $this
74
     */
75
    public function extension($ext) {
76
      $this->callback[] = function (File $file) use ($ext) {
77
        return (in_array($file->getExtension(), (array) $ext));
78
      };
79
      return $this;
80
    }
81
82
83
    /**
84
     * @param array|string $status
85
     * @return $this
86
     */
87
    public function status($status) {
88
      $this->callback[] = function (File $file) use ($status) {
89
        $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...
90
        return in_array($file->getStatus(), $status);
91
      };
92
    }
93
94
95
    /**
96
     * @return $this
97
     */
98
    public function notDeleted() {
99
      $this->status([
100
        File::STATUS_ADDED,
101
        File::STATUS_COPIED,
102
        File::STATUS_RENAMED,
103
        File::STATUS_MODIFIED,
104
        File::STATUS_UNKNOWN,
105
      ]);
106
      return $this;
107
    }
108
109
110
    /**
111
     * @param File $fileInfo
112
     * @return bool
113
     */
114
    public function isValid(File $fileInfo) {
115
116
      foreach ($this->callback as $callback) {
117
        if ($callback($fileInfo) === false) {
118
          return false;
119
        }
120
      }
121
122
      return true;
123
    }
124
125
  }