Completed
Push — master ( bc0916...92f140 )
by cam
04:43
created

v40.php ➔ maj_timestamp_mysql()   B

Complexity

Conditions 11
Paths 25

Size

Total Lines 46

Duplication

Lines 10
Ratio 21.74 %

Importance

Changes 0
Metric Value
cc 11
nc 25
nop 1
dl 10
loc 46
rs 7.3166
c 0
b 0
f 0

How to fix   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, Système de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright © avec tendresse depuis 2001                                 *
7
 *  Arnaud Martin, Antoine Pitrou, Philippe Rivière, Emmanuel Saint-James  *
8
 *                                                                         *
9
 *  Ce programme est un logiciel libre distribué sous licence GNU/GPL.     *
10
 *  Pour plus de détails voir le fichier COPYING.txt ou l'aide en ligne.   *
11
\***************************************************************************/
12
13
/**
14
 * Gestion des mises à jour de bdd de SPIP
15
 * 
16
 * Mises à jour en 4.0 (avant 2021)
17
 * 
18
 * À partir de 2021, les numéros d'upgrade sont de la forme YYYYMMDD00 où 00 est un incrément.
19
 *
20
 * @package SPIP\Core\SQL\Upgrade
21
 **/
22
if (!defined('_ECRIRE_INC_VERSION')) {
23
	return;
24
}
25
26
27
// adaptation des timestamp mysql
28
$GLOBALS['maj'][24379] = [['maj_timestamp_mysql']];
29
30
/**
31
 * Mise à jour des bdd Mysql pour réparer les timestamp auto-update absents
32
 *
33
 * @uses base_lister_toutes_tables()
34
 * @uses _mysql_remplacements_definitions_table()
35
 **/
36
function maj_timestamp_mysql($tables = null) {
37
38
	include_spip('base/dump');
39
	if (is_null($tables)) {
40
		$tables = base_lister_toutes_tables();
41
	} elseif (is_string($tables)) {
42
		$tables = [$tables];
43
	} elseif (!is_array($tables)) {
44
		return;
45
	}
46
47
	// rien a faire si base non mysql
48
	if (strncmp($GLOBALS['connexions'][0]['type'], 'mysql', 5) !== 0) {
49
		return;
50
	}
51
52
	$trouver_table = charger_fonction('trouver_table', 'base');
53
	// forcer le vidage de cache
54
	$trouver_table('');
55
56
	foreach ($tables as $table) {
57
		if (time() >= _TIME_OUT) {
58
			return;
59
		}
60
		if ($desc = $trouver_table($table)) {
61
			$fields_corrected = _mysql_remplacements_definitions_table($desc['field']);
62
			$d = array_diff($desc['field'], $fields_corrected);
63
			if ($d) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $d of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
64
				spip_log("Table $table TIMESTAMP incorrect", "maj");
65 View Code Duplication
				foreach ($desc['field'] as $field => $type) {
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...
66
					if ($desc['field'][$field] !== $fields_corrected[$field]) {
67
						spip_log("Adaptation TIMESTAMP table $table", "maj." . _LOG_INFO_IMPORTANTE);
68
						sql_alter("table $table change $field $field " . $fields_corrected[$field]);
69
						$trouver_table('');
70
						$new_desc = $trouver_table($table);
71
						spip_log("Apres conversion $table : " . var_export($new_desc['field'], true),
72
							"maj." . _LOG_INFO_IMPORTANTE);
73
					}
74
				}
75
			}
76
		}
77
	}
78
79
	// forcer le vidage de cache
80
	$trouver_table('');
81
}
82