Completed
Pull Request — master (#349)
by mw
01:56
created

SRFTime::getResultText()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

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