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

ToolsFilter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 69
wmc 9
lcom 1
cbo 1
ccs 0
cts 29
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createFromString() 0 19 3
B isValid() 0 15 5
1
<?php
2
3
  namespace Funivan\Cs\FileTool;
4
5
  /**
6
   * @author Ivan Shcherbak <[email protected]> 2016
7
   */
8
  class ToolsFilter {
9
10
    /**
11
     * @var null|string[]
12
     */
13
    private $include;
14
15
    /**
16
     * @var null|string[]
17
     */
18
    private $exclude;
19
20
21
    /**
22
     * @param string[]|null $include
23
     * @param string[]|null $exclude
24
     */
25
    public function __construct(array $include = null, array $exclude = null) {
26
      $this->include = $include;
27
      $this->exclude = $exclude;
28
    }
29
30
31
    /**
32
     * @param string $filters
33
     * @param string $delimiter
34
     */
35
    public static function createFromString($filters, $delimiter = ',') {
36
      $include = null;
37
      $exclude = null;
38
39
40
      $fixerNames = explode($delimiter, $filters);
41
      $fixerNames = array_map('trim', $fixerNames);
42
      $fixerNames = array_filter($fixerNames);
43
44
      foreach ($fixerNames as $name) {
45
        if (strpos($name, '-') === 0) {
46
          $exclude[] = substr($name, 1);
47
        } else {
48
          $include[] = $name;
49
        }
50
      }
51
52
      return new self($include, $exclude);
53
    }
54
55
56
    /**
57
     * @param FileTool $tool
58
     * @return bool
59
     */
60
    public function isValid(FileTool $tool) {
61
62
      $toolName = $tool->getName();
63
64
      if (null !== $this->exclude and in_array($toolName, $this->exclude)) {
65
        return false;
66
      }
67
68
      if (null !== $this->include and !in_array($toolName, $this->include)) {
69
        return false;
70
      }
71
72
73
      return true;
74
    }
75
76
  }