Completed
Push — master ( 2df768...364599 )
by Stephan
02:08
created

PurgePage.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace PurgePage;
4
5
class PurgePage {
6
7
	public static function init() {
8
		$GLOBALS['wgExtensionMessagesFiles']['PurgePageMagic'] = __DIR__ . '/PurgePage.magic.php';
9
	}
10
	
11
	public static function registerParserFunction( \Parser &$parser ) {
12
13
		$parser->setFunctionHook( 'purge', function ( $parser ) {
14
15
			$params = func_get_args();
16
17
			if ( isset( $params[ 0 ] ) && isset( $params[ 1 ] ) ) {
18
				$pageName = $params[ 1 ];
19
			}
20
21
			$title = \Title::newFromText( $pageName );
0 ignored issues
show
The variable $pageName 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...
22
23
			if ( $title->isContentPage() && $title->exists() ) {
24
				\WikiPage::factory( $title )->doPurge();
25
			}
26
27
		} );
28
	}
29
}
30