Completed
Pull Request — master (#95)
by Mark A.
07:56
created

ApprovedDatePropertyAnnotator::isAnnotatorFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use SMW\DIProperty;
6
use SMW\SemanticData;
7
use SMWDataItem as DataItem;
8
use SMWDITime as DITime;
9
use SESP\PropertyAnnotator;
10
use SESP\AppFactory;
11
12
/**
13
 * @private
14
 * @ingroup SESP
15
 *
16
 * @license GNU GPL v2+
17
 */
18
class ApprovedDatePropertyAnnotator implements PropertyAnnotator {
19
20
	/**
21
	 * Predefined property ID
22
	 */
23
	const PROP_ID = '___APPROVEDDATE';
24
25
	/**
26
	 * @var AppFactory
27
	 */
28
	private $appFactory;
29
30
	/**
31
	 * @var Integer|null
32
	 */
33
	private $approvedDate;
34
35
	/**
36
	 * @param AppFactory $appFactory
37
	 */
38
	public function __construct( AppFactory $appFactory ) {
39
		$this->appFactory = $appFactory;
40
	}
41
42
	/**
43
	 * @since 2.0
44
	 *
45
	 * @param Integer $approvedDate
46
	 */
47
	public function setApprovedDate( $approvedDate ) {
48
		$this->approvedDate = $approvedDate;
49
	}
50
51
	/**
52
	 * {@inheritDoc}
53
	 */
54
	public function isAnnotatorFor( DIProperty $property ) {
55
		return $property->getKey() === self::PROP_ID;
56
	}
57
58
	/**
59
	 * {@inheritDoc}
60
	 */
61
	public function addAnnotation(
62
		DIProperty $property, SemanticData $semanticData
63
	) {
64
		if ( $this->approvedDate === null && class_exists( 'ApprovedRevs' ) ) {
65
			$logReader = new LogReader(
66
				$semanticData->getSubject()->getTitle(), 'approval'
67
			);
68
			$this->approvedDate = $logReader->getDate();
69
		}
70
71
		if ( $this->approvedDate ) {
72
			$dataItem = new DITime(
73
				DITime::CM_GREGORIAN,
74
				$this->approvedDate->format( 'Y' ),
75
				$this->approvedDate->format( 'm' ),
76
				$this->approvedDate->format( 'd' ),
77
				$this->approvedDate->format( 'H' ),
78
				$this->approvedDate->format( 'i' )
79
			);
80
		}
81
82
		if ( $dataItem instanceof DataItem ) {
83
			$semanticData->addPropertyObjectValue( $property, $dataItem );
0 ignored issues
show
Bug introduced by
The variable $dataItem 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...
84
		} else {
85
			$semanticData->removeProperty( $property );
86
		}
87
	}
88
}
89