Completed
Push — master ( e11cc9...0b2559 )
by cam
04:14
created

editer_logo.php ➔ logo_migrer_en_base()   C

Complexity

Conditions 15
Paths 102

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
nc 102
nop 2
dl 0
loc 58
rs 5.9
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-2019                                                *
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
/**
14
 * Gestion de l'API de modification/suppression des logos
15
 *
16
 * @package SPIP\Core\Logo\Edition
17
 */
18
19
if (!defined('_ECRIRE_INC_VERSION')) {
20
	return;
21
}
22
23
24
/**
25
 * Supprimer le logo d'un objet
26
 *
27
 * @param string $objet
28
 * @param int $id_objet
29
 * @param string $etat
30
 *     `on` ou `off`
31
 */
32
function logo_supprimer($objet, $id_objet, $etat) {
33
	$chercher_logo = charger_fonction('chercher_logo', 'inc');
34
	$objet = objet_type($objet);
35
	$primary = id_table_objet($objet);
36
	include_spip('inc/chercher_logo');
37
38
	// existe-t-il deja un logo ?
39
	$logo = $chercher_logo($id_objet, $primary, $etat);
40
	if ($logo) {
41
		# TODO : deprecated, a supprimer -> anciens logos IMG/artonxx.png pas en base
42
		if (count($logo) < 6) {
43
			spip_log("Supprimer ancien logo $logo", 'logo');
44
			spip_unlink($logo[0]);
45
		}
46
		elseif ($doc = $logo[5]
47
			and isset($doc['id_document'])
48
		  and $id_document = $doc['id_document']) {
49
50
			include_spip('action/editer_liens');
51
			// supprimer le lien dans la base
52
			objet_dissocier(array('document' => $id_document), array($objet => $id_objet), array('role' => '*'));
53
54
			// verifier si il reste des liens avec d'autres objets et sinon supprimer
55
			$liens = objet_trouver_liens(array('document' => $id_document), '*');
56
			if (!count($liens)) {
57
				$supprimer_document = charger_fonction('supprimer_document', 'action');
58
				$supprimer_document($doc['id_document']);
59
			}
60
		}
61
	}
62
}
63
64
/**
65
 * Modifier le logo d'un objet
66
 *
67
 * @param string $objet
68
 * @param int $id_objet
69
 * @param string $etat
70
 *     `on` ou `off`
71
 * @param string|array $source
72
 *     - array : sous tableau de `$_FILE` issu de l'upload
73
 *     - string : fichier source (chemin complet ou chemin relatif a `tmp/upload`)
74
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|boolean|object|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
75
 *     Erreur, sinon ''
76
 */
77
function logo_modifier($objet, $id_objet, $etat, $source) {
78
	$chercher_logo = charger_fonction('chercher_logo', 'inc');
0 ignored issues
show
Unused Code introduced by
$chercher_logo 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...
79
	$objet = objet_type($objet);
80
	$primary = id_table_objet($objet);
0 ignored issues
show
Unused Code introduced by
$primary 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...
81
	include_spip('inc/chercher_logo');
82
83
	$mode = preg_replace(",\W,", '', $etat);
84
	if (!$mode){
85
		spip_log("logo_modifier : etat $etat invalide", 'logo');
86
		$erreur = 'etat invalide';
87
88
		return $erreur;
89
	}
90
	// chercher dans la base
91
	$mode_document = 'logo' . $mode;
92
93
	include_spip('inc/documents');
94
	$erreur = '';
0 ignored issues
show
Unused Code introduced by
$erreur 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...
95
96
	if (!$source) {
97
		spip_log('spip_image_ajouter : source inconnue', 'logo');
98
		$erreur = 'source inconnue';
99
100
		return $erreur;
101
	}
102
103
	// fichier dans upload/
104
	if (is_string($source)) {
105
		$tmp_name = false;
106
		if (file_exists($source)) {
107
			$tmp_name = $source;
108
		} elseif (file_exists($f = determine_upload() . $source)) {
109
			$tmp_name = $f;
110
		}
111
		if (!$tmp_name) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tmp_name of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
112
			spip_log('spip_image_ajouter : source inconnue', 'logo');
113
			$erreur = 'source inconnue';
114
115
			return $erreur;
116
		}
117
		$source = array(
118
			'tmp_name' => $tmp_name,
119
			'name' => basename($tmp_name),
120
		);
121
	} elseif ($erreur = check_upload_error($source['error'], '', true)) {
122
		return $erreur;
123
	}
