Passed
Push — develop ( fc7043...b7cd40 )
by Jens
02:53
created

Document   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 84
Duplicated Lines 5.95 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 5
loc 84
rs 10
wmc 14
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __get() 0 21 7
A __set() 0 10 3
B getContent() 5 25 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
 * User: Jens
4
 * Date: 17-2-2017
5
 * Time: 13:00
6
 */
7
8
namespace library\storage;
9
10
11
class Document
12
{
13
    public $id;
14
    public $path;
15
    public $title;
16
    public $slug;
17
    public $type;
18
    public $documentType;
19
    public $documentTypeSlug;
20
    public $state;
21
    public $lastModificationDate;
22
    public $creationDate;
23
    public $lastModifiedBy;
24
    protected $fields;
25
    protected $bricks;
26
    protected $dynamicBricks;
27
    protected $content;
28
29
    protected $dbHandle;
30
31
    protected $jsonEncodedFields = array('fields', 'bricks', 'dynamicBricks');
32
33
    public function __get($name) {
34
        if (in_array($name, $this->jsonEncodedFields)) {
35
            if (is_string($this->$name)) {
36
                return json_decode($this->$name);
37
            } else {
38
                return $this->$name;
39
            }
40
        } elseif ($name === 'content') {
41
            if ($this->dbHandle === null) {
42
                throw new \Exception('Document doesnt have a dbHandle handle. (path: ' . $this->path . ')');
43
            } else {
44
                if ($this->content === null) {
45
                    $this->getContent();
46
                }
47
                return $this->content;
48
            }
49
        } elseif ($name === 'repository') {
50
            throw new \Exception('Trying to get protected property repository.');
51
        }
52
        return $this->$name;
53
    }
54
55
    public function __set($name, $value) {
56
        if (in_array($name, $this->jsonEncodedFields)) {
57
            $this->$name = json_encode($value);
58
        } elseif ($name === 'content') {
59
            // Dont do anything for now..
60
            return;
61
        }
62
63
        $this->$name = $value;
64
    }
65
66
    /**
67
     * @throws \Exception
68
     */
69
    protected function getContent()
70
    {
71
        $folderPathWithWildcard = $this->path . '%';
72
        $sql = '    SELECT *
73
                      FROM documents
74
                     WHERE `path` LIKE ' . $this->dbHandle->quote($folderPathWithWildcard) . '
75
                       AND substr(`path`, ' . (strlen($this->path) + 2) . ') NOT LIKE "%/%"
76
                       AND substr(`path`, ' . (strlen($this->path) + 1) . ', 1) = "/"
77
                       AND path != ' . $this->dbHandle->quote($this->path) . '
78
                    ';
79
        $stmt = $this->dbHandle->query($sql);
80 View Code Duplication
        if ($stmt === false) {
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...
81
            $errorInfo = $this->dbHandle->errorInfo();
82
            $errorMsg = $errorInfo[2];
83
            throw new \Exception('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
84
        }
85
        $contents = $stmt->fetchAll(\PDO::FETCH_CLASS, '\library\storage\Document');
86
        foreach ($contents as $key => $document) {
87
            if ($document->type === 'folder') {
88
                $document->dbHandle = $this->dbHandle;
89
                $contents[$key] = $document;
90
            }
91
        }
92
        $this->content = $contents;
93
    }
94
}