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.

Post::getById()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 15
loc 15
rs 9.4285
cc 1
eloc 11
nc 1
nop 1
1
<?php
2
3
 /**
4
  * Post Class
5
  *
6
  * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
7
  * @author     Omar El Gabry <[email protected]>
8
  */
9
10
 class Post extends 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...
11
12
     /**
13
      * get all posts
14
      *
15
      * @access public
16
      * @param  integer  $pageNum
17
      * @return array    Associative array of the posts, and Pagination Object.
18
      *
19
      */
20 View Code Duplication
     public function getAll($pageNum = 1){
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...
21
22
         $pagination = Pagination::pagination("posts", "", [], $pageNum);
23
         $offset     = $pagination->getOffset();
24
         $limit      = $pagination->perPage;
25
26
         $database   = Database::openConnection();
27
         $query  = "SELECT posts.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, posts.title, posts.content, posts.date ";
28
         $query .= "FROM users, posts ";
29
         $query .= "WHERE users.id = posts.user_id ";
30
         $query .= "ORDER BY posts.date DESC ";
31
         $query .= "LIMIT $limit OFFSET $offset";
32
33
         $database->prepare($query);
34
         $database->execute();
35
         $posts = $database->fetchAllAssociative();
36
37
         $this->appendNumberOfComments($posts, $database);
0 ignored issues
show
Unused Code introduced by
The call to Post::appendNumberOfComments() has too many arguments starting with $database.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
38
39
         return array("posts" => $posts, "pagination" => $pagination);
40
     }
41
42
     /**
43
      * append number of comments to the array of posts for each post.
44
      *
45
      * @access private
46
      * @param  array
47
      *
48
      */
49
     private function appendNumberOfComments(&$posts){
50
51
         $postId = 0;
52
         $database = Database::openConnection();
53
54
         $query  = "SELECT COUNT(*) AS comments FROM comments WHERE post_id = :post_id ";
55
         $database->prepare($query);
56
         $database->bindParam(':post_id', $postId);
57
58
         foreach($posts as $key => $post){
59
             $postId = (int)$posts[$key]["id"];
0 ignored issues
show
Unused Code introduced by
$postId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
             $database->execute();
61
             $posts[$key]["comments"] = $database->fetchAssociative()["comments"];
62
         }
63
     }
64
65
     /**
66
      * get post by Id.
67
      *
68
      * @access public
69
      * @param  integer  $postId
70
      * @return array    Array holds the data of the post
71
      */
72 View Code Duplication
     public function getById($postId){
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...
73
74
         $database = Database::openConnection();
75
         $query  = "SELECT posts.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, posts.title, posts.content, posts.date ";
76
         $query .= "FROM users, posts ";
77
         $query .= "WHERE posts.id = :id ";
78
         $query .= "AND users.id = posts.user_id LIMIT 1 ";
79
80
         $database->prepare($query);
81
         $database->bindValue(':id', $postId);
82
         $database->execute();
83
84
         $post = $database->fetchAssociative();
85
         return $post;
86
     }
87
88
     /**
89
      * create post
90
      *
91
      * @access public
92
      * @param  integer   $userId
93
      * @param  string    $title
94
      * @param  string    $content
95
      * @return bool
96
      * @throws Exception If post couldn't be created
97
      *
98
      */
99
     public function create($userId, $title, $content){
100
101
         $validation = new Validation();
102 View Code Duplication
         if(!$validation->validate([
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...
103
             'Title'   => [$title, "required|minLen(2)|maxLen(60)"],
104
             'Content'   => [$content, "required|minLen(4)|maxLen(1800)"]])) {
105
             $this->errors = $validation->errors();
106
             return false;
107
         }
108
109
         $database = Database::openConnection();
110
         $query    = "INSERT INTO posts (user_id, title, content) VALUES (:user_id, :title, :content)";
111
112
         $database->prepare($query);
113
         $database->bindValue(':user_id', $userId);
114
         $database->bindValue(':title', $title);
115
         $database->bindValue(':content', $content);
116
         $database->execute();
117
118
         if($database->countRows() !== 1){
119
             throw new Exception ("Couldn't add news feed");
120
         }
121
122
         return true;
123
     }
124
125
     /**
126
      * update Post
127
      *
128
      * @access public
129
      * @static static method
130
      * @param  string    $postId
131
      * @param  string    $title
132
      * @param  string    $content
133
      * @return array     Array of the updated post
134
      * @throws Exception If post couldn't be updated
135
      *
136
      */
137
     public function update($postId, $title, $content){
138
139
         $validation = new Validation();
140 View Code Duplication
         if(!$validation->validate([
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...
141
             'Title'   => [$title, "required|minLen(2)|maxLen(60)"],
142
             'Content' => [$content, "required|minLen(4)|maxLen(1800)"]])) {
143
             $this->errors = $validation->errors();
144
             return false;
145
         }
146
147
         $database = Database::openConnection();
148
         $query = "UPDATE posts SET title = :title, content = :content WHERE id = :id LIMIT 1";
149
150
         $database->prepare($query);
151
         $database->bindValue(':title', $title);
152
         $database->bindValue(':content', $content);
153
         $database->bindValue(':id', $postId);
154
         $result = $database->execute();
155
156
         if(!$result){
157
             throw new Exception("Couldn't update post of ID: " . $postId);
158
         }
159
160
         $post = $this->getById($postId);
161
         return $post;
162
     }
163
164
 }
165