Article   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 159
Duplicated Lines 22.64 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 17
c 5
b 0
f 2
lcom 1
cbo 1
dl 36
loc 159
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getImageArticle() 0 10 2
A getDateFr() 0 10 1
A getExtract() 0 5 1
B getLastArticle() 18 31 4
B getArticle() 0 30 4
B getCategoryArticle() 18 37 4

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\blog\app\controller;
3
	
4
	
5
	use core\App;
6
	
7
	class Article {
8
		
9
		
10
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
11
		public function __construct() {
12
			
13
		}
14
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
15
		
16
		
17
		//-------------------------- GETTER ----------------------------------------------------------------------------//
18
		protected function getImageArticle($url_article) {
19
			$url_image = ROOT."modules/blog/images/".$url_article.".png";
20
			
21
			if (file_exists($url_image)) {
22
				return WEBROOT."modules/blog/images/".$url_article.".png";;
23
			}
24
			else {
25
				return WEBROOT."modules/blog/images/fond-bloc.jpg";
26
			}
27
		}
28
		
29
		/**
30
		 * @param $date
31
		 * @return string
32
		 * function that return datein french format
33
		 */
34
		protected function getDateFr($date) {
35
			$mois = array("Janvier", "Fevrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Decembre");
36
			
37
			$explode = explode("-", $date);
38
			$jour_d = $explode[2];
39
			$mois_d = $explode[1];
40
			$annee_d = $explode[0];
41
			
42
			return $jour_d." ".$mois[$mois_d - 1]." ".$annee_d;
43
		}
44
		
45
		private function getExtract($article) {
46
			$article = substr(strip_tags($article), 0, 150)."...";
47
			
48
			return $article;
49
		}
50
		
51
		/**
52
		 * this function get last articles
53
		 */
54
		public function getLastArticle() {
55
			$dbc = App::getDb();
56
			$nb_article = Blog::getArticleIndex();
57
			
58
			$query = $dbc->select()
59
				->from("_blog_article")
60
				->from("identite")
61
				->where("_blog_article.ID_state", "=", 1, "AND")
62
				->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
63
				->orderBy("publication_date", "DESC")
64
				->limit(0, $nb_article)->get();
65
			
66 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
67
				$articles = [];
68
				
69
				foreach ($query as $obj) {
70
					$articles[] = [
71
						"id_article" => $obj->ID_article,
72
						"title" => $obj->title,
73
						"url" => $obj->url,
74
						"image" => $this->getImageArticle($obj->url),
75
						"article" => $this->getExtract($obj->article),
76
						"pseudo" => $obj->pseudo,
77
						"publication_date" => $this->getDateFr($obj->publication_date),
78
						"categories" => Blog::getCategory()->getCategoryArticle($obj->url)
79
					];
80
				}
81
				
82
				Blog::setValues(["articles" => $articles]);
83
			}
84
		}
85
		
86
		/**
87
		 * function that get one article
88
		 */
89
		public function getArticle() {
90
			$dbc = App::getDb();
91
			$param = Blog::$router_parameter;
92
			
93
			$query = $dbc->select()->from("_blog_article")->from("identite")->from("_blog_state")
94
				->where("ID_article", "=", $param, "OR")
95
				->where("url", "=", $param, "AND")
96
				->where("_blog_article.ID_identite", "=", "identite.ID_identite", "AND", true)
97
				->where("_blog_article.ID_state", "=", "_blog_state.ID_state", "", true)
98
				->get();
99
			
100
			if ((is_array($query)) && (count($query) == 1)) {
101
				foreach ($query as $obj) {
102
					$this->getExtract($obj->article);
103
					
104
					Blog::setValues(["article" => [
105
						"id_article" => $obj->ID_article,
106
						"title" => $obj->title,
107
						"url" => $obj->url,
108
						"article" => $obj->article,
109
						"extract" => $this->getExtract($obj->article),
110
						"pseudo" => $obj->pseudo,
111
						"id_state" => $obj->ID_state,
112
						"state" => $obj->state,
113
						"publication_date" => $this->getDateFr($obj->publication_date),
114
						"categories" => Blog::getCategory()->getCategoryArticle()
115
					]]);
116
				}
117
			}
118
		}
119
		
120
		/**
121
		 * function that get all categories of an article
122
		 */
123
		public function getCategoryArticle() {
124
			$dbc = App::getDb();
125
			$category = Blog::$router_parameter;
126
			
127
			$query = $dbc->select()
128
				->from("_blog_article")
129
				->from("_blog_category")
130
				->from("_blog_article_category")
131
				->from("identite")
132
				->where("_blog_category.ID_category", "=", $category, "OR")
133
				->where("_blog_category.category", "=", $category, "AND")
134
				->where("_blog_article.ID_state", "=", 1, "AND")
135
				->where("_blog_article_category.ID_article", "=", "_blog_article.ID_article", "AND", true)
136
				->where("_blog_article_category.ID_category", "=", "_blog_category.ID_category", "AND", true)
137
				->where("_blog_article.ID_identite", "=", "identite.ID_identite", "", true)
138
				->orderBy("publication_date", "DESC")
139
				->get();
140
			
141 View Code Duplication
			if ((is_array($query)) && (count($query) > 0)) {
142
				$articles = [];
143
				
144
				foreach ($query as $obj) {
145
					$articles[] = [
146
						"id_article" => $obj->ID_article,
147
						"title" => $obj->title,
148
						"url" => $obj->url,
149
						"image" => $this->getImageArticle($obj->url),
150
						"article" => $this->getExtract($obj->article),
151
						"pseudo" => $obj->pseudo,
152
						"publication_date" => $this->getDateFr($obj->publication_date),
153
						"categories" => Blog::getCategory()->getCategoryArticle($obj->url)
154
					];
155
				}
156
				
157
				Blog::setValues(["articles" => $articles]);
158
			}
159
		}
160
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
161
		
162
		
163
		//-------------------------- SETTER ----------------------------------------------------------------------------//
164
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
165
	}