Completed
Push — master ( 0a532a...959a2b )
by mw
122:08 queued 105:20
created

Query/ProfileAnnotator/NullProfileAnnotator.php (2 issues)

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
namespace SMW\Query\ProfileAnnotator;
4
5
use SMW\DIProperty;
6
use SMWDIContainer as DIContainer;
7
8
/**
9
 * @license GNU GPL v2+
10
 * @since 1.9
11
 *
12
 * @author mwjames
13
 */
14
class NullProfileAnnotator implements ProfileAnnotator {
15
16
	/**
17
	 * @var DIContainer
18
	 */
19
	private $container;
20
21
	/**
22
	 * @since 1.9
23
	 *
24
	 * @param DIContainer $container
25
	 */
26 80
	public function __construct( DIContainer $container ) {
27 80
		$this->container = $container;
28 80
	}
29
30
	/**
31
	 * @since 1.9
32
	 *
33
	 * @return array
34
	 */
35 1
	public function getErrors() {
36 1
		return $this->getSemanticData()->getErrors();
37
	}
38
39
	/**
40
	 * ProfileAnnotator::getProperty
41
	 *
42
	 * @since 1.9
43
	 *
44
	 * @return array
45
	 */
46 79
	public function getProperty() {
47 79
		return new DIProperty( '_ASK' );
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \SMW\DIProperty('_ASK'); (SMW\DIProperty) is incompatible with the return type declared by the interface SMW\Query\ProfileAnnotat...eAnnotator::getProperty of type SMW\Query\ProfileAnnotator\DIProperty.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
48
	}
49
50
	/**
51
	 * ProfileAnnotator::getContainer
52
	 *
53
	 * @since 1.9
54
	 *
55
	 * @return DIContainer
56
	 */
57 79
	public function getContainer() {
58 79
		return $this->container;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->container; (SMWDIContainer) is incompatible with the return type declared by the interface SMW\Query\ProfileAnnotat...Annotator::getContainer of type SMW\Query\ProfileAnnotator\DIContainer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
59
	}
60
61
	/**
62
	 * ProfileAnnotator::getSemanticData
63
	 *
64
	 * @since 1.9
65
	 *
66
	 * @return SemanticData
67
	 */
68 79
	public function getSemanticData() {
69 79
		return $this->container->getSemanticData();
70
	}
71
72
	/**
73
	 * ProfileAnnotator::addAnnotation
74
	 *
75
	 * @since 1.9
76
	 */
77 79
	public function addAnnotation() {
78 79
	}
79
80
}
81