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

ApprovedByPropertyAnnotator::addAnnotation()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 6
eloc 14
nc 12
nop 2
1
<?php
2
3
namespace SESP\PropertyAnnotators;
4
5
use ApprovedRevs;
6
use SESP\AppFactory;
7
use SESP\PropertyAnnotator;
8
use SESP\LogReader;
9
use SMW\DIWikiPage;
10
use SMWDataItem as DataItem;
11
use SMW\DIProperty;
12
use SMW\SemanticData;
13
use User;
14
15
/**
16
 * @private
17
 * @ingroup SESP
18
 *
19
 * @license GNU GPL v2+
20
 */
21
class ApprovedByPropertyAnnotator implements PropertyAnnotator {
22
23
	/**
24
	 * Predefined property ID
25
	 */
26
	const PROP_ID = '___APPROVEDBY';
27
28
	/**
29
	 * @var AppFactory
30
	 */
31
	private $appFactory;
32
33
	/**
34
	 * @var Integer|null
35
	 */
36
	private $approvedBy;
37
38
	/**
39
	 * @param AppFactory $appFactory
40
	 */
41
	public function __construct( AppFactory $appFactory ) {
42
		$this->appFactory = $appFactory;
43
	}
44
45
	/**
46
	 * @since 2.0
47
	 *
48
	 * @param User $approvedBy
49
	 */
50
	public function setApprovedRev( User $approvedBy ) {
51
		$this->approvedBy = $approvedBy;
0 ignored issues
show
Documentation Bug introduced by
It seems like $approvedBy of type object<User> is incompatible with the declared type integer|null of property $approvedBy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
	}
53
54
	/**
55
	 * {@inheritDoc}
56
	 */
57
	public function isAnnotatorFor( DIProperty $property ) {
58
		return $property->getKey() === self::PROP_ID;
59
	}
60
61
	/**
62
	 * {@inheritDoc}
63
	 */
64
	public function addAnnotation(
65
		DIProperty $property, SemanticData $semanticData
66
	) {
67
		if ( $this->approvedBy === null && class_exists( 'ApprovedRevs' ) ) {
68
			$logReader = new LogReader(
69
				$semanticData->getSubject()->getTitle(), 'approval'
0 ignored issues
show
Bug introduced by
It seems like $semanticData->getSubject()->getTitle() targeting SMW\DIWikiPage::getTitle() can also be of type null; however, SESP\LogReader::__construct() does only seem to accept object<SESP\Title>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
70
			);
71
			$this->approvedBy = $logReader->getUser();
0 ignored issues
show
Documentation Bug introduced by
It seems like $logReader->getUser() of type object<User> is incompatible with the declared type integer|null of property $approvedBy.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
		}
73
74
		if ( $this->approvedBy ) {
75
			$userPage = $this->approvedBy->getUserPage();
76
			if ( $userPage instanceof Title ) {
0 ignored issues
show
Bug introduced by
The class SESP\PropertyAnnotators\Title does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
77
				$dataItem = DIWikiPage::newFromTitle( $userPage );
78
			}
79
		}
80
81
		if ( $dataItem instanceof DataItem ) {
82
			$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...
83
		} else {
84
			$semanticData->removeProperty( $property );
85
		}
86
	}
87
}
88