GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 841b7d...3ed1d3 )
by
unknown
13:44 queued 04:45
created

src/MessageLog.php (1 issue)

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
/**
4
 * File holding the Lingo\MessageLog class.
5
 *
6
 * This file is part of the MediaWiki extension Lingo.
7
 *
8
 * @copyright 2011 - 2018, Stephan Gambke
9
 * @license   GNU General Public License, version 2 (or any later version)
10
 *
11
 * The Lingo extension is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by the Free
13
 * Software Foundation; either version 2 of the License, or (at your option) any
14
 * later version.
15
 *
16
 * The Lingo extension is distributed in the hope that it will be useful, but
17
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19
 * details.
20
 *
21
 * You should have received a copy of the GNU General Public License along
22
 * with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 * @author Stephan Gambke
25
 *
26
 * @file
27
 * @ingroup Lingo
28
 */
29
30
namespace Lingo;
31
use Html;
32
use Parser;
33
use ParserOptions;
34
35
/**
36
 * This class holds messages (errors, warnings, notices) for Lingo
37
 *
38
 * Contains a static function to initiate the parsing.
39
 *
40
 * @ingroup Lingo
41
 */
42
class MessageLog {
43
44
	private $mMessages = [];
45
	private $mParser = null;
46
47
	const MESSAGE_ERROR = 1;
48
	const MESSAGE_WARNING = 2;
49
	const MESSAGE_NOTICE = 3;
50
51
	/**
52
	 * @param $message
53
	 * @param int $severity
54
	 */
55
	public function addMessage( $message, $severity = self::MESSAGE_NOTICE ) {
56
		$this->mMessages[] = [ $message, $severity ];
57
58
		// log errors and warnings in debug log
59
		if ( $severity == self::MESSAGE_WARNING ||
60
			$severity == self::MESSAGE_ERROR
61
		) {
62
			wfDebug( $message );
63
		}
64
	}
65
66
	/**
67
	 * @param $message
68
	 */
69
	public function addError( $message ) {
70
		$this->mMessages[] = [ $message, self::MESSAGE_ERROR ];
71
		wfDebug( "Error: $message\n" );
72
	}
73
74
	/**
75
	 * @param $message
76
	 */
77
	public function addWarning( $message ) {
78
		$this->mMessages[] = [ $message, self::MESSAGE_WARNING ];
79
		wfDebug( "Warning: $message\n" );
80
	}
81
82
	/**
83
	 * @param $message
84
	 */
85
	public function addNotice( $message ) {
86
		$this->mMessages[] = [ $message, self::MESSAGE_NOTICE ];
87
	}
88
89
	/**
90
	 * @param int $severity
91
	 * @param null $header
92
	 * @return null|string
93
	 */
94
	public function getMessagesFormatted( $severity = self::MESSAGE_WARNING, $header = null ) {
95
		global $wgTitle, $wgUser;
96
97
		$ret = '';
98
99
		foreach ( $this->mMessages as $message ) {
100
			if ( $message[ 1 ] <= $severity ) {
101
				$ret .= '* ' . $message[ 0 ] . "\n";
102
			}
103
		}
104
105
		if ( $ret != '' ) {
106
			if ( !$this->mParser ) {
107
				$parser = new Parser();
108
			}
109
110
			if ( $header == null ) {
111
				$header = '';
112
			} elseif ( $header != '' ) {
113
				$header = Html::rawElement( 'div', [ 'class' => 'heading' ], $header );
114
			}
115
116
			$ret = Html::rawElement( 'div', [ 'class' => 'messages' ],
117
				$header . "\n" .
118
				$ret
119
			);
120
121
			// FIXME: Variable 'parser' might have not been defined
122
			// FIXME: $parser->parse returns ParserOutput, not String
123
			$ret = $parser->parse( $ret, $wgTitle, ParserOptions::newFromUser( $wgUser ) );
0 ignored issues
show
The variable $parser 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...
124
		} else {
125
			// FIXME: Should probably return '' (and throw an error if necessary)
126
			$ret = null;
127
		}
128
129
		return $ret;
130
	}
131
132
}
133
134