124
125
	// supprimer le logo eventueel existant
126
	// TODO : si un logo existe, le modifier plutot que supprimer + reinserer (mais il faut gerer le cas ou il est utilise par plusieurs objets, donc pas si simple)
127
	// mais de toute facon l'interface actuelle oblige a supprimer + reinserer
128
	logo_supprimer($objet, $id_objet, $etat);
129
130
131
	$source['mode'] = $mode_document;
132
	$ajouter_documents = charger_fonction('ajouter_documents', 'action');
133
	$ajoutes = $ajouter_documents('new', [$source], $objet, $id_objet, $mode_document);
134
135
	$id_document = reset($ajoutes);
136
137
	if (!is_numeric($id_document)) {
138
		$erreur = ($id_document ? $id_document : 'Erreur inconnue');
139
		spip_log("Erreur ajout logo : $erreur pour source=".json_encode($source), 'logo');
140
		return $erreur;
141
	}
142
143
	return ''; // tout est bon, pas d'erreur
144
145
}
146
147
function logo_migrer_en_base($objet, $time_limit) {
148
149
	$dir_logos_erreurs = sous_repertoire(_DIR_IMG, 'logo_erreurs');
150
	$dir_logos = sous_repertoire(_DIR_IMG, 'logo');
151
	$formats_logos = array('jpg', 'png', 'svg', 'gif');
152
	if (isset($GLOBALS['formats_logos'])) {
153
		$formats_logos = $GLOBALS['formats_logos'];
154
	}
155
156
157
	$chercher_logo = charger_fonction('chercher_logo', 'inc');
158
	include_spip('inc/chercher_logo');
159
	$_id_objet = id_table_objet($objet);
160
	$type = type_du_logo($_id_objet);
0 ignored issues
show
Deprecated Code introduced by
The function type_du_logo() has been deprecated with message: MAIS NE PAS SUPPRIMER CAR SERT POUR L'UPGRADE des logos et leur mise en base

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
161
162
	foreach (['on', 'off'] as $mode) {
163
		$nom_base = $type . $mode;
164
		$dir = (defined('_DIR_LOGOS') ? _DIR_LOGOS : _DIR_IMG);
165
166
		$deja = array();
167
		$files = glob($dir . $nom_base . "*");
168
169
		foreach ($files as $file) {
170
			$logo = substr($file, strlen($dir . $nom_base));
171
			$logo = explode('.', $logo);
172
			if (is_numeric($logo[0])
173
			  and $id_objet = intval($logo[0])) {
174
				if (!isset($deja[$id_objet])) {
175
					$logo = $chercher_logo($id_objet, $_id_objet, $mode);
176
					// if no logo in base
177
					if (!$logo or count($logo)<6) {
178
						foreach ($formats_logos as $format) {
179
							if (@file_exists($d = ($dir . ($nom = $nom_base . intval($id_objet) . '.' . $format)))) {
180
								// logo_modifier commence par supprimer le logo existant, donc on le deplace pour pas le perdre
181
								@rename($d, $dir_logos . $nom);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
182
								// et on le declare comme nouveau logo
183
								logo_modifier($objet, $id_objet, $mode, $dir_logos . $nom);
184
								break;
185
							}
186
						}
187
					}
188
					$deja[$id_objet] = true;
189
				}
190
			}
191
			// si le fichier est encore la on le move : rien a faire ici
192
			if (file_exists($file)) {
193
				@rename($file, $dir_logos_erreurs . basename($file));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
194
			}
195
196
			if ($time_limit and time() > $time_limit) {
197
				effacer_meta('drapeau_edition');
198
				return;
199
			}
200
		}
201
202
	}
203
	effacer_meta('drapeau_edition');
204
}