Completed
Push — master ( 2623ee...a82f7c )
by Jeroen De
06:13 queued 04:44
created

EventExtractor   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 5
dl 0
loc 55
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extractEvents() 0 13 3
A getDates() 0 20 5
A getPropertyValueCollectionsWithDates() 0 9 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace ModernTimeline;
6
7
use ModernTimeline\ResultFacade\PropertyValueCollection;
8
use ModernTimeline\ResultFacade\Subject;
9
use ModernTimeline\ResultFacade\SubjectCollection;
10
use SMWDITime;
11
12
class EventExtractor {
13
14
	/**
15
	 * @param SubjectCollection $pages
16
	 * @return Event[]
17
	 */
18 7
	public function extractEvents( SubjectCollection $pages ): array {
19 7
		$events = [];
20
21 7
		foreach ( $pages->getSubjects() as $subject ) {
22 5
			[ $startDate, $endDate ] = $this->getDates( $subject );
0 ignored issues
show
Bug introduced by
The variable $startDate does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $endDate does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
23
24 5
			if ( $startDate !== null ) {
25 4
				$events[] = new Event( $subject, $startDate, $endDate );
26
			}
27
		}
28
29 7
		return $events;
30
	}
31
32 5
	private function getDates( Subject $subject ): array {
33 5
		$startDate = null;
34 5
		$endDate = null;
35
36 5
		foreach ( $this->getPropertyValueCollectionsWithDates( $subject ) as $propertyValues ) {
37 4
			$dataItem = $propertyValues->getDataItems()[0];
38
39 4
			if ( $dataItem instanceof SMWDITime ) {
0 ignored issues
show
Bug introduced by
The class SMWDITime does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
40 4
				if ( $startDate === null ) {
41 4
					$startDate = $dataItem;
42
				}
43 1
				else if ( $endDate === null ) {
44 1
					$endDate = $dataItem;
45 1
					break;
46
				}
47
			}
48
		}
49
50 5
		return [ $startDate, $endDate ];
51
	}
52
53
	/**
54
	 * @return PropertyValueCollection[]
55
	 */
56 5
	private function getPropertyValueCollectionsWithDates( Subject $subject ) {
57 5
		return array_filter(
58 5
			$subject->getPropertyValueCollections()->toArray(),
59 5
			function( PropertyValueCollection $pvc ) {
60 4
				return $pvc->getPrintRequest()->getTypeID() === '_dat'
61 4
					&& $pvc->getDataItems() !== [];
62 5
			}
63
		);
64
	}
65
66
}
67