FirstNodeSimplifier::isSimplifierFor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PPP\Module\TreeSimplifier;
4
5
use InvalidArgumentException;
6
use PPP\DataModel\AbstractNode;
7
use PPP\DataModel\FirstNode;
8
use PPP\DataModel\ResourceListNode;
9
10
/**
11
 * @licence MIT
12
 * @author Thomas Pellissier Tanon
13
 */
14
class FirstNodeSimplifier implements NodeSimplifier {
15
16
	/**
17
	 * @var NodeSimplifierFactory
18
	 */
19
	private $simplifierFactory;
20
21
	/**
22
	 * @param NodeSimplifierFactory $simplifierFactory
23
	 */
24 7
	public function __construct(NodeSimplifierFactory $simplifierFactory) {
25 7
		$this->simplifierFactory = $simplifierFactory;
26 7
	}
27
28
	/**
29
	 * @see NodeSimplifier::isSimplifierFor
30
	 */
31 6
	public function isSimplifierFor(AbstractNode $node) {
32 6
		return $node instanceof FirstNode;
33
	}
34
35
	/**
36
	 * @see NodeSimplifier::simplify
37
	 * @param FirstNode $node
38
	 */
39 4
	public function simplify(AbstractNode $node) {
40 4
		if(!$this->isSimplifierFor($node)) {
41 1
			throw new InvalidArgumentException('FirstNodeSimplifier can only simplify FirstNode');
42
		}
43
44 3
		$nodeSimplifier = $this->simplifierFactory->newNodeSimplifier();
45 3
		$operand = $nodeSimplifier->simplify($node->getOperand());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PPP\DataModel\AbstractNode as the method getOperand() does only exist in the following sub-classes of PPP\DataModel\AbstractNode: PPP\DataModel\FirstNode, PPP\DataModel\LastNode, PPP\DataModel\ReducerNode, PPP\DataModel\SortNode. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
46
47 3
		if($operand instanceof ResourceListNode) {
48 2
			if($operand->isEmpty()) {
49 1
				return $operand;
50
			} else {
51 1
				$resources = $operand->toArray();
52 1
				return new ResourceListNode(array(reset($resources)));
0 ignored issues
show
Documentation introduced by
array(reset($resources)) is of type array<integer,object<PPP...\ResourceNode>|false"}>, but the function expects a array<integer,object<PPP...odel\ResourceListNode>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
			}
54
		} else {
55 1
			return new FirstNode($operand);
56
		}
57
	}
58
}
59