Messagerie   B
last analyzed

Complexity

Total Complexity 50

Size/Duplication

Total Lines 377
Duplicated Lines 21.49 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 50
c 2
b 0
f 1
lcom 1
cbo 0
dl 81
loc 377
rs 8.6206

23 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 13 5
A getIdMessage() 0 3 1
A getObjet() 0 3 1
A getMessage() 0 3 1
A getDateMessage() 0 3 1
A getUrl() 0 3 1
A getIdExpediteur() 0 3 1
A getPseudoExpediteur() 0 3 1
A getIdReceveur() 0 3 1
A getPseudoReceveur() 0 3 1
A getValues() 0 3 1
A getIdIdentiteExist() 0 15 4
B getBoiteReception() 32 32 4
B getMessagesEnvoyes() 0 30 4
B getMessageSupprimes() 31 31 4
A getMessageNonLu() 0 16 2
B getUnMessage() 0 32 4
A setValues() 0 3 1
A setLireMessage() 9 9 1
B setEnvoyerMessage() 0 56 8
A setLireAllMessage() 0 5 1
A setArchiverAllMessage() 0 5 1
A setArchiverMessage() 9 9 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Messagerie often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Messagerie, and based on these observations, apply Extract Interface, too.

1
<?php
2
	namespace modules\messagerie\app\controller;
3
4
	use core\App;
5
	use core\functions\ChaineCaractere;
6
	use modules\bataille\app\controller\Bataille;
7
	
