simplifiedTripleProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 106
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 106
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 77
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PPP\Wikidata\TreeSimplifier;
4
5
use DataValues\TimeValue;
6
use DateTime;
7
use PPP\DataModel\AbstractNode;
8
use PPP\DataModel\IntersectionNode;
9
use PPP\DataModel\JsonLdResourceNode;
10
use PPP\DataModel\MissingNode;
11
use PPP\DataModel\ResourceListNode;
12
use PPP\DataModel\StringResourceNode;
13
use PPP\DataModel\TripleNode;
14
use PPP\DataModel\UnionNode;
15
use PPP\Wikidata\WikibaseResourceNode;
16
use Wikibase\DataModel\Entity\EntityIdValue;
17
use Wikibase\DataModel\Entity\ItemId;
18
use Wikibase\DataModel\Entity\PropertyId;
19
20
/**
21
 * @covers PPP\Wikidata\TreeSimplifier\SpecificTripleNodeSimplifier
22
 *
23
 * @licence AGPLv3+
24
 * @author Thomas Pellissier Tanon
25
 */
26
class SpecificTripleNodeSimplifierTest extends NodeSimplifierBaseTest {
27
28
	protected function buildSimplifier() {
29
		$resourceListNodeParserMock = $this->getMockBuilder('PPP\Wikidata\ValueParsers\ResourceListNodeParser')
30
			->disableOriginalConstructor()
31
			->getMock();
32
		$resourceListForEntityPropertyMock = $this->getMockBuilder('PPP\Wikidata\TreeSimplifier\ResourceListForEntityProperty')
33
			->disableOriginalConstructor()
34
			->getMock();
35
36
		date_default_timezone_set('UTC');
37
		return new SpecificTripleNodeSimplifier($resourceListNodeParserMock, $resourceListForEntityPropertyMock, new DateTime('2015-03-12'));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \PPP\Wikidata...ateTime('2015-03-12')); (PPP\Wikidata\TreeSimplif...ficTripleNodeSimplifier) is incompatible with the return type declared by the abstract method PPP\Wikidata\TreeSimplif...seTest::buildSimplifier of type PPP\Wikidata\TreeSimplifier\NodeSimplifier.

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...
38
	}
39
40
	public function simplifiableProvider() {
41
		return array(
42
			array(
43
				new TripleNode(new ResourceListNode(), new ResourceListNode(), new MissingNode())
44
			)
45
		);
46
	}
47
48
	public function nonSimplifiableProvider() {
49
		return array(
50
			array(
51
				new MissingNode()
52
			)
53
		);
54
	}
55
56
	/**
57
	 * @dataProvider simplifiedTripleProvider
58
	 */
