Conditions | 19 |
Paths | 119 |
Total Lines | 74 |
Code Lines | 48 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
44 | public function analyze($sequence) |
||
45 | { |
||
46 | $writtenExtensions = []; |
||
47 | $deletedExtensions = []; |
||
48 | $corruptedFiles = []; |
||
49 | $writtenFiles = []; |
||
50 | $numberOfKnownFileExtensions = 0; |
||
51 | $numberOfInfoFiles = 0; |
||
52 | |||
53 | foreach ($sequence as $file) { |
||
54 | if ($file->getType() === 'file') { |
||
55 | switch ($file->getCommand()) { |
||
56 | case Monitor::WRITE: |
||
57 | if ($file->getSuspicionClass() === Classifier::NOT_SUSPICIOUS) { |
||
58 | if ($numberOfInfoFiles < SequenceAnalyzer::NUMBER_OF_INFO_FILES) { |
||
59 | $numberOfInfoFiles++; |
||
60 | break; |
||
61 | } |
||
62 | } |
||
63 | $numberOfKnownFileExtensions += $this->countKnownFileExtensions($file); |
||
64 | $pathInfo = pathinfo($file->getOriginalName()); |
||
65 | if (isset($pathInfo['extension'])) { |
||
66 | $writtenExtensions[$pathInfo['extension']] = 1; |
||
67 | $writtenFiles[] = $file; |
||
68 | } |
||
69 | if ($file->getCorrupted()) { |
||
70 | $corruptedFiles[] = $file; |
||
71 | } |
||
72 | break; |
||
73 | case Monitor::READ: |
||
74 | break; |
||
75 | case Monitor::RENAME: |
||
76 | break; |
||
77 | case Monitor::DELETE: |
||
78 | $pathInfo = pathinfo($file->getOriginalName()); |
||
79 | if (isset($pathInfo['extension'])) { |
||
80 | $deletedExtensions[] = $pathInfo['extension']; |
||
81 | } |
||
82 | break; |
||
83 | case Monitor::CREATE: |
||
84 | break; |
||
85 | default: |
||
86 | break; |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | |||
91 | // File type funneling must be at least 2 files |
||
92 | if (sizeof($writtenFiles) > 2) { |
||
93 | // Some files were written |
||
94 | if ($numberOfKnownFileExtensions === 0) { |
||
|
|||
95 | if (sizeof($writtenExtensions) === sizeof($writtenFiles)) { |
||
96 | // All files have distinct unknown extensions |
||
97 | return 2; |
||
98 | } |
||
99 | if (sizeof($writtenExtensions) === 1) { |
||
100 | // All files have the same extension |
||
101 | return 2; |
||
102 | } |
||
103 | // All file extensions are unknown |
||
104 | return 1; |
||
105 | } elseif ($numberOfKnownFileExtensions === sizeof($writtenFiles)) { |
||
106 | if ($numberOfKnownFileExtensions === sizeof($corruptedFiles)) { |
||
107 | // All files are corrupted |
||
108 | return 2; |
||
109 | } |
||
110 | // All written files have known extensions |
||
111 | return 0; |
||
112 | } |
||
113 | // Some files are known |
||
114 | return 0; |
||
115 | } |
||
116 | |||
117 | return 0; |
||
118 | } |
||
132 |