Completed
Pull Request — master (#11)
by Elisha-Wigwe Chijioke
03:03
created

PotatoModel   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 170
Duplicated Lines 8.82 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 16
Bugs 6 Features 6
Metric Value
wmc 18
c 16
b 6
f 6
lcom 1
cbo 1
dl 15
loc 170
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A __set() 0 4 1
A __get() 0 8 2
A __isset() 0 4 1
A getAll() 0 7 2
A find() 7 7 2
A save() 0 4 2
A isStored() 0 4 1
A insert() 0 6 1
A update() 0 6 1
A destroy() 8 8 2
A getClassTableName() 0 7 1

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 Elchroy\PotatoORM;
4
5
class PotatoModel
6
{
7
    /**
8
     * [$queryTo The query (instance of PotatoQuery) to handle all database queries.
9
     *
10
     * @var [type] Instance of potatoQuery class, to be difined during construction.
11
     */
12
    public $queryTo;
13
14
    /**
15
     * [$dataToSave The properties of a object of theis class to be saved in the database.
16
     *
17
     * @var array This is an array of property names to be saved in to the database.
18
     */
19
    public $dataToSave = [];
20
21
    /**
22
     * [__construct The constructor to initialize public variables].
23
     *
24
     * @param PotatoQuery|null $potatoQuery The query to be used for the communication with the databse.
25
     */
26
    public function __construct(PotatoQuery $potatoQuery = null)
27
    {
28
        $this->queryTo = $potatoQuery == null ? new PotatoQuery() : $potatoQuery;
29
    }
30
31
    /**
32
     * [__set magic Property to set put of the object into the arary of properties to be saved in the database.].
33
     *
34
     * @param [type] $property [The property of the object to be saved.]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
35
     * @param [type] $value    [The value of given property to be saved.]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
36
     */
37
    public function __set($property, $value)
38
    {
39
        $this->dataToSave[$property] = $value;
40
    }
41
42
    /**
43
     * [__get Magic function to get the property from the dataToSave array, the array of properties.].
44
     *
45
     * @param [type] $property [The property to be gotten from the data to save array of propoerties.]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
46
     *
47
     * @return [type] [If the property is cound, return the value of that property. Otherwise throw an exception]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
48
     */
49
    public function __get($property)
50
    {
51
        if (array_key_exists($property, $this->dataToSave)) {
52
            return $this->dataToSave[$property];
53
        }
54
55
        return "$property not found.";
56
    }
57
58
    /**
59
     * [__isset Magic function to check if the given propertyh of the object is set.].
60
     *
61
     * @param [type] $property [The property in question]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
62
     *
63
     * @return bool True if the property exists. False otherwise.
64
     */
65
    public function __isset($property)
66
    {
67
        return isset($this->dataToSave[$property]);
68
    }
69
70
    /**
71
     * [getAll Get all records of database table corresponding the the called class name.
72
     *
73
     * @param [type] $query [The query to be used to communicated with the databse.]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
74
     *
75
     * @return [type] Return the returned value of the query's query. The return value is an array of records represented as objects.
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
76
     */
77
    public static function getAll($query = null)
78
    {
79
        $query = $query == null ? new PotatoQuery() : $query;
80
        $table = self::getClassTableName();
81
82
        return $query->getFrom($table);
83
    }
84
85
    /**
86
     * [find Find on record or row in the table given by an ID.
87
     *
88
     * @param [type] $id    [the ID of the row record to be fetched.]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
89
     * @param [type] $query [The query to be used for the database communication.]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
90
     *
91
     * @return [type] [The returned value of finding the record with the given ID.]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
92
     */
93 View Code Duplication
    public static function find($id, $query = null)
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...
94
    {
95
        $query = $query == null ? new PotatoQuery() : $query;
96
        $table = self::getClassTableName();
97
98
        return $query->getOne($table, $id);
99
    }
100
101
    /**
102
     * SAve a recrd into the database. Saveing is either inserting or updating.
103
     *
104
     * @return [type] [A newly inserted record ot a recently updated record in the database.]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
105
     */
106
    public function save()
107
    {
108
        return $this->isStored() ? $this->update() : $this->insert();
109
    }
110
111
    /**
112
     * Check if the object to be saved has an ID.
113
     *
114
     * @return bool [description]
115
     */
116
    public function isStored()
117
    {
118
        return isset($this->id);
119
    }
120
121
    /**
122
     * Insert a new record into the database while communicating with the qury handler.
123
     *
124
     * @return [type] [The returned value of the quering the databse.]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
125
     */
126
    public function insert()
127
    {
128
        $table = self::getClassTableName();
129
130
        return $this->queryTo->storeIn($table, $this->dataToSave);
0 ignored issues
show
Documentation introduced by
$this->dataToSave is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
131
    }
132
133
    /**
134
     * Update a record in a database. The record must have an ID before it can be updated.
135
     *
136
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
137
     */
138
    public function update()
139
    {
140
        $table = self::getClassTableName();
141
142
        return $this->queryTo->updateAt($table, $this->dataToSave);
143
    }
144
145
    /**
146
     * Destroy a recored or row from a database table.
147
     *
148
     * @param [type] $id    [The ID of the record to be deleted.]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
149
     * @param [type] $query [The query to handle to deleting operation]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
150
     *
151
     * @return [type] [An array of all the records in that table without the deleted recode.]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
152
     */
153 View Code Duplication
    public static function destroy($id, $query = null)
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...
154
    {
155
        $query = $query == null ? new PotatoQuery() : $query;
156
        $table = self::getClassTableName();
157
        $query->deleteFrom($table, $id);
158
159
        return self::getAll($query); // return all the items after the previous item has been deleted.
160
    }
161
162
    /**
163
     * [getClassTableName Get the name of the class from which this function is called, including classes that inherit from this class.].
164
     *
165
     * @return [type] [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
166
     */
167
    public static function getClassTableName()
168
    {
169
        $tableWithNameSpance = strtolower(get_called_class());
170
        $table = explode('\\', $tableWithNameSpance);
171
172
        return end($table);
173
    }
174
}
175