Completed
Push — spip-2.1 ( b6b097 )
by cam
42:28 queued 30:44
created

editer_auteur.php ➔ instituer_auteur()   F

Complexity

Conditions 20
Paths 541

Size

Total Lines 88
Code Lines 54

Duplication

Lines 5
Ratio 5.68 %

Importance

Changes 0
Metric Value
cc 20
eloc 54
nc 541
nop 2
dl 5
loc 88
rs 2.6768
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/***************************************************************************\
4
 *  SPIP, Systeme de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright (c) 2001-2016                                                *
7
 *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
8
 *                                                                         *
9
 *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
10
 *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
11
\***************************************************************************/
12
13
if (!defined('_ECRIRE_INC_VERSION')) return;
14
15
// http://doc.spip.org/@action_editer_auteur_dist
16
function action_editer_auteur_dist($arg=null) {
17
18
	if (is_null($arg)){
19
		$securiser_action = charger_fonction('securiser_action', 'inc');
20
		$arg = $securiser_action();
21
	}
22
23
24
	// si id_auteur n'est pas un nombre, c'est une creation
25
	if (!$id_auteur = intval($arg)) {
26
27
		if (($id_auteur = insert_auteur()) > 0){
28
29
			# cf. GROS HACK
30
			# recuperer l'eventuel logo charge avant la creation
31
			# ils ont un id = 0-id_auteur de la session
32
			$id_hack = 0 - $GLOBALS['visiteur_session']['id_auteur'];
33
			$chercher_logo = charger_fonction('chercher_logo', 'inc');
34 View Code Duplication
			if (list($logo) = $chercher_logo($id_hack, 'id_auteur', 'on'))
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...
35
				rename($logo, str_replace($id_hack, $id_auteur, $logo));
36 View Code Duplication
			if (list($logo) = $chercher_logo($id_hack, 'id_auteur', 'off'))
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...
37
				rename($logo, str_replace($id_hack, $id_auteur, $logo));
38
		}
39
	}
40
41
	// Enregistre l'envoi dans la BD
42
	if ($id_auteur > 0)
43
		$err = auteurs_set($id_auteur);
44
45
	if ($redirect = _request('redirect')) {
46
		if ($err){
0 ignored issues
show
Bug introduced by
The variable $err 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...
47
			$ret = ('&redirect=' . $redirect);
48
			spip_log("echec editeur auteur: " . join(' ',$echec));
0 ignored issues
show
Bug introduced by
The variable $echec seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
49
			$echec = '&echec=' . join('@@@', $echec);
0 ignored issues
show
Bug introduced by
The variable $echec seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
50
			$redirect = generer_url_ecrire('auteur_infos',"id_auteur=$id_auteur$echec$ret",'&');
51
		}
52
		else
53
			$redirect = urldecode($redirect);
54
55
		$redirect = parametre_url($redirect,'id_auteur', $id_auteur, '&');
56
57
		include_spip('inc/headers');
58
		redirige_par_entete($redirect);
59
	}
60
	else
61
		return array($id_auteur,$err);
62
63
	$redirect = _request('redirect');
0 ignored issues
show
Unused Code introduced by
$redirect 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...
64
65
}
66
67
function insert_auteur($source=null) {
68
69
	// Ce qu'on va demander comme modifications
70
	$champs = array();
71
	$champs['source'] = $source?$source:'spip';
72
73
	$champs['login'] = '';
74
	$champs['statut'] = '5poubelle';  // inutilisable tant qu'il n'a pas ete renseigne et institue
75
	$champs['webmestre'] = 'non';
76
77
	// Envoyer aux plugins
78
	$champs = pipeline('pre_insertion',
79
		array(
80
			'args' => array(
81
				'table' => 'spip_auteurs',
82
			),
83
			'data' => $champs
84
		)
85
	);
86
	$id_auteur = sql_insertq("spip_auteurs", $champs);
87
	pipeline('post_insertion',
88
		array(
89
			'args' => array(
90
				'table' => 'spip_auteurs',
91
				'id_objet' => $id_auteur
92
			),
93
			'data' => $champs
94
		)
95
	);
96
	return $id_auteur;
97
}
98
99
100
// Appelle toutes les fonctions de modification d'un auteur
101
function auteurs_set($id_auteur, $set = null) {
102
	$err = '';
103
104
	if (is_null($set)){
105
		$c = array();
106
		foreach (array(
107
			'nom','email','bio',
108
			'nom_site','url_site',
109
			'imessage','pgp',
110
		) as $champ)
111
			$c[$champ] = _request($champ,$set);
112
	}
113
	else{
114
		$c = $set;
115
		unset($c['webmestre']);
116
		unset($c['pass']);
117
		unset($c['login']);
118
	}
119
120
	include_spip('inc/modifier');
121
	revision_auteur($id_auteur, $c);
122
123
	// Modification de statut, changement de rubrique ?
124
	$c = array();
125
	foreach (array(
126
		'statut', 'new_login','new_pass','login','pass','webmestre','restreintes','id_parent'
127
	) as $champ)
128
		if (_request($champ,$set))
129
			$c[preg_replace(',^new_,','',$champ)] = _request($champ,$set);
130
131
	$err .= instituer_auteur($id_auteur, $c);
132
133
	// Un lien auteur a prendre en compte ?
134
	$err .= auteur_referent($id_auteur, array('article' => _request('lier_id_article',$set)));
135
136
	return $err;
137
}
138
139
function auteur_referent($id_auteur,$c){
140
	foreach($c as $objet => $id_objet){
141
		if ($id_objet=intval($id_objet)){
142
			$table = table_objet($objet);
143
			$primary = id_table_objet($objet);
144
			// Lier a un article sur lequel on a une liaison possible
145
			if (in_array($table, array('articles','rubriques','messages'))){
146
				sql_insertq("spip_auteurs_$table", array($primary => $id_objet, 'id_auteur' =>$id_auteur));
147
			}
148
		}
149
	}
150
151
	return ''; // pas d'erreur
152
}
153
154
// http://doc.spip.org/@instituer_auteur
155
function instituer_auteur($id_auteur, $c) {
156
	if (!$id_auteur=intval($id_auteur))
157
		return false;
158
	// commencer par traiter les cas particuliers des logins et pass
159
	// avant le changement de statut eventuel
160
	if (isset($c['login']) OR isset($c['pass'])){
161
		$auth_methode = sql_getfetsel('source','spip_auteurs','id_auteur='.intval($id_auteur));
162
		include_spip('inc/auth');
163
		if (isset($c['login']))
164
			auth_modifier_login($auth_methode, $c['login'], $id_auteur);
165
		if (isset($c['pass'])){
166
			$c['login'] = sql_getfetsel('login','spip_auteurs','id_auteur='.intval($id_auteur));
167
			auth_modifier_pass($auth_methode, $c['login'], $c['pass'], $id_auteur);
168
		}
169
	}
170
171
	
172
	$champs = array();
173
	$statut = $statut_ancien = sql_getfetsel('statut','spip_auteurs','id_auteur='.intval($id_auteur));
174
	
175
	if (isset($c['statut'])
176
	AND (($statut_ancien == 'nouveau') OR autoriser('modifier', 'auteur', $id_auteur,null, array('statut' => '?'))))
177
		$statut = $champs['statut'] = $c['statut'];
178
179
	// Restreindre avant de declarer l'auteur
180
	// (section critique sur les droits)
181
	if ($c['id_parent']) {
182
		if (is_array($c['restreintes']))
183
			$c['restreintes'][] = $c['id_parent'];
184
		else
185
			$c['restreintes'] = array($c['id_parent']);
186
	}
187
188
	if (isset($c['webmestre'])
189
	  AND autoriser('modifier', 'auteur', $id_auteur,null, array('webmestre' => '?')))
190
		$champs['webmestre'] = $c['webmestre']=='oui'?'oui':'non';
191
	
192
	// Envoyer aux plugins
193
	$champs = pipeline('pre_edition',
194
		array(
195
			'args' => array(
196
				'table' => 'spip_auteurs',
197
				'id_objet' => $id_auteur,
198
				'action' => 'instituer',
199
			),
200
			'data' => $champs
201
		)
202
	);
203
	
204
	if (is_array($c['restreintes'])
205
	AND autoriser('modifier', 'auteur', $id_auteur, NULL, array('restreint'=>$c['restreintes']))) {
206
		sql_delete("spip_auteurs_rubriques", "id_auteur=".sql_quote($id_auteur));
207
		foreach (array_unique($c['restreintes']) as $id_rub)
208
			if ($id_rub = intval($id_rub)) // si '0' on ignore
209
				sql_insertq('spip_auteurs_rubriques', array('id_auteur' => $id_auteur, 'id_rubrique'=>$id_rub));
210
	}
211
212
	if (!count($champs)) return;
213
	sql_updateq('spip_auteurs', $champs , 'id_auteur='.$id_auteur);
214
	include_spip('inc/modifier');
215
	sql_updateq('spip_auteurs',$champs,'id_auteur='.$id_auteur);
216
	
217
	// Invalider les caches
218
	include_spip('inc/invalideur');
219
	suivre_invalideur("id='id_auteur/$id_auteur'");
220
	
221
	// Pipeline
222
	pipeline('post_edition',
223
		array(
224
			'args' => array(
225
				'table' => 'spip_auteurs',
226
				'id_objet' => $id_auteur,
227
				'action' => 'instituer',
228
			),
229
			'data' => $champs
230
		)
231
	);
232
233
	// Notifications
234 View Code Duplication
	if ($notifications = charger_fonction('notifications', 'inc')) {
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...
235
		$notifications('instituerauteur', $id_auteur,
236
			array('statut' => $statut, 'statut_ancien' => $statut_ancien)
237
		);
238
	}
239
240
	return ''; // pas d'erreur
241
242
}
243
244
245
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
246