RecursiveOperatorNodeSimplifier::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
ccs 3
cts 3
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\OperatorNode;
8
9
/**
10
 * @licence MIT
11
 * @author Thomas Pellissier Tanon
12
 */
13
class RecursiveOperatorNodeSimplifier implements NodeSimplifier {
14
15
	/**
16
	 * @var NodeSimplifierFactory
17
	 */
18
	private $simplifierFactory;
19
20
	/**
21
	 * @param NodeSimplifierFactory $simplifierFactory
22
	 */
23 7
	public function __construct(NodeSimplifierFactory $simplifierFactory) {
24 7
		$this->simplifierFactory = $simplifierFactory;
25 7
	}
26
27
	/**
28
	 * @see NodeSimplifier::isSimplifierFor
29
	 */
30 6
	public function isSimplifierFor(AbstractNode $node) {
31 6
		return $node instanceof OperatorNode;
32
	}
33
34
	/**
35
	 * @see NodeSimplifier::simplify
36
	 * @param OperatorNode $node
37
	 */
38 3
	public function simplify(AbstractNode $node) {
39 3
		if(!$this->isSimplifierFor($node)) {
40 1
			throw new InvalidArgumentException('RecursiveOperatorNodeSimplifier can only simplify OperatorNode');
41
		}
42
43 2
		$nodeSimplifier = $this->simplifierFactory->newNodeSimplifier();
44
45 2
		$operands = array();
46 2
		foreach($node->getOperands() as $operand) {
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 getOperands() does only exist in the following sub-classes of PPP\DataModel\AbstractNode: PPP\DataModel\IntersectionNode, PPP\DataModel\OperatorNode, PPP\DataModel\UnionNode. 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...
47 2
			$operands[] = $nodeSimplifier->simplify($operand);
48 2
		}
49
50 2
		$class = get_class($node);
51 2
		return new $class($operands);
52
	}
53
}
54