EditorModel   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 10.61 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 66
rs 10
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getFilesListByPath() 7 7 1
A getFileInfo() 0 16 2
A saveFile() 0 10 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
3
namespace Rudolf\Modules\Appearance\Editor;
4
5
use Rudolf\Framework\Model\AdminModel;
6
7
class EditorModel extends AdminModel
8
{
9
    /**
10
     * @var string
11
     */
12
    private $root;
13
14
    public function __construct()
15
    {
16
        parent::__construct();
17
18
        $this->root = THEMES_ROOT.'/front/'.FRONT_THEME.'/';
19
    }
20
21
    /**
22
     * @param string $path
23
     *
24
     * @return array
25
     */
26 View Code Duplication
    public function getFilesListByPath($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
27
    {
28
        return array_diff(
29
            scandir($this->root.$path, SCANDIR_SORT_ASCENDING),
30
            array('.', '..')
31
        );
32
    }
33
34
    /**
35
     * @param string $path
36
     *
37
     * @return array
38
     */
39
    public function getFileInfo($path)
40
    {
41
        $file = $this->root.$path;
42
43
        if (!file_exists($file)) {
44
            $file = $this->root.'templates/_head.html.php';
45
        }
46
47
        return [
48
            'name' => str_replace($this->root, '', $file),
49
            'content' => file_get_contents($file),
50
            'last-modified' => date('Y-m-d H:i:s', filemtime($file)),
51
            'size' => filesize($file),
52
            'perms' => decoct(fileperms($file) & 0777),
53
        ];
54
    }
55
56
    /**
57
     * @param string $path
58
     * @param string $data
59
     *
60
     * @return bool|int
61
     */
62
    public function saveFile($path, $data)
63
    {
64
        $file = $this->root.$path;
65
66
        if (!file_exists($file) || !is_file($file) || !is_writable($file)) {
67
            return false;
68
        }
69
70
        return file_put_contents($file, $data, LOCK_EX);
71
    }
72
}
73