Passed
Push — master ( 69ba4d...961cf5 )
by Vitaly
08:05
created

TreeNode::key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 31.03.17 at 09:23
5
 */
6
namespace samsonframework\stringconditiontree;
7
8
/**
9
 * Class TreeNode
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
class TreeNode extends IterableTreeNode
14
{
15
    /** @var self Pointer to parent node */
16
    public $parent;
17
18
    /** @var string Tree node value */
19
    public $value;
20
21
    /** @var string Tree node identifier */
22
    public $identifier;
23
24
    /** @var string Tree node full value */
25
    public $fullValue;
26
27
    /**
28
     * TreeNode constructor.
29
     *
30
     * @param string   $value  Node value
31
     * @param string   $identifier Node identifier
32
     * @param TreeNode $parent Pointer to parent node
33
     */
34
    public function __construct(string $value = '', string $identifier = '', self $parent = null)
35
    {
36
        $this->value = $value;
37
        $this->parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parent can also be of type object<self>. However, the property $parent is declared as type object<samsonframework\s...conditiontree\TreeNode>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
38
        $this->identifier = $identifier;
39
40
        if ($parent !== null) {
41
            $this->fullValue = $parent->fullValue . ($value !== StringConditionTree::SELF_NAME ? $value : '');
42
        }
43
    }
44
45
    /**
46
     * Append new node instance and return it.
47
     *
48
     * @param string $value Node value
49
     * @param string $identifier Node identifier
50
     *
51
     * @return TreeNode New created node instance
52
     */
53
    public function append(string $value, string $identifier): self
54
    {
55
        return $this->children[$value] = new self($value, $identifier, $this);
0 ignored issues
show
Documentation introduced by
$this is of type this<samsonframework\str...conditiontree\TreeNode>, but the function expects a null|object<self>.

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...
56
    }
57
58
    /**
59
     * Convert tree node to associative array.
60
     *
61
     * @return array Tree structure as hashed array
62
     */
63
    public function toArray(): array
64
    {
65
        $result = [];
66
67
        if ($this->identifier !== '') {
68
            $result[StringConditionTree::SELF_NAME] = $this->identifier;
69
        }
70
71
        /** @var self $child */
72
        foreach ($this as $key => $child) {
73
            $result[$key] = $child->toArray();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class samsonframework\stringco...ontree\IterableTreeNode as the method toArray() does only exist in the following sub-classes of samsonframework\stringco...ontree\IterableTreeNode: samsonframework\stringconditiontree\TreeNode. 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...
74
        }
75
76
        return $result;
77
    }
78
79
80
}
81