8
	class Messagerie {
9
		public static $url_message;
10
11
		private $id_message;
12
		private $objet;
13
		private $message;
14
		private $date_message;
15
		private $url;
16
17
		private $id_expediteur;
18
		private $pseudo_expediteur;
19
20
		private $pseudo_receveur;
21
		private $id_receveur;
22
23
		private $values = [];
24
		
25
		
26
		//-------------------------- BUILDER ----------------------------------------------------------------------------//
27
		/**
28
		 * @param null $type_boite
29
		 * initialisation de la récupération des messages des différents boites
30
		 */
31
		public function __construct($type_boite = null) {
32
			if ($type_boite !== null) {
33
				if ($type_boite == "boite réception") {
34
					$this->getBoiteReception();
35
				}
36
				else if ($type_boite == "messages envoyés") {
37
					$this->getMessagesEnvoyes();
38
				}
39
				else if ($type_boite == "messages supprimés") {
40
					$this->getMessageSupprimes();
41
				}
42
			}
43
		}
44
		//-------------------------- END BUILDER ----------------------------------------------------------------------------//
45
		
46
		
47
		
48
		//-------------------------- GETTER ----------------------------------------------------------------------------//
49
		public function getIdMessage() {
50
			return $this->id_message;
51
		}
52
		public function getObjet() {
53
			return $this->objet;
54
		}
55
		public function getMessage() {
56
			return $this->message;
57
		}
58
		public function getDateMessage(){
59
		    return $this->date_message;
60
		}
61
		public function getUrl(){
62
		    return $this->url;
63
		}
64
		public function getIdExpediteur() {
65
			return $this->id_expediteur;
66
		}
67
		public function getPseudoExpediteur() {
68
			return $this->pseudo_expediteur;
69
		}
70
		public function getIdReceveur() {
71
			return $this->id_receveur;
72
		}
73
		public function getPseudoReceveur() {
74
			return $this->pseudo_receveur;
75
		}
76
		public function getValues(){
77
		    return ["messagerie" => $this->values];
78
		}
79
80
		/**
81
		 * fonction qui permet de récupérer tous les messages dans la boite de récéption
82
		 */
83 View Code Duplication
		private function getBoiteReception() {
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...
Coding Style introduced by
getBoiteReception uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
84
			$dbc = App::getDb();
85
86
			$query = $dbc->select()
87
				->from("_messagerie_boite_reception")
88
				->from("_messagerie_message")
89
				->from("identite")
90
				->where("_messagerie_boite_reception.ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE], "AND")
91
				->where("_messagerie_boite_reception.supprimer", " IS ", "NULL", "AND", true)
92
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
93
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "", true)
94
				->orderBy("date", "DESC")
95
				->get();
96
97
			if ((is_array($query)) && (count($query) > 0)) {
98
				foreach ($query as $obj) {
99
					$arr = [
100
						"id_message" => $obj->ID_message,
101
						"objet" => $obj->objet,
102
						"lu" => $obj->lu,
103
						"date_message" => $obj->date,
104
						"id_expediteur" => $obj->ID_expediteur,
105
						"pseudo_expediteur" => $obj->pseudo,
106
						"url" => $obj->url
107
					];
108
					
109
					$values[] = $arr;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$values was never initialized. Although not strictly required by PHP, it is generally a good practice to add $values = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
110
				}
111
				
112
				$this->setValues($values);
0 ignored issues
show
Bug introduced by
The variable $values does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
113
			}
114
		}
115
116
		/**
117
		 * fonction qui permet de récupérer tous les messages dans la boite des messages envoyes
118
		 */
119
		private function getMessagesEnvoyes() {
0 ignored issues
show
Coding Style introduced by
getMessagesEnvoyes uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
120
			$dbc = App::getDb();
121
122
			$query = $dbc->select()
123
				->from("_messagerie_boite_reception")
124
				->from("_messagerie_message")
125
				->from("identite")
126
				->where("_messagerie_message.ID_expediteur", "=", $_SESSION['idlogin'.CLEF_SITE], "AND")
127
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
128
				->where("_messagerie_boite_reception.ID_identite", "=", "identite.ID_identite", "", true)
129
				->orderBy("date", "DESC")
130
				->get();
131
132
			if ((is_array($query)) && (count($query) > 0)) {
133
				foreach ($query as $obj) {
134
					$arr = [
135
						"id_message" => $obj->ID_message,
136
						"objet" => $obj->objet,
137
						"date_message" => $obj->date,
138
						"id_expediteur" => $obj->ID_expediteur,
139
						"pseudo_receveur" => $obj->pseudo,
140
						"url" => $obj->url
141
					];
142
143
					$values[] = $arr;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$values was never initialized. Although not strictly required by PHP, it is generally a good practice to add $values = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
144
				}
145
				
146
				$this->setValues($values);
0 ignored issues
show
Bug introduced by
The variable $values does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
147
			}
148
		}
149
150
		/**
151
		 * fonction qui récupère tous les messages supprimés
152
		 */
153 View Code Duplication
		private function getMessageSupprimes() {
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...
Coding Style introduced by
getMessageSupprimes uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
154
			$dbc = App::getDb();
155
			
156
			$query = $dbc->select()
157
				->from("_messagerie_boite_reception")
158
				->from("_messagerie_message")
159
				->from("identite")
160
				->where("_messagerie_boite_reception.ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE], "AND")
161
				->where("_messagerie_boite_reception.supprimer", "=", 1, "AND")
162
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "AND", true)
163
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "", true)
164
				->orderBy("date", "DESC")
165
				->get();
166
167
			if ((is_array($query)) && (count($query) > 0)) {
168
				foreach ($query as $obj) {
169
					$arr = [
170
						"id_message" => $obj->ID_message,
171
						"objet" => $obj->objet,
172
						"date_message" => $obj->date,
173
						"id_expediteur" => $obj->ID_expediteur,
174
						"pseudo_expediteur" => $obj->pseudo,
175
						"url" => $obj->url
176
					];
177
178
					$values[] = $arr;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$values was never initialized. Although not strictly required by PHP, it is generally a good practice to add $values = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
179
				}
180
				
181
				$this->setValues($values);
0 ignored issues
show
Bug introduced by
The variable $values does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
182
			}
183
		}
