UpdateArticleCount   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B execute() 0 27 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 34 and the first side effect is on line 26.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Provide a better count of the number of articles
4
 * and update the site statistics table, if desired.
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along
17
 * with this program; if not, write to the Free Software Foundation, Inc.,
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 * http://www.gnu.org/copyleft/gpl.html
20
 *
21
 * @file
22
 * @ingroup Maintenance
23
 * @author Rob Church <[email protected]>
24
 */
25
26
require_once __DIR__ . '/Maintenance.php';
27
28
/**
29
 * Maintenance script to provide a better count of the number of articles
30
 * and update the site statistics table, if desired.
31
 *
32
 * @ingroup Maintenance
33
 */
34
class UpdateArticleCount extends Maintenance {
35
36
	public function __construct() {
37
		parent::__construct();
38
		$this->addDescription( 'Count of the number of articles and update the site statistics table' );
39
		$this->addOption( 'update', 'Update the site_stats table with the new count' );
40
		$this->addOption( 'use-master', 'Count using the master database' );
41
	}
42
43
	public function execute() {
44
		$this->output( "Counting articles..." );
45
46
		if ( $this->hasOption( 'use-master' ) ) {
47
			$dbr = $this->getDB( DB_MASTER );
48
		} else {
49
			$dbr = $this->getDB( DB_REPLICA, 'vslow' );
50
		}
51
		$counter = new SiteStatsInit( $dbr );
0 ignored issues
show
Bug introduced by
It seems like $dbr can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
52
		$result = $counter->articles();
53
54
		$this->output( "found {$result}.\n" );
55
		if ( $this->hasOption( 'update' ) ) {
56
			$this->output( "Updating site statistics table... " );
57
			$dbw = $this->getDB( DB_MASTER );
58
			$dbw->update(
59
				'site_stats',
60
				[ 'ss_good_articles' => $result ],
61
				[ 'ss_row_id' => 1 ],
62
				__METHOD__
63
			);
64
			$this->output( "done.\n" );
65
		} else {
66
			$this->output( "To update the site statistics table, run the script "
67
				. "with the --update option.\n" );
68
		}
69
	}
70
}
71
72
$maintClass = "UpdateArticleCount";
73
require_once RUN_MAINTENANCE_IF_MAIN;
74