McTest::execute()   D
last analyzed

Complexity

Conditions 14
Paths 336

Size

Total Lines 60
Code Lines 42

Duplication

Lines 16
Ratio 26.67 %

Importance

Changes 0
Metric Value
cc 14
eloc 42
nc 336
nop 0
dl 16
loc 60
rs 4.6516
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
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 33 and the first side effect is on line 25.

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
 * Makes several 'set', 'incr' and 'get' requests on every memcached
4
 * server and shows a report.
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
 */
24
25
require_once __DIR__ . '/Maintenance.php';
26
27
/**
28
 * Maintenance script that  makes several 'set', 'incr' and 'get' requests
29
 * on every memcached server and shows a report.
30
 *
31
 * @ingroup Maintenance
32
 */
33
class McTest extends Maintenance {
34
	public function __construct() {
35
		parent::__construct();
36
		$this->addDescription( "Makes several 'set', 'incr' and 'get' requests on every"
37
			. " memcached server and shows a report" );
38
		$this->addOption( 'i', 'Number of iterations', false, true );
39
		$this->addOption( 'cache', 'Use servers from this $wgObjectCaches store', false, true );
40
		$this->addArg( 'server[:port]', 'Memcached server to test, with optional port', false );
41
	}
42
43
	public function execute() {
44
		global $wgMainCacheType, $wgMemCachedTimeout, $wgObjectCaches;
45
46
		$cache = $this->getOption( 'cache' );
47
		$iterations = $this->getOption( 'i', 100 );
48
		if ( $cache ) {
49
			if ( !isset( $wgObjectCaches[$cache] ) ) {
50
				$this->error( "MediaWiki isn't configured with a cache named '$cache'", 1 );
51
			}
52
			$servers = $wgObjectCaches[$cache]['servers'];
53
		} elseif ( $this->hasArg() ) {
54
			$servers = [ $this->getArg() ];
55
		} elseif ( $wgMainCacheType === CACHE_MEMCACHED ) {
56
			global $wgMemCachedServers;
57
			$servers = $wgMemCachedServers;
58
		} elseif ( isset( $wgObjectCaches[$wgMainCacheType]['servers'] ) ) {
59
			$servers = $wgObjectCaches[$wgMainCacheType]['servers'];
60
		} else {
61
			$this->error( "MediaWiki isn't configured for Memcached usage", 1 );
62
		}
63
64
		# find out the longest server string to nicely align output later on
65
		$maxSrvLen = $servers ? max( array_map( 'strlen', $servers ) ) : 0;
0 ignored issues
show
Bug introduced by
The variable $servers 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...
66
67
		foreach ( $servers as $server ) {
68
			$this->output(
69
				str_pad( $server, $maxSrvLen ),
70
				$server # output channel
71
			);
72
73
			$mcc = new MemcachedClient( [
74
				'persistant' => true,
75
				'timeout' => $wgMemCachedTimeout
76
			] );
77
			$mcc->set_servers( [ $server ] );
78
			$set = 0;
79
			$incr = 0;
80
			$get = 0;
81
			$time_start = microtime( true );
82 View Code Duplication
			for ( $i = 1; $i <= $iterations; $i++ ) {
83
				if ( $mcc->set( "test$i", $i ) ) {
84
					$set++;
85
				}
86
			}
87 View Code Duplication
			for ( $i = 1; $i <= $iterations; $i++ ) {
88
				if ( !is_null( $mcc->incr( "test$i", $i ) ) ) {
89
					$incr++;
90
				}
91
			}
92 View Code Duplication
			for ( $i = 1; $i <= $iterations; $i++ ) {
93
				$value = $mcc->get( "test$i" );
94
				if ( $value == $i * 2 ) {
95
					$get++;
96
				}
97
			}
98
			$exectime = microtime( true ) - $time_start;
99
100
			$this->output( " set: $set   incr: $incr   get: $get time: $exectime", $server );
101
		}
102
	}
103
}
104
105
$maintClass = "McTest";
106
require_once RUN_MAINTENANCE_IF_MAIN;
107