SequenceOperationsAnalyzer::analyze()   B
last analyzed

Complexity

Conditions 10
Paths 30

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 31
c 1
b 0
f 0
nc 30
nop 1
dl 0
loc 43
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @copyright Copyright (c) 2018 Matthias Held <[email protected]>
5
 * @author Matthias Held <[email protected]>
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace OCA\RansomwareDetection\Analyzer;
23
24
use OCA\RansomwareDetection\Monitor;
25
26
class SequenceOperationsAnalyzer
27
{
28
    /**
29
     * Sequence operation class.
30
     *
31
     * @var int
32
     */
33
    const NO_WRITE_AND_DELETE = 0;
34
    const ONLY_WRITE = 1;
35
    const ONLY_DELETE = 2;
36
    const EQUAL_WRITE_AND_DELETE = 3;
37
    const DIFF_WRITE_AND_DELETE = 4;
38
39
    /**
40
     * Classifies the operations in a sequence.
41
     *
42
     * @param array $sequence
43
     *
44
     * @return int
45
     */
46
    public function analyze($sequence)
47
    {
48
        $numberOfWrittenFiles = 0;
49
        $numberOfDeletedFiles = 0;
50
        $numberOfRenamedFiles = 0;
51
52
        $sequenceClass = self::NO_WRITE_AND_DELETE;
53
54
        foreach ($sequence as $fileOperation) {
55
            switch ($fileOperation->getCommand()) {
56
                case Monitor::WRITE:
57
                    $numberOfWrittenFiles++;
58
                    break;
59
                case Monitor::READ:
60
                    break;
61
                case Monitor::RENAME:
62
                    $numberOfRenamedFiles++;
63
                    break;
64
                case Monitor::DELETE:
65
                    $numberOfDeletedFiles++;
66
                    break;
67
                default:
68
                    break;
69
            }
70
        }
71
72
        if ($numberOfWrittenFiles > 0) {
73
            if ($numberOfDeletedFiles > 0) {
74
                if ($numberOfWrittenFiles === $numberOfDeletedFiles) {
75
                    $sequenceClass = self::EQUAL_WRITE_AND_DELETE;
76
                } else {
77
                    $sequenceClass = self::DIFF_WRITE_AND_DELETE;
78
                }
79
            } else {
80
                $sequenceClass = self::ONLY_WRITE;
81
            }
82
        } else {
83
            if ($numberOfDeletedFiles > 0) {
84
                $sequenceClass = self::ONLY_DELETE;
85
            }
86
        }
87
88
        return $sequenceClass;
89
    }
90
}
91