Completed
Push — master ( 8f477d...f5ebb0 )
by Tim
06:15 queued 04:03
created

FreeSpace   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 15.63 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 10
loc 64
rs 10
wmc 6
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 6 1
A setPath() 0 5 1
A getPath() 0 5 1
A invokeTest() 10 23 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SimpleSAML\Module\monitor\TestCase\FileSystem;
4
5
use \SimpleSAML\Module\monitor\State as State;
6
use \SimpleSAML\Module\monitor\TestData as TestData;
7
use \SimpleSAML\Module\monitor\TestResult as TestResult;
8
9
final class FreeSpace extends \SimpleSAML\Module\monitor\TestCaseFactory
10
{
11
    /**
12
     * @var string|null
13
     */
14
    private $path = null;
15
16
    /**
17
     * @var TestData $testData
18
     *
19
     * @return void
20
     */
21
    protected function initialize($testData)
22
    {
23
        $this->setPath($testData->getInput('path'));
24
        $this->setCategory($testData->getInput('category'));
25
        parent::initialize($testData);
26
    }
27
28
    /**
29
     * @return void
30
     */
31
    private function setPath($path)
32
    {
33
        assert(is_string($path));
34
        $this->path = $path;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    private function getPath()
41
    {
42
        assert(is_string($this->path));
43
        return $this->path;
44
    }
45
46
    /**
47
     * @return void
48
     */
49
    protected function invokeTest()
50
    {
51
        $path = $this->getPath();
52
        $testResult = new TestResult($this->getCategory(), $path);
53
54
        $size = disk_total_space($path);
55
        $used = $size - disk_free_space($path);
56
        $free = round(100 - (($used / $size) * 100));
57
58 View Code Duplication
        if ($free >= 15) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
            $testResult->setMessage($free . '% free space');
60
            $testResult->setState(State::OK);
61
        } else if ($free < 5) {
62
            $testResult->setMessage('Critical: ' . $free . '% free space');
63
            $testResult->setState(State::ERROR);
64
        } else {
65
            $testResult->setMessage($free . '% free space');
66
            $testResult->setState(State::WARNING);
67
        }
68
69
        $testResult->addOutput($free, 'free_percentage');
0 ignored issues
show
Bug introduced by
The method addOutput() does not seem to exist on object<SimpleSAML\Module\monitor\TestResult>.

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...
70
        $this->setTestResult($testResult);
71
    }
72
}
73