Completed
Push — master ( ca5b5d...2e03d0 )
by Nils
03:27
created

FilterExtension   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 9
c 3
b 0
f 1
lcom 1
cbo 3
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 18 5
A isFiltered() 0 11 4
1
<?php
2
3
namespace whm\Smoke\Extensions\SmokeFilter;
4
5
use phmLabs\Components\Annovent\Event\Event;
6
use whm\Smoke\Http\Response;
7
use whm\Smoke\Yaml\EnvAwareYaml;
8
9
class FilterExtension
10
{
11
    private $filters = array();
12
13
    public function init($filters = array(), $filterFile = "")
14
    {
15
        if ($filterFile != "") {
16
            if (!file_exists($filterFile)) {
17
                throw new \RuntimeException('Filter file not found: ' . $filterFile);
18
            }
19
20
            $filterElements = EnvAwareYaml::parse(file_get_contents($filterFile));
21
22
            foreach ($filterElements as $rule => $uris) {
0 ignored issues
show
Bug introduced by
The expression $filterElements of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
23
                foreach ($uris as $uri) {
24
                    $this->filters[] = array("rule" => $rule, "uri" => $uri);
25
                }
26
            }
27
        } else {
28
            $this->filters = $filters;
29
        }
30
    }
31
32
    /**
33
     * @Event("Scanner.CheckResponse.isFiltered")
34
     */
35
    public function isFiltered(Event $event, $ruleName, Response $response)
36
    {
37
        foreach ($this->filters as $filter) {
38
            if ($ruleName === $filter['rule'] && 0 < preg_match($filter['uri'], (string)$response->getUri())) {
39
                $event->setProcessed();
40
                return true;
41
            }
42
        }
43
44
        return false;
45
    }
46
}
47