GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Model   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 7.03 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A pluralize() 0 11 2
A deleteById() 9 9 2
A errors() 0 3 1
A exists() 0 7 1
A countAll() 0 5 1
B applyOptions() 0 11 5

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
/**
4
 * Model Class
5
 *
6
 * Main/Super class for model classes
7
 *
8
 * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
9
 * @author     Omar El Gabry <[email protected]>
10
 */
11
12
class Model {
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...
13
14
    /**
15
     * Table name for the Model.
16
     *
17
     * @var string
18
    */
19
    public $table = false;
20
21
    /**
22
     * Array of validation errors
23
     *
24
     * @var array
25
     */
26
    protected $errors = [];
27
28
    /**
29
     * Constructor
30
     *
31
     */
32
    public function __construct(){
33
        if($this->table === false){
34
            $this->table = $this->pluralize(get_class($this));
35
        }
36
    }
37
38
    /**
39
     * pluralize for table names
40
     *
41
     * Automatically selects a database table name based on a pluralized lowercase object class name
42
     * (i.e. class 'User' => table 'users')
43
     *
44
     * @param   string $word
45
     * @return  string
46
     */
47
    private function pluralize($word){
48
49
        $word = strtolower($word);
50
        $plural = [
51
            "newsfeed" => "newsfeed",
52
            "man" => "men",
53
            "woman" => "women"
54
        ];
55
56
        return isset($plural[$word])? $plural[$word]: $word . "s";
57
    }
58
59
    /**
60
     * delete record by id
61
     *
62
     * @param  string $id
63
     * @return bool
64
     * @throws Exception if feed couldn't be deleted
65
     */
66 View Code Duplication
    public function deleteById($id){
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...
67
68
        $database = Database::openConnection();
69
        $database->deleteById($this->table, $id);
70
71
        if($database->countRows() !== 1){
72
            throw new Exception ("Couldn't delete news feed");
73
        }
74
    }
75
76
    /**
77
     * get errors
78
     *
79
     * @return array errors
80
     */
81
    public function errors(){
82
        return $this->errors;
83
    }
84
85
    /**
86
     * is record exists?
87
     *
88
     * Almost all methods in model needs you to pass the current user id,
89
     * Another approach is to create in Model class a property call id(will be inherited by all extending classes)
90
     * Ex:
91
     *  Inside postsController:
92
     *  post->id = $postId
93
     *  post->updatePost(....) -> Inside updatePost() you can get the post id by: $this->id
94
     *
95
     * @param  string  $id
96
     * @return bool
97
     */
98
    public function exists($id){
99
100
        $database = Database::openConnection();
101
        $database->getById($this->table, $id);
102
103
        return $database->countRows() === 1;
104
    }
105
106
    /**
107
     * Counting the number of a current model's table.
108
     *
109
     * @return integer
110
     */
111
    public function countAll(){
112
113
        $database = Database::openConnection();
114
        return $database->countAll($this->table);
115
    }
116
117
    /**
118
     * adds parts to current query using an array.
119
     *
120
     * The array will have $key which is the field value, and the $value is the query to be added to the current query
121
     * Only fields with existing value(false, and 0 are accepted as valid values) will be considered in our query.
122
     *
123
     * @param  array  $options
124
     * @param  string $implodeBy
125
     * @return string
126
     */
127
    public function applyOptions(array $options, $implodeBy){
128
129
        $queries = [];
130
131
        foreach($options as $key => $value){
132
            if(!empty($key) || $key === false || $key === 0){
133
                $queries[] = $value;
134
            }
135
        }
136
        return implode($implodeBy, $queries);
137
    }
138
139
}
140