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

statistiques_visites.php ➔ exec_statistiques_visites_args()   F

Complexity

Conditions 25
Paths > 20000

Size

Total Lines 143
Code Lines 91

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 91
nc 442368
nop 6
dl 0
loc 143
rs 2
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
include_spip('inc/presentation');
16
include_spip('inc/statistiques');
17
18
// http://doc.spip.org/@exec_statistiques_visites_dist
19
function exec_statistiques_visites_dist()
20
{
21
	$id_article = intval(_request('id_article'));
22
	$type = _request('type');
23
	if (!preg_match('/^\w+$/', $type)) $type = 'day';
24
	$duree = intval(_request('duree'));
25
	if (!$duree) $duree = 105;
26
	$interval = intval(_request('interval'));
27
	if (!$interval) {
28
	  if ($type == 'day')
29
	    $interval = 3600*24;
30
	  else $interval = 3600;
31
	}
32
33
	// nombre de referers a afficher
34
	$limit = intval(_request('limit'));
35
	if ($limit == 0) $limit = 100;
36
37
	if (!autoriser('voirstats', $id_article ? 'article':'', $id_article)) {
38
		include_spip('inc/minipres');
39
		echo minipres();
40
	} else exec_statistiques_visites_args($id_article, $duree, $interval, $type, $limit);
41
}
42
43
44
// http://doc.spip.org/@exec_statistiques_visites_args
45
function exec_statistiques_visites_args($id_article, $duree, $interval, $type, $limit,$serveur='')
46
{
47
	$titre = $pourarticle = "";
48
49
	if ($id_article){
50
		$row = sql_fetsel("titre, visites, popularite", "spip_articles", "statut='publie' AND id_article=$id_article",'','','','',$serveur);
51
52
		if ($row) {
53
			$titre = typo($row['titre']);
54
			$total_absolu = $row['visites'];
55
			$val_popularite = round($row['popularite']);
56
		}
57
	} else {
58
		$row = sql_fetsel("SUM(visites) AS total_absolu", "spip_visites",'','','','','',$serveur);
59
		$total_absolu = $row ? $row['total_absolu'] : 0;
60
		$val_popularite = 0;
61
	}
62
63
	if ($titre) $pourarticle = " "._T('info_pour')." &laquo; $titre &raquo;";
64
	if ($serveur) {
65
		if ($row = sql_fetsel('valeur','spip_meta',"nom='nom_site'",'','','','',$serveur)){
66
			$titre = $row['valeur'].($titre?" / $titre":"");
67
		}
68
	}
69
70
	$commencer_page = charger_fonction('commencer_page', 'inc');
71
	echo $commencer_page(_T('titre_page_statistiques_visites').$pourarticle, "statistiques_visites", "statistiques");
72
	echo gros_titre(_T('titre_evolution_visite')."<html>".aide("confstat")."</html>",'', false);
73
//	barre_onglets("statistiques", "evolution");
74
	if ($titre) echo gros_titre($titre,'', false);
75
76
	echo debut_gauche('', true);
77
	echo "<div class='cadre cadre-e' style='padding: 5px;'>";
78
	echo "<div class='cadre_padding verdana1 spip_x-small'>";
79
	echo typo(_T('info_afficher_visites'));
80
	echo "<ul>";
81
82
	if ($id_article>0) {
83
		echo "<li><b><a href='" . generer_url_ecrire("statistiques_visites","") . "'>"._T('info_tout_site')."</a></b></li>";
84
	} else {
85
		echo "<li><b>"._T('titre_page_articles_tous')."</b></li>";
86
	}
87
88
	echo "</ul>";
89
	echo "</div>";
90
	echo "</div>";
91
	
92
	$classement = array();
93
	$liste = 0;
94
	echo aff_statistique_visites_popularite($serveur, $id_article, $classement, $liste);
95
96
	// Par visites depuis le debut
97
	$result = aff_statistique_visites_par_visites($serveur, $id_article, $classement);
98
99
	if ($result OR $id_article)
100
		echo creer_colonne_droite('', true);
101
102
	if ($id_article) {
103
		echo bloc_des_raccourcis(icone_horizontale(_T('icone_retour_article'), generer_url_ecrire("articles","id_article=$id_article"), "article-24.gif","rien.gif", false));
104
	}
105
	echo $result;
106
107
	echo debut_droite('', true);
108
109
	if ($id_article) {
110
			$table = "spip_visites_articles";
111
			$table_ref = "spip_referers_articles";
112
			$where = "id_article=$id_article";
113
			  
114
	} else {
115
			$table = "spip_visites";
116
			$table_ref = "spip_referers";
117
			$where = "";
118
	}
119
120
	$order = "date";
121
122
	$where2 = $duree ? "$order > DATE_SUB(".sql_quote(date('Y-m-d H:i:s')).",INTERVAL $duree $type)": '';
123
	if ($where) $where2 = $where2 ?  "$where2 AND $where" : $where;
124
125
	// sur certains SQL, la division produit un entier tronque a la valeur inferieure
126
	// on ne peut donc faire un CEIL, il faut faire un FLOOR
127
	$log = statistiques_collecte_date('visites', "(FLOOR((UNIX_TIMESTAMP($order)+$interval-1) / $interval) *  $interval)", $table, $where2, $serveur);
128
129
	if ($log)
0 ignored issues
show
Bug Best Practice introduced by
The expression $log 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...
130
	  $res = statistiques_tous($log, $id_article, $table, $where, $order, $serveur, $duree, $interval, $total_absolu, $val_popularite,  $classement, $liste);
0 ignored issues
show
Bug introduced by
The variable $total_absolu 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...
Bug introduced by
The variable $val_popularite 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...
131
	  
132
	$mois = statistiques_collecte_date("SUM(visites)",
133
		"DATE_FORMAT($order,'%Y%m')", 
134
		$table,
135
		"$order > DATE_SUB(NOW(),INTERVAL 2700 DAY)"
136
		. ($where ? " AND $where" : ''),
137
		$serveur);
138
139
	if (count($mois)>1)  {
140
		$res[] = "<br /><span class='verdana1 spip_small'><b>"
0 ignored issues
show
Bug introduced by
The variable $res 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...
141
			. _T('info_visites_par_mois')
142
			. "</b></span>"
143
			. statistiques_par_mois($mois, '');
144
	}
145
  echo cadre_stat($res, $table, $id_article);
146
147
	if ($id_article) {
148
		$signatures = charger_fonction('signatures', 'statistiques');
149
		echo $signatures($duree, $interval, $type, $id_article, $serveur);
150
		/*
151
  Il faudra optimiser les requetes de ces stats c'est vraiment trop horrible :
152
  plusieurs secondes d'attente sur un site comme contrib.
153
  par ailleurs, l'affichage presente des defauts :
154
  cf http://trac.rezo.net/trac/spip/ticket/1598
155
		$forums = charger_fonction('forums', 'statistiques');
156
		echo $forums($duree, $interval, $type, $id_article, $serveur);
157
		*/
158
	}
159
160
161
	$referenceurs = charger_fonction('referenceurs', 'inc');
162
	$res = $referenceurs($id_article, "visites", $table_ref, $where, '', $limit);
163
164
	if ($res) {
165
166
		// Le lien pour en afficher "plus"
167
		$args = ($id_article?"id_article=$id_article&" : '') . "limit=" . strval($limit+200);
168
		$n =  count($res);
169
		$plus = generer_url_ecrire('statistiques_visites', $args);
170
		if ($plus) {
171
			$plus = "<div style='text-align:right;'><b><a href='$plus'>+++</a></b></div>";
172
		}
173
		$titre = _T("onglet_origine_visites")
174
		. " ($n " 
175
		. ($n == 1 ?  _T('info_site') :  _T('info_sites'))
176
		  . ")";
177
		echo '<br />', gros_titre($titre,'', false);
178
		echo "<div style='overflow:hidden;' class='verdana1 spip_small'><br />";
179
		echo "<ul class='referers'><li>";
180
		echo join("</li><li>\n",$res);
181
		echo "</li></ul>";
182
		echo $plus;
183
		echo "<br /></div>";	
184
	}
185
186
	echo fin_gauche(), fin_page();	
187
}
188
?>
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...
189