Completed
Push — master ( 7880aa...3f359c )
by Thomas
05:14
created

MissingObjectTripleNodeSimplifier::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
cc 1
eloc 3
nc 1
nop 2
crap 1.0156
1
<?php
2
3
namespace PPP\Wikidata\TreeSimplifier;
4
5
use InvalidArgumentException;
6
use PPP\DataModel\AbstractNode;
7
use PPP\DataModel\MissingNode;
8
use PPP\DataModel\ResourceListNode;
9
use PPP\DataModel\TripleNode;
10
use PPP\Module\TreeSimplifier\NodeSimplifier;
11
use PPP\Wikidata\ValueParsers\ResourceListNodeParser;
12
use PPP\Wikidata\WikibaseResourceNode;
13
14
/**
15
 * Simplifies a triple node when the object is missing.
16
 *
17
 * @licence GPLv2+
18
 * @author Thomas Pellissier Tanon
19
 */
20
class MissingObjectTripleNodeSimplifier implements NodeSimplifier {
21
22
	/**
23
	 * @var ResourceListNodeParser
24
	 */
25
	private $resourceListNodeParser;
26
27
	/**
28
	 * @var ResourceListForEntityProperty
29
	 */
30
	private $resourceListForEntityProperty;
31
32
	/**
33
	 * @param ResourceListNodeParser $resourceListNodeParser
34
	 * @param ResourceListForEntityProperty $resourceListForEntityProperty
35
	 */
36 13
	public function __construct(ResourceListNodeParser $resourceListNodeParser, ResourceListForEntityProperty $resourceListForEntityProperty) {
37 13
		$this->resourceListNodeParser = $resourceListNodeParser;
38 13
		$this->resourceListForEntityProperty = $resourceListForEntityProperty;
39
	}
40
41
	/**
42
	 * @see NodeSimplifier::isSimplifierFor
43
	 */
44 12
	public function isSimplifierFor(AbstractNode $node) {
45 12
		return $node instanceof TripleNode &&
46
			$node->getSubject() instanceof ResourceListNode &&
47
			$node->getPredicate() instanceof ResourceListNode &&
48 6
			$node->getObject() instanceof MissingNode;
49 12
	}
50
51
	/**
52
	 * @see NodeSimplifier::doSimplification
53
	 */
54 7
	public function simplify(AbstractNode $node) {
55
		if(!$this->isSimplifierFor($node)) {
56
			throw new InvalidArgumentException('MissingObjectTripleNodeSimplifier can only simplify TripleNode with a missing object');
57
		}
58
59
		return $this->doSimplification($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PPP\DataModel\AbstractNode> is not a sub-type of object<PPP\DataModel\TripleNode>. It seems like you assume a child class of the class PPP\DataModel\AbstractNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
60 7
	}
61
62 4
	private function doSimplification(TripleNode $node) {
63
		$subjectNodes = $this->resourceListNodeParser->parse($node->getSubject(), 'wikibase-item');
0 ignored issues
show
Compatibility introduced by
$node->getSubject() of type object<PPP\DataModel\AbstractNode> is not a sub-type of object<PPP\DataModel\ResourceListNode>. It seems like you assume a child class of the class PPP\DataModel\AbstractNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
64
		$propertyNodes = $this->resourceListNodeParser->parse($node->getPredicate(), 'wikibase-property');
0 ignored issues
show
Compatibility introduced by
$node->getPredicate() of type object<PPP\DataModel\AbstractNode> is not a sub-type of object<PPP\DataModel\ResourceListNode>. It seems like you assume a child class of the class PPP\DataModel\AbstractNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
65 4
		$results = array();
66
67
		foreach($subjectNodes as $subject) {
68
			foreach($propertyNodes as $predicate) {
69
				$results[] = $this->getNodesForObject($subject, $predicate);
70
			}
71
		}
72
73
		$node = new ResourceListNode($results);
74 4
		return $node;
75 4
	}
76
77 4
	protected function getNodesForObject(WikibaseResourceNode $subject, WikibaseResourceNode $predicate) {
78 4
		return $this->resourceListForEntityProperty->getForEntityProperty(
79
			$subject->getDataValue()->getEntityId(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface DataValues\DataValue as the method getEntityId() does only exist in the following implementations of said interface: Wikibase\DataModel\Entity\EntityIdValue.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
80
			$predicate->getDataValue()->getEntityId()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface DataValues\DataValue as the method getEntityId() does only exist in the following implementations of said interface: Wikibase\DataModel\Entity\EntityIdValue.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
81
		);
82 4
	}
83
}
84