184
185
		/*
186
		 * fonction qui permetlors de l'envoit d'un message d'être sur que le membre existe
187
		 */
188
		private function getIdIdentiteExist($pseudo) {
189
			$dbc = App::getDb();
190
191
			$pseudo = trim($pseudo);
192
193
			$query = $dbc->select("ID_identite")->from("identite")->where("pseudo", "=", $pseudo)->get();
194
195
			if ((count($query) == 1) && (is_array($query))) {
196
				foreach ($query as $obj) {
197
					return $obj->ID_identite;
198
				}
199
			}
200
201
			return false;
202
		}
203
		
204
		/**
205
		 * fonction qui sert à récupérer les messages non lus
206
		 */
207
		public function getMessageNonLu() {
0 ignored issues
show
Coding Style introduced by
getMessageNonLu uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
208
			$dbc = App::getDb();
209
			
210
			$query = $dbc->select("ID_message")
211
				->from("_messagerie_boite_reception")
212
				->where("ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE], "AND")
213
				->where("lu", " IS NULL ", "", "OR", true)
214
				->where("lu", "=", 0, "", true)
215
				->get();
216
			
217
			$count = count($query);
218
			
219
			if ($count > 0) {
220
				$this->setValues(["messages_non_lu" => count($query)]);
221
			}
222
		}
223
224
		/**
225
		 * @param $url_message
226
		 * fonction qui récupère un message suivant une url
227
		 */
228
		public function getUnMessage($url_message) {
229
			$dbc = App::getDb();
230
231
			$query = $dbc->select()
232
				->from("_messagerie_message")
233
				->from("_messagerie_boite_reception")
234
				->from("identite")
235
				->where("_messagerie_message.url", "=", $url_message, "AND")
236
				->where("_messagerie_message.ID_expediteur", "=", "identite.ID_identite", "AND", true)
237
				->where("_messagerie_boite_reception.ID_message", "=", "_messagerie_message.ID_message", "", true)
238
				->get();
239
240
			if ((is_array($query)) && (count($query) > 0)) {
241
				foreach ($query as $obj) {
242
					$this->setValues([
243
						"id_message" => $obj->ID_message,
244
						"objet" => $obj->objet,
245
						"message" => $obj->message,
246
						"date_message" => $obj->date,
247
						"id_expediteur" => $obj->ID_expediteur,
248
						"pseudo_expediteur" => $obj->pseudo,
249
						"url" => $obj->url,
250
						"supprimer" => $obj->supprimer
251
					]);
252
					
253
					$this->setLireMessage($obj->ID_message);
254
				}
255
			}
256
			else {
257
				return false;
258
			}
259
		}
260
		//-------------------------- END GETTER ----------------------------------------------------------------------------//
261
		
262
		
263
		
264
		//-------------------------- SETTER ----------------------------------------------------------------------------//
265
		/**
266
		 * @param $values
267
		 * can set values while keep older infos
268
		 */
269
		public function setValues($values) {
270
			$this->values = array_merge($this->values, $values);
271
		}
272
		
273
		/**
274
		 * @param $id_message
275
		 * fonction qui passe un message en lu un fois qu'il a été ouvert
276
		 */
277 View Code Duplication
		private function setLireMessage($id_message) {
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...
Coding Style introduced by
setLireMessage uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
278
			$dbc = App::getDb();
279
			
280
			$dbc->update("lu", 1)
281
				->from("_messagerie_boite_reception")
282
				->where("ID_message", "=", $id_message, "AND")
283
				->where("ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE])
284
				->set();
285
		}
286
		
287
		/**
288
		 * @param $id_message
289
		 * pour passer le message en supprimé, il sera alors consultable dans la table messages supprimés
290
		 */
291 View Code Duplication
		public function setArchiverMessage($id_message) {
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...
Coding Style introduced by
setArchiverMessage uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
292
			$dbc = App::getDb();
293
294
			$dbc->update("supprimer", 1)->update("lu", 1)
295
				->from("_messagerie_boite_reception")
296
				->where("ID_message", "=", $id_message, "AND")
297
				->where("ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE])
298
				->set();
299
		}
300
301
		/**
302
		 * @param $objet
303
		 * @param $destinataire
304
		 * @param $message
305
		 * @return bool
306
		 *
307
		 * fonction qui sert à envoyer un message à un ou plusieurs destinataires
308
		 */
309
		public function setEnvoyerMessage($objet, $destinataire, $message) {
0 ignored issues
show
Coding Style introduced by
setEnvoyerMessage uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
310
			$dbc = App::getDb();
311
312
			//on test si un ou plusieurs destinataires ++ si ils existent
313
			if (ChaineCaractere::FindInString($destinataire, ",")) {
314
				$destinataires = explode(",", $destinataire);
315
				$c = count($destinataires);
316
317
				for ($i=0 ; $i<$c ; $i++) {
318
					if ($this->getIdIdentiteExist($destinataires[$i]) !== false) {
319
						$destinataires[] = $this->getIdIdentiteExist($destinataires[$i]);
320
						$expediteur = $_SESSION['idlogin'.CLEF_SITE];
321
					}
322
					else {
323
						return false;
324
					}
325
				}
326
			}
327
			else {
328
				if ($this->getIdIdentiteExist($destinataire) !== false) {
329
					$destinataires[] = $this->getIdIdentiteExist($destinataire);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$destinataires was never initialized. Although not strictly required by PHP, it is generally a good practice to add $destinataires = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
330
					$expediteur = $_SESSION['idlogin'.CLEF_SITE];
331
				}
332
				else if (is_numeric($destinataire)) {
333
					$destinataires[] = $destinataire;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$destinataires was never initialized. Although not strictly required by PHP, it is generally a good practice to add $destinataires = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
334
					$expediteur = 1;
335
				}
336
				else {
337
					return false;
338
				}
339
			}
340
341
			//cela veut dire qu'on a au moin 1 membre à qui envoyer le message
342
			if (count($destinataires) > 0) {
343
				$dbc->insert("message", $message)
344
					->insert("objet", $objet)
345
					->insert("url", ChaineCaractere::setUrl($objet."-".date("Y-m-d H:i:s")))
346
					->insert("date", date("Y-m-d H:i:s"))
347
					->insert("ID_expediteur", $expediteur)
0 ignored issues
show
Bug introduced by
The variable $expediteur does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
348
					->into("_messagerie_message")
349
					->set();
350
351
				$id_message = $dbc->lastInsertId();
352
353
				foreach ($destinataires as $destinataire) {
354
					$dbc->insert("ID_identite", $destinataire)
355
						->insert("ID_message", $id_message)
356
						->into("_messagerie_boite_reception")
357
						->set();
358
				}
359
360
				return true;
361
			}
362
363
			return false;
364
		}
365
		
366
		/**
367
		 * fonction qui permet de passer tous les messages en lu
368
		 */
369
		public function setLireAllMessage() {
0 ignored issues
show
Coding Style introduced by
setLireAllMessage uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
370
			$dbc = App::getDb();
371
			
372
			$dbc->update("lu", 1)->from("_messagerie_boite_reception")->where("ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE])->set();
373
		}
374
		
375
		/**
376
		 * fonction qui permet de supprimer tous les messages
377
		 */
378
		public function setArchiverAllMessage() {
0 ignored issues
show
Coding Style introduced by
setArchiverAllMessage uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
379
			$dbc = App::getDb();
380
			
381
			$dbc->update("supprimer", 1)->update("lu", 1)->from("_messagerie_boite_reception")->where("ID_identite", "=", $_SESSION['idlogin'.CLEF_SITE])->set();
382
		}
383
		//-------------------------- END SETTER ----------------------------------------------------------------------------//
384
	}