Completed
Push — master ( 5a0c59...249923 )
by Jeroen De
115:19 queued 95:17
created

formats/time/SRF_Time.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
/**
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
	protected function getResultText( SMWQueryResult $res, $outputmode ) {
27
		$dataItems = $this->getSortKeys( $res );
28
29
		if ( empty( $dataItems ) ) {
30 2
			return $this->params['default'];
31 2
		}
32
33 2
		$sortKeys = array_keys( $dataItems );
34
35
		switch ( $this->mFormat ) {
36
			case 'latest':
37 2
				$result = max( $sortKeys );
38
				break;
39 2
			case 'earliest':
40 2
				$result = min( $sortKeys );
41 1
				break;
42 1
		}
43 1
44 1
		$dataValue = SMWDataValueFactory::getInstance()->newDataValueByItem( $dataItems[$result], null );
0 ignored issues
show
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 1
		return $dataValue->getLongHTMLText();
46
	}
47
48 2
	/**
49 2
	 * Returns an array with sortkeys for dates pointing to their source DataItems.
50
	 *
51
	 * @param SMWQueryResult $res
52
	 *
53
	 * @return array
54
	 */
55
	protected function getSortKeys( SMWQueryResult $res ) {
56
		$seconds = [];
57
58
		while ( $row = $res->getNext() ) {
59 2
			foreach( $row as /* SMWResultArray */ $resultArray ) {
60 2
				foreach ( $resultArray->getContent() as /* SMWDataItem */ $dataItem ) {
61
					if ( $dataItem->getDIType() === SMWDataItem::TYPE_TIME ) {
62 2
						$seconds[$dataItem->getSortKey()] = $dataItem;
63 2
					}
64 2
				}
65 2
			}
66 2
		}
67
68
		return $seconds;
69
	}
70
71
	/**
72 2
	 * @see SMWResultPrinter::getParamDefinitions
73
	 *
74
	 * @since 1.8
75
	 *
76
	 * @param $definitions array of IParamDefinition
77
	 *
78
	 * @return array of IParamDefinition|array
79
	 */
80
	public function getParamDefinitions( array $definitions ) {
81
		$params = parent::getParamDefinitions( $definitions );
82
83
		$params['limit'] = [
84 2
			'type' => 'integer',
85 2
			'default' => 1000,
86
			'message' => 'srf_paramdesc_limit',
87 2
		];
88
89
		return $params;
90
	}
91
92
}
93