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; |
|
|
|
|
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); |
|
|
|
|
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(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
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 theid
property of an instance of theAccount
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.