Completed
Push — master ( f480bf...21d5d4 )
by Patrick
03:35
created

ThemeDB   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 158
Duplicated Lines 17.72 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 28
loc 158
rs 10
c 0
b 0
f 0
wmc 30
lcom 2
cbo 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCurrentYear() 0 13 3
B getAllFromCollection() 0 23 6
B searchFromCollection() 0 22 5
A getObjectFromCollectionByID() 10 10 2
A addObjectToCollection() 11 11 2
A updateObjectInCollection() 0 12 2
A deleteObjectFromCollection() 7 7 1
A getAllThemes() 0 4 1
A getAllThemesForUser() 0 4 1
A getThemeByID() 0 4 1
A deleteTheme() 0 4 1
A addTheme() 0 10 2
A updateTheme() 0 11 2

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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 3 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
require_once("/var/www/secure_settings/class.FlipsideSettings.php");
3
class ThemeDB
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private $dataSet;
6
    private $tables = array();
7
    
8
    function __construct()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
9
    {
10
        $this->dataSet = \DataSetFactory::getDataSetByName('registration');
11
    }
12
13
    function getCurrentYear()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
14
    {
15
        if(!isset($this->tables['vars']))
16
        {
17
            $this->tables['vars'] = $this->dataSet['vars'];
18
        }
19
        $data = $this->tables['vars']->read(new \Data\Filter('name eq year'));
20
        if(empty($data))
21
        {
22
            return false;
23
        }
24
        return $data[0]['value'];
25
    }
26
27
    function getAllFromCollection($collection, $year = false, $uid = false, $fields = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
28
    {
29
        if($year === false)
30
        {
31
            $year = $this->getCurrentYear();
32
        }
33
        $table = $this->dataSet[$collection];
34
        $filter = false;
35
        if($year !== '*' && $uid !== false)
36
        {
37
            $filter = new \Data\Filter("year eq $year and registrars eq $uid");
38
        }
39
        else if($year !== '*')
40
        {
41
            $filter = new \Data\Filter("year eq $year");
42
        }
43
        else if($uid !== false)
44
        {
45
            $filter = new \Data\Filter("registrars eq $uid");
46
        }
47
        $data = $table->read($filter, $fields);
48
        return $data;
49
    }
50
51
    function searchFromCollection($collection, $criteria, $fields = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
    {
53
        $col = $this->db->selectCollection($collection);
0 ignored issues
show
Bug introduced by
The property db does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
54
        foreach($criteria as $key=>$value)
55
        {
56
            if($value[0] === '/')
57
            {
58
                $criteria[$key] = array('$regex'=>new MongoRegex("$value"));
59
            }
60
        }
61
        $cursor = $col->find($criteria);
62
        if($fields !== false)
63
        {
64
            $cursor->fields($fields);
65
        }
66
        $ret    = array();
67
        foreach($cursor as $doc)
68
        {
69
            array_push($ret, $doc);
70
        }
71
        return $ret;
72
    }
73
74 View Code Duplication
    function getObjectFromCollectionByID($collection, $id)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
75
    {
76
        $table = $this->dataSet[$collection];
77
        $data = $table->read(new \Data\Filter("_id eq $id"));
78
        if(empty($data))
79
        {
80
            return false;
81
        }
82
        return $data[0];
83
    }
84
85 View Code Duplication
    function addObjectToCollection($collection, $obj)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
86
    {
87
         unset($obj['_id']);
88
         $table = $this->dataSet[$collection];
89
         $res = $table->create($obj);
90
         if($res !== false)
91
         {
92
             return $res;
93
         }
94
         return false;
95
    }
96
97
    function updateObjectInCollection($collection, $obj)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
98
    {
99
        $id = $obj['_id'];
100
        unset($obj['_id']);
101
        $table = $this->dataSet[$collection];
102
        $res = $table->update(new \Data\Filter("_id eq $id"), $obj);
103
        if($res !== false)
104
        {
105
            return true;
106
        }
107
        return false;
108
    }
109
110 View Code Duplication
    function deleteObjectFromCollection($collection, $obj)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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...
111
    {
112
        $id = $obj['_id'];
113
        unset($obj['_id']);
114
        $table = $this->dataSet[$collection];
115
        return $table->delete(new \Data\Filter("_id eq $id"));
116
    }
117
118
    function getAllThemes($year = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
119
    {
120
        return $this->getAllFromCollection('themes', $year);
121
    }
122
123
    function getAllThemesForUser($uid, $year = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
124
    {
125
        return $this->getAllFromCollection('themes', $year, $uid);
126
    }
127
128
    function getThemeByID($id)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
129
    {
130
        return $this->getObjectFromCollectionByID('themes', $id);
131
    }
132
133
    function deleteTheme($theme)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
134
    {
135
        return $this->db->themes->remove(array('_id'=>new MongoId($theme['_id'])));
136
    }
137
138
    function addTheme($theme)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
139
    {
140
         unset($theme['_id']);
141
         $res = $this->db->themes->insert($theme);
142
         if($res['ok'] === true)
143
         {
144
             return $theme['_id'];
145
         }
146
         return false;
147
    }
148
149
    function updateTheme($theme)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
150
    {
151
        $id = new MongoId($theme['_id']);
152
        unset($theme['_id']);
153
        $res = $this->db->themes->update(array('_id' => $id), $theme);
154
        if($res['ok'] === true)
155
        {
156
            return true;
157
        }
158
        return false;
159
    } 
160
}
161
// vim: set tabstop=4 shiftwidth=4 expandtab:
162
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
163