SRFTime::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Formats that returns a time.
5
 *
6
 * @licence GNU GPL v3+
7
 * @author nischayn22
8
 * @author Jeroen De Dauw < [email protected] >
9
 */
10
class SRFTime extends SMWResultPrinter {
11
12
	/**
13
	 * (non-PHPdoc)
14
	 * @see SMWResultPrinter::getName()
15
	 */
16
	public function getName() {
17
		// Give grep a chance to find the usages:
18
		// srf_printername_latest, srf_printername_earliest
19
		return wfMessage( 'srf_printername_' . $this->mFormat )->text();
20
	}
21
22
	/**
23
	 * (non-PHPdoc)
24
	 * @see SMWResultPrinter::getResultText()
25
	 */
26 2
	protected function getResultText( SMWQueryResult $res, $outputmode ) {
27 2
		$dataItems = $this->getSortKeys( $res );
28
29 2
		if ( empty( $dataItems ) ) {
30
			return $this->params['default'];
31
		}
32
33 2
		$sortKeys = array_keys( $dataItems );
34
35 2
		switch ( $this->mFormat ) {
36 2
			case 'latest':
37 1
				$result = max( $sortKeys );
38 1
				break;
39 1
			case 'earliest':
40 1
				$result = min( $sortKeys );
41 1
				break;
42
		}
43
44 2
		$dataValue = SMWDataValueFactory::getInstance()->newDataValueByItem( $dataItems[$result], null );
0 ignored issues
show
Bug introduced by Jeroen De Dauw
The variable $result 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...
45 2
		return $dataValue->getLongHTMLText();
46
	}
47
48
	/**
49
	 * Returns an array with sortkeys for dates pointing to their source DataItems.
50
	 *
51
	 * @param SMWQueryResult $res
52
	 *
53
	 * @return array
54
	 */
55 2
	protected function getSortKeys( SMWQueryResult $res ) {
56 2
		$seconds = [];
57
58 2
		while ( $row = $res->getNext() ) {
59 2
			foreach ( $row as /* SMWResultArray */
60
					  $resultArray ) {
61 2
				foreach ( $resultArray->getContent() as /* SMWDataItem */
62
						  $dataItem ) {
63 2
					if ( $dataItem->getDIType() === SMWDataItem::TYPE_TIME ) {
64 2
						$seconds[$dataItem->getSortKey()] = $dataItem;
65
					}
66
				}
67
			}
68
		}
69
70 2
		return $seconds;
71
	}
72
73
	/**
74
	 * @see SMWResultPrinter::getParamDefinitions
75
	 *
76
	 * @since 1.8
77
	 *
78
	 * @param $definitions array of IParamDefinition
79
	 *
80
	 * @return array of IParamDefinition|array
81
	 */
82 2
	public function getParamDefinitions( array $definitions ) {
83 2
		$params = parent::getParamDefinitions( $definitions );
84
85 2
		$params['limit'] = [
86
			'type' => 'integer',
87
			'default' => 1000,
88
			'message' => 'srf_paramdesc_limit',
89
		];
90
91 2
		return $params;
92
	}
93
94
}
95