Passed
Push — master ( b9668a...d3086a )
by Anthony
04:34
created

RelationFaction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 23.96 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getListeRelation() 23 23 3
A getAllRelationsPossible() 0 15 2
A getIdFactionRelation() 0 17 3
A setAjouterRelation() 0 13 2

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\bataille\app\controller;
3
	
4
	
5
	use core\App;
6
	use core\HTML\flashmessage\FlashMessage;
7
	
8
	class RelationFaction extends Faction {
9
		//-------------------------- GETTER ----------------------------------------------------------------------------//
10
		/**
11
		 * renvoi la liste des relations d'une faction
12
		 */
13 View Code Duplication
		public function getListeRelation() {
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...
14
			$dbc = App::getDb();
15
			
16
			$query = $dbc->select()->from("_bataille_faction_relation")
17
				->from("_bataille_faction")
18
				->where("_bataille_faction_relation.ID_faction", "=", $this->id_faction, "AND")
19
				->where("_bataille_faction_relation.ID_autre_faction", "=", "_bataille_faction.ID_faction", "", true)
20
				->get();
21
			
22
			$relations = [];
23
			if (count($query) > 0) {
24
				foreach ($query as $obj) {
25
					$relations[] = [
26
						"id_relation" => $obj->ID_faction_relation,
27
						"relation" => $obj->relation,
28
						"id_autre_faction" => $obj->ID_autre_faction,
29
						"nom_autre_faction" => $obj->nom_faction
30
					];
31
				}
32
			}
33
			
34
			Bataille::setValues(["relations" => $relations]);
35
		}
36
		
37
		/**
38
		 * fonction qui récupère la liste des relations qu'il sera possible
39
		 * de mettre dans le select
40
		 */
41
		public function getAllRelationsPossible() {
42
			$dbc1 = Bataille::getDb();
43
			
44
			$query = $dbc1->select()->from("faction_relations");
45
			
46
			$relations = [];
47
			foreach ($query as $obj) {
48
				$relations[] = [
49
					"relation" => $obj->relation
50
				];
51
			}
52
			
53
			Bataille::setValues(["liste_relations" => $relations]);
54
			return $relations;
55
		}
56
		
57
		/**
58
		 * @param $relation
59
		 * @return array
60
		 */
61
		public function getIdFactionRelation($relation) {
62
			$dbc = App::getDb();
63
			
64
			$query = $dbc->select("ID_autre_faction")->from("_bataille_faction_relation")
65
				->where("relation", "=", $relation, "AND")
66
				->where("ID_faction", "=", $this->id_faction)
67
				->get();
68
			
69
			$relations = [];
70
			if (count($query) > 0) {
71
				foreach ($query as $obj) {
72
					$relations[] = $obj->ID_autre_faction;
73
				}
74
			}
75
			
76
			return $relations;
77
		}
78
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
79
		
80
		
81
		//-------------------------- SETTER ----------------------------------------------------------------------------//
82
		/**
83
		 * @param $nom_faction
84
		 * @param $relation
85
		 * @return bool
86
		 * fonction qui permet d'ajouter une relation
87
		 */
88
		public function setAjouterRelation($nom_faction, $relation) {
89
			$dbc = App::getDb();
90
			
91
			if ($this->getFactionExist($nom_faction) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
92
				FlashMessage::setFlash("Cette faction n'existe pas, vérifiez que vous avez correctement écrit son nom");
93
				return false;
94
			}
95
			
96
			$dbc->insert("relation", $relation)
97
				->insert("ID_faction", $this->id_faction)
98
				->insert("ID_autre_faction", $this->id_autre_faction)
99
				->into("_bataille_faction_relation")->set();
100
		}
101
		
102
		//-------------------------- END SETTER ----------------------------------------------------------------------------//    
103
	}