59
	public function testSimplify(AbstractNode $outputNode, TripleNode $inputNode) {
60
		$resourceListNodeParserMock = $this->getMockBuilder('PPP\Wikidata\ValueParsers\ResourceListNodeParser')
61
			->disableOriginalConstructor()
62
			->getMock();
63
		$resourceListNodeParserMock->expects($this->any())
64
			->method('parse')
65
			->with($this->equalTo(new ResourceListNode(array(new StringResourceNode('Douglas Adams')))))
66
			->will($this->returnValue(new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42')))))));
67
		$resourceListForEntityPropertyMock = $this->getMockBuilder('PPP\Wikidata\TreeSimplifier\ResourceListForEntityProperty')
68
			->disableOriginalConstructor()
69
			->getMock();
70
		$resourceListForEntityPropertyMock->expects($this->any())
71
			->method('getForEntityProperty')
72
			->with($this->equalTo(new ItemId('Q42')), $this->equalTo('P569'))
73
			->will($this->returnValue(new ResourceListNode(array(new WikibaseResourceNode('', new TimeValue('+00000001952-03-11T00:00:00Z', 0, 0, 0, TimeValue::PRECISION_DAY, 'http://www.wikidata.org/entity/Q1985786'))))));
74
75
		date_default_timezone_set('UTC');
76
		$simplifier = new SpecificTripleNodeSimplifier($resourceListNodeParserMock, $resourceListForEntityPropertyMock, new DateTime('2015-03-12'));
77
78
		$this->assertEquals(
79
			$outputNode,
80
			$simplifier->simplify($inputNode)
81
		);
82
	}
83
84
	public function simplifiedTripleProvider() {
85
		return array(
86
			array(
87
				new TripleNode(
88
					new ResourceListNode(array(new StringResourceNode('a'))),
89
					new ResourceListNode(array(new StringResourceNode('foo'))),
90
					new MissingNode()
91
				),
92
				new TripleNode(
93
					new ResourceListNode(array(new StringResourceNode('a'))),
94
					new ResourceListNode(array(new StringResourceNode('foo'))),
95
					new MissingNode()
96
				)
97
			),
98
			array(
99
				new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42'))))),
100
				new TripleNode(
101
					new ResourceListNode(array(new StringResourceNode('Douglas Adams'))),
102
					new ResourceListNode(array(new StringResourceNode('name'))),
103
					new MissingNode()
104
				)
105
			),
106
			array(
107
				new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42'))))),
108
				new TripleNode(
109
					new ResourceListNode(array(new StringResourceNode('Douglas Adams'))),
110
					new ResourceListNode(array(new StringResourceNode('definition'))),
111
					new MissingNode()
112
				)
113
			),
114
			array(
115
				new UnionNode(array(
116
					new ResourceListNode(array(new WikibaseResourceNode('Douglas Adams', new EntityIdValue(new ItemId('Q42'))))),
117
					new TripleNode(
118
						new ResourceListNode(array(new StringResourceNode('Douglas Adams'))),
119
						new ResourceListNode(array(new StringResourceNode('foo'))),
120
						new MissingNode()
121
					)
122
				)),
123
				new TripleNode(
124
					new ResourceListNode(array(new StringResourceNode('Douglas Adams'))),
125
					new ResourceListNode(array(new StringResourceNode('name'), new StringResourceNode('foo'))),
126
					new MissingNode()
127
				)
128
			),
129
			array(
130
				new IntersectionNode(array(
131
					new TripleNode(
132
						new ResourceListNode(array(new StringResourceNode('Douglas Adams'))),
133
						new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P40'))))),
134
						new MissingNode()
135
					),
136
					new TripleNode(
137
						new MissingNode(),
138
						new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P21'))))),
139
						new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q6581097')))))
140
					)
141
				)),
142
				new TripleNode(
143
					new ResourceListNode(array(new StringResourceNode('Douglas Adams'))),
144
					new ResourceListNode(array(new StringResourceNode('son'))),
145
					new MissingNode()
146
				)
147
			),
148
			array(
149
				new IntersectionNode(array(
150
					new TripleNode(
151
						new ResourceListNode(array(new StringResourceNode('Douglas Adams'))),
152
						new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P40'))))),
153
						new MissingNode()
154
					),
155
					new TripleNode(
156
						new MissingNode(),
157
						new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new PropertyId('P21'))))),
158
						new ResourceListNode(array(new WikibaseResourceNode('', new EntityIdValue(new ItemId('Q6581072')))))
159
					)
160
				)),
161
				new TripleNode(
162
					new ResourceListNode(array(new StringResourceNode('Douglas Adams'))),
163
					new ResourceListNode(array(new StringResourceNode('daughter'))),
164
					new MissingNode()
165
				)
166
			),
167
			array(
168
				new ResourceListNode(array(
169
					new JsonLdResourceNode(
170
						'63',
171
						(object) array(
172
							'@context' => 'http://schema.org',
173
							'@type' => 'Duration',
174
							'name' => '63',
175
							'http://www.w3.org/1999/02/22-rdf-syntax-ns#value' => (object) array(
176
								'@type' => 'Duration',
177
								'@value' => 'P63Y0M1D'
178
							)
179
						)
180
					)
181
				)),
182
				new TripleNode(
183
					new ResourceListNode(array(new StringResourceNode('Douglas Adams'))),
184
					new ResourceListNode(array(new StringResourceNode('age'))),
185
					new MissingNode()
186
				)
187
			),
188
		);
189
	}
190
}
191