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

SRFTime   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 0
loc 83
ccs 24
cts 27
cp 0.8889
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 5 1
A getResultText() 0 21 4
A getSortKeys() 0 15 5
A getParamDefinitions() 0 11 1
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
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...
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