Filter   A
last analyzed

Complexity

Total Complexity 41

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 41
eloc 82
dl 0
loc 150
ccs 76
cts 80
cp 0.95
rs 9.1199
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
B filtrateByFileTypes() 0 25 8
A filtrate() 0 11 4
A filtrateBySender() 0 2 1
B filtrateByAttachmentName() 0 19 7
A filtrateBySubject() 0 2 1
A filtrateByAttribute() 0 11 4
C isTemplateMatched() 0 46 16

How to fix   Complexity   

Complex Class

Complex classes like Filter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Filter, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: execut
5
 * Date: 9/27/16
6
 * Time: 3:40 PM
7
 */
8
9
namespace execut\import\components\source\adapter\email;
10
11
12
use yii\base\Component;
13
14
class Filter extends Component
15
{
16
    public $subject = null;
17
    public $sender = null;
18
    public $attachmentName = null;
19
    public $excludedFileTypes = [
20
        'jpg',
21
        'jpeg',
22
        'gif',
23
        'png',
24
        'bmp'
25
    ];
26 7
    public function filtrate($mails) {
27 7
        if (empty($this->subject) && empty($this->sender) && empty($this->attachmentName)) {
28 1
            return [];
29
        }
30
31 7
        $this->filtrateBySender($mails);
32 7
        $this->filtrateBySubject($mails);
33 7
        $this->filtrateByFileTypes($mails);
34 7
        $this->filtrateByAttachmentName($mails);
35
36 7
        return $mails;
37
    }
38
39 7
    protected function filtrateByFileTypes(&$mails) {
40 7
        if ($this->excludedFileTypes === null) {
41 1
            return;
42
        }
43
44 6
        foreach ($mails as $mail) {
45 6
            if (empty($mail->attachments)) {
46 5
                continue;
47
            }
48
49 1
            $attachments = [];
50 1
            foreach ($mail->attachments as $attachment) {
51 1
                $isExcluded = false;
52 1
                foreach ($this->excludedFileTypes as $fileType) {
53 1
                    if (strpos($attachment->fileName, '.' . $fileType) !== false) {
54 1
                        $isExcluded = true;
55
                    }
56
                }
57
58 1
                if (!$isExcluded) {
59 1
                    $attachments[] = $attachment;
60
                }
61
            }
62
63 1
            $mail->attachments = $attachments;
64
        }
65 6
    }
66
67 7
    protected function filtrateByAttachmentName(&$mails) {
68 7
        if ($this->attachmentName === null) {
69 6
            return;
70
        }
71
72 1
        foreach ($mails as $key => $mail) {
73 1
            if (empty($mail->attachments)) {
74
                continue;
75
            }
76
77 1
            $attachments = [];
78 1
            foreach ($mail->attachments as $attachment) {
79 1
                if ($this->isTemplateMatched($attachment->fileName, $this->attachmentName)) {
80 1
                    $attachments[] = $attachment;
81
                }
82
            }
83
84 1
            if (empty($attachments)) {
85 1
                unset($mails[$key]);
86
            }
87
        }
88 1
    }
89
90 7
    protected function filtrateBySender(&$mails) {
91 7
        return $this->filtrateByAttribute($mails, 'sender');
0 ignored issues
show
Bug introduced by eXeCUT
Are you sure the usage of $this->filtrateByAttribute($mails, 'sender') targeting execut\import\components...::filtrateByAttribute() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
92
    }
93
94 7
    protected function filtrateBySubject(&$mails) {
95 7
        return $this->filtrateByAttribute($mails, 'subject');
0 ignored issues
show
Bug introduced by eXeCUT
Are you sure the usage of $this->filtrateByAttribute($mails, 'subject') targeting execut\import\components...::filtrateByAttribute() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
96
    }
97
98 7
    protected function filtrateByAttribute(&$mails, $attribute) {
99 7
        if (empty($this->$attribute)) {
100 7
            return;
101
        }
102
103 6
        $subjectTemplate = $this->$attribute;
104 6
        foreach ($mails as $mailKey => $mail) {
105 6
            $subject = $mail->$attribute;
106 6
            $isMatch = $this->isTemplateMatched($subject, $subjectTemplate);
107 6
            if (!$isMatch) {
108 6
                unset($mails[$mailKey]);
109
            }
110
        }
111 6
    }
112
113
    /**
114
     * @param $subject
115
     * @param $subjectTemplate
116
     * @return bool
117
     */
118 7
    protected function isTemplateMatched($subject, $subjectTemplate)
119
    {
120 7
        $isMatch = true;
121 7
        $subject = mb_strtolower($subject);
122 7
        $subject = trim($subject);
123 7
        $subjectTemplate = mb_strtolower($subjectTemplate);
124 7
        if (strpos($subjectTemplate, '*') !== false) {
125 4
            $parts = explode('*', $subjectTemplate);
126 4
            $prevMatchPos = false;
127 4
            foreach ($parts as $key => $part) {
128 4
                if (empty($part) && $key !== 0) {
129 2
                    if ($key === (count($parts) - 1)) {
130 2
                        break;
131
                    }
132
133
                    $matchPos = 0;
134
                } else {
135 4
                    if (empty($part) && $key === 0) {
136 1
                        $matchPos = 0;
137
                    } else {
138 4
                        $matchPos = strpos($subject, $part);
139
                    }
140
141 4
                    if ($key === 0 && $matchPos !== 0) {
142 2
                        $isMatch = false;
143 2
                        break;
144
                    }
145
                }
146
147 4
                if ($matchPos === false || ($prevMatchPos !== false && $matchPos < $prevMatchPos)) {
148
                    $isMatch = false;
149
                    break;
150
                }
151
152 4
                if ($key === (count($parts) - 1) && (strlen($subject) > (strlen($part) + $matchPos))) {
153 1
                    $isMatch = false;
154 1
                    break;
155
                }
156
157 4
                $prevMatchPos = $matchPos;
158
            }
159 4
        } else if ($subject !== $subjectTemplate) {
160 2
            $isMatch = false;
161
        }
162
163 7
        return $isMatch;
164
    }
165
}