PropertyTypeLookup::getTypeOfProperty()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Queryr\EntityStore;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\DBALException;
7
use Wikibase\DataModel\Entity\PropertyId;
8
9
/**
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
class PropertyTypeLookup {
14
15
	private $connection;
16
	private $tableName;
17
18
	/**
19
	 * This constructor is package private. Construction is done via EntityStoreFactory.
20
	 *
21
	 * @param Connection $connection
22
	 * @param string $tableName
23
	 */
24 4
	public function __construct( Connection $connection, $tableName ) {
25 4
		$this->connection = $connection;
26 4
		$this->tableName = $tableName;
27 4
	}
28
29
	/**
30
	 * @param PropertyId $id
31
	 *
32
	 * @return string|null
33
	 * @throws PropertyTypeLookupException
34
	 */
35 4
	public function getTypeOfProperty( PropertyId $id ) {
36 4
		$query = $this->buildQuery( $id );
37
38
		try {
39 4
			$queryResult = $query->execute();
40
		}
41 1
		catch ( DBALException $ex ) {
42 1
			throw new PropertyTypeLookupException( $ex->getMessage(), $ex );
43
		}
44
45 3
		return $this->getTypeFromResult( $queryResult );
0 ignored issues
show
Bug introduced by
It seems like $queryResult defined by $query->execute() on line 39 can also be of type integer; however, Queryr\EntityStore\Prope...up::getTypeFromResult() does only seem to accept object<Traversable>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
46
	}
47
48 4
	private function buildQuery( PropertyId $id ) {
49 4
		return $this->connection->createQueryBuilder()
50 4
			->select( 'property_type' )
51 4
			->from( $this->tableName )
52 4
			->where( 'property_id = ?' )
53 4
			->setParameter( 0, (int)$id->getNumericId() );
54
	}
55
56 3
	private function getTypeFromResult( \Traversable $rows ) {
57 3
		$rows = iterator_to_array( $rows );
58
59 3
		if ( count( $rows ) < 1 ) {
60 1
			return null;
61
		}
62
63 2
		return reset( $rows )['property_type'];
64
	}
65
66
}