Completed
Push — develop ( b91125...52c290 )
by Seth
03:00
created

Filter::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace smtech\CanvasICSSync\SyncIntoCanvas;
4
5
class Filter
6
{
7
    /**
8
     * Whether or not this filter is enabled
9
     * @var boolean
10
     */
11
    protected $enabled = false;
12
13
    /**
14
     * Regex to include (applied first)
15
     * @var string
16
     */
17
    protected $include;
18
19
    /**
20
     * Regex to exclude (applied second)
21
     * @var string
22
     */
23
    protected $exclude;
24
25
    public function __construct($enabled, $include, $exclude)
26
    {
27
        $this->setEnabled($enabled);
28
        $this->setIncludeExpression($include);
29
        $this->setExcludeExpression($exclude);
30
    }
31
32
    public function setEnabled($enabled)
33
    {
34
        $this->enabled = (boolean) $enabled;
35
    }
36
37
    public function isEnabled()
38
    {
39
        return $enabled;
0 ignored issues
show
Bug introduced by
The variable $enabled does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
40
    }
41
42
    public function setIncludeExpression($regex)
43
    {
44
        $this->include = (string) $regex;
45
    }
46
47
    public function getIncludeExpression()
48
    {
49
        if (empty($this->include)) {
50
            return false;
51
        }
52
        return $this->include;
53
    }
54
55
    public function setExcludeExpression($regex)
56
    {
57
        $this->exclude = (string) $regex;
58
    }
59
60
    public function getExcludeExpression()
61
    {
62
        if (empty($this->exclude)) {
63
            return false;
64
        }
65
        return $this->exclude;
66
    }
67
68
    public function filter(Event $event)
69
    {
70
        return (
71
            // include this event if filtering is off...
72
            $this->isEnabled() == false ||
73
            (
74
               (
75
                   ( // if filtering is on, and there's an include pattern test that pattern...
76
                       $this->getIncludeExpression() &&
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getIncludeExpression() of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
77
                       preg_match('%' . $this->getIncludeExpression() . '%', $event->getProperty('SUMMARY'))
0 ignored issues
show
Bug introduced by
The method getProperty() does not seem to exist on object<smtech\CanvasICSSync\SyncIntoCanvas\Event>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
                   )
79
               ) &&
80
               !( // if there is an exclude pattern, make sure that this event is NOT excluded
81
                   $this->getExcludeExpression() &&
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getExcludeExpression() of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
82
                   preg_match('%' . $this->getExcludeExpression() . '%', $event->getProperty('SUMMARY'))
0 ignored issues
show
Bug introduced by
The method getProperty() does not seem to exist on object<smtech\CanvasICSSync\SyncIntoCanvas\Event>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
               )
84
           )
85
        );
86
    }
87
}
88