Completed
Push — master ( a9e755...6c6dd7 )
by Bret R.
07:17
created

FileCheck   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 9.62 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 5
loc 52
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isWritable() 0 5 1
A check() 5 19 4

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
namespace BretRZaun\StatusPage\Check;
3
4
use BretRZaun\StatusPage\Result;
5
6
/**
7
 * checks if the given file exists
8
 *
9
 * Options:
10
 * - *writable*: checks if the file is writable
11
 *
12
 * @package BretRZaun\StatusPage\Check
13
 */
14
class FileCheck extends AbstractCheck
15
{
16
17
    /**
18
     * @var string
19
     */
20
    protected $filename;
21
22
    /**
23
     * @var array
24
     */
25
    protected $options;
26
27
    /**
28
     * FileCheck constructor.
29
     * @param $label
30
     * @param $filename
31
     * @param array $options
32
     */
33
    public function __construct($label, $filename, array $options = [])
34
    {
35
        parent::__construct($label);
36
        $this->filename = $filename;
37
        $this->options = $options;
38
    }
39
40
    public function isWritable()
41
    {
42
        $this->options['writable'] = true;
43
        return $this;
44
    }
45
46
    public function check(): Result
47
    {
48
        $result = new Result($this->label);
49
50 View Code Duplication
        if (!file_exists($this->filename)) {
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...
51
            $result->setSuccess(false);
52
            $result->setError("$this->filename does not exist!");
53
            return $result;
54
        }
55
56
        if (array_key_exists('writable', $this->options)) {
57
            if (!is_writable($this->filename)) {
58
                $result->setSuccess(false);
59
                $result->setError("$this->filename ist not writable!");
60
            }
61
        }
62
63
        return $result;
64
    }
65
}
66