Completed
Push — master ( 85d1e0...3dc773 )
by
unknown
04:22
created

MessageLog::getMessagesFormatted()   C

Complexity

Conditions 7
Paths 21

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
ccs 0
cts 24
cp 0
rs 6.7272
cc 7
eloc 20
nc 21
nop 2
crap 56
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 - 2016, 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 = array();
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[] = array( $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[] = array( $message, self::MESSAGE_ERROR );
71
		wfDebug( "Error: $message\n" );
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $message instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
72
	}
73
74
	/**
75
	 * @param $message
76
	 */
77
	public function addWarning( $message ) {
78
		$this->mMessages[] = array( $message, self::MESSAGE_WARNING );
79
		wfDebug( "Warning: $message\n" );
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $message instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
80
	}
81
82
	/**
83
	 * @param $message
84
	 */
85
	public function addNotice( $message ) {
86
		$this->mMessages[] = array( $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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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', array( 'class' => 'heading' ), $header );
114
			}
115
116
			$ret = Html::rawElement( 'div', array( 'class' => 'messages' ),
117
				$header . "\n" .
118
				$ret
119
			);
120
121
			// FIXME: Variable 'parser' might have not been defined
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "Variable 'parser' might have not been defined"
Loading history...
122
			// FIXME: $parser->parse returns ParserOutput, not String
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "$parser->parse returns ParserOutput, not String"
Loading history...
123
			$ret = $parser->parse( $ret, $wgTitle, ParserOptions::newFromUser( $wgUser ) );
0 ignored issues
show
Bug introduced by
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)
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "Should probably return '' (and throw an error if necessary"
Loading history...
126
			$ret = null;
127
		}
128
129
		return $ret;
130
	}
131
132
}
133
134