Issues (195)

lib/Service/DetectionService.php (6 issues)

1
<?php
2
/**
3
 * @copyright Copyright (c) 2019 Matthias Held <[email protected]>
4
 * @author Matthias Held <[email protected]>
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace OCA\RansomwareDetection\Service;
22
23
use OCA\RansomwareDetection\AppInfo\Application;
24
use OCA\RansomwareDetection\Model\Detection;
25
use OCA\RansomwareDetection\Model\DetectionDeserializer;
26
use OCA\RansomwareDetection\Classifier;
27
use OCA\RansomwareDetection\Analyzer\SequenceAnalyzer;
28
use OCP\IConfig;
0 ignored issues
show
The type OCP\IConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use OCP\ILogger;
0 ignored issues
show
The type OCP\ILogger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
31
class DetectionService {
32
33
    /** @var ILogger */
34
    protected $logger;
35
36
    /** @var FileOperationService */
37
    protected $service;
38
39
    /** @var DetectionDeserializer */
40
    protected $deserializer;
41
42
    /** @var IConfig */
43
    protected $config;
44
45
    /** @var Classifier */
46
    protected $classifier;
47
48
    /** @var string */
49
    protected $userId;
50
51
    /**
52
     * @param ILogger              $logger
53
     * @param FileOperationService  $service
54
     * @param IConfig               $config
55
     * @param Classifier            $classifier
56
     * @param SequenceAnalyzer      $sequenceAnalyzer
57
     * @param string                $userId
58
     */
59
    public function __construct(
60
        ILogger $logger,
61
        FileOperationService $service,
62
        DetectionDeserializer $deserializer,
63
        IConfig $config,
64
        Classifier $classifier,
65
        SequenceAnalyzer $sequenceAnalyzer,
66
        $userId
67
    ) 
68
    {
69
        $this->logger = $logger;
70
        $this->service = $service;
71
        $this->deserializer = $deserializer;
72
        $this->config = $config;
73
        $this->classifier = $classifier;
74
        $this->sequenceAnalyzer = $sequenceAnalyzer;
0 ignored issues
show
Bug Best Practice introduced by
The property sequenceAnalyzer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
75
        $this->userId = $userId;
76
    }
77
78
    public function getDetections() {
79
        $files = $this->service->findAll();
80
81
        $sequences = array();
82
        $detectionObjects = array();
83
84
        // Classify files and put together the sequences.
85
        foreach ($files as $file) {
86
            $this->classifier->classifyFile($file);
87
            $sequences[$file->getSequence()][] = $file;
88
        }
89
90
        foreach ($sequences as $id => $sequence) {
91
            if (sizeof($sequence) >= $this->config->getAppValue(Application::APP_ID, 'minimum_sequence_length', 0)) {
92
                usort($sequence, function ($a, $b) {
93
                    return $b->getId() - $a->getId();
94
                });
95
                $result = $this->sequenceAnalyzer->analyze($id, $sequence);
96
                $this->logger->debug('detection: suspicion score of sequence ' . $id . ' is ' . $result->getSuspicionScore() . '.', array('app' => Application::APP_ID));
97
                if ($result->getSuspicionScore() >= 0.5) {
98
                    $detection = new Detection($id, $sequence);
99
                    array_push($detectionObjects, $detection);
100
                }
101
            }
102
        }
103
        usort($detectionObjects, function ($a, $b) {
104
            return $b->getId() - $a->getId();
105
        });
106
        return $detectionObjects;
107
    }
108
109
    public function getDetection($id) {
0 ignored issues
show
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

109
    public function getDetection(/** @scrutinizer ignore-unused */ $id) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
        return DetectionSerializer::deserialize(json_decode(new Detection(1, array())));
0 ignored issues
show
The type OCA\RansomwareDetection\...ice\DetectionSerializer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
new OCA\RansomwareDetect...l\Detection(1, array()) of type OCA\RansomwareDetection\Model\Detection is incompatible with the type string expected by parameter $json of json_decode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
        return DetectionSerializer::deserialize(json_decode(/** @scrutinizer ignore-type */ new Detection(1, array())));
Loading history...
111
    }
112
}