Completed
Push — master ( eb5763...da62cd )
by Anthony
02:23
created

AdminComment   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 10.87 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 1
dl 10
loc 92
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getValues() 0 3 1
A getAllTable() 0 19 4
A getAllComment() 10 20 3
A getNbCheckedComment() 0 8 1
A setValues() 0 3 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
	namespace modules\comment\admin\controller;
3
	
4
	use core\App;
5
	use modules\comment\app\controller\Comment;
6
	
7
	class AdminComment extends Comment {
8
		private $values = [];
9
		
10
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
11
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
12
		
13
		
14
		//-------------------------- GETTER ----------------------------------------------------------------------------//
15
		/**
16
		 * @return array
17
		 * function to get values out of the class
18
		 */
19
		public function getValues(){
20
			return ["comment" => $this->values];
21
		}
22
		
23
		/**
24
		 * function that get a list of all table_name wich are in comment module
25
		 */
26
		public function getAllTable() {
27
			$dbc = App::getDb();
28
			
29
			$query = $dbc->select("table_name, ID_in_table")->from("_comment_all")->groupBy("ID_in_table, table_name")->get();
30
			
31
			if ((is_array($query)) && (count($query) > 0)) {
32
				$values = [];
33
				foreach ($query as $obj) {
34
					$values[] = [
35
						"table_name" => str_replace("_", " ", $obj->table_name),
36
						"id_in_table" => $obj->ID_in_table,
37
						"nb_checked_comment" => $this->getNbCheckedComment($obj->table_name, 1),
38
						"nb_unchecked_comment" => $this->getNbCheckedComment($obj->table_name, 0),
39
					];
40
				}
41
				
42
				$this->setValues($values);
43
			}
44
		}
45
		
46
		/**
47
		 * @param $table
48
		 * @param $id_in_table
49
		 * function to get all comments of a table with an id
50
		 */
51
		public function getAllComment($table, $id_in_table) {
52
			$dbc = App::getDb();
53
			
54
			$query = $dbc->select()->from("_comment_all")->where("table_name", "=", $table, "AND")
55
				->where("ID_in_table", "=", $id_in_table)->orderBy("checked", "DESC")->get();
56
			
57
			$values = [];
58 View Code Duplication
			if (count($query) > 0) {
1 ignored issue
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...
59
				foreach ($query as $obj) {
60
					$values[] = [
61
						"comment" => $obj->comment,
62
						"date" => $obj->date,
63
						"pseudo" => $obj->pseudo,
64
						"id_identite" => $obj->ID_identite,
65
					];
66
				}
67
			}
68
			
69
			$this->setValues($values);
70
		}
71
		
72
		/**
73
		 * @param $table_name
74
		 * @param $checked
75
		 * @return int
76
		 * function that get number of checked or unchecked comment for a table_name
77
		 */
78
		private function getNbCheckedComment($table_name, $checked) {
79
			$dbc = App::getDb();
80
			
81
			$query = $dbc->select("ID_comment")->from("_comment_all")->where("table_name", "=", $table_name, "AND")
82
				->where("checked", "=", $checked)->get();
83
			
84
			return count($query);
85
		}
86
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
87
		
88
		
89
		//-------------------------- SETTER ----------------------------------------------------------------------------//
90
		/**
91
		 * @param $values
92
		 * function to set all values and sort it in future
93
		 */
94
		public function setValues($values) {
95
			$this->values = array_merge($this->values, $values);
96
		}
97
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
98
	}