malef /
associate
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Malef\Associate\DoctrineOrm\Association; |
||
| 4 | |||
| 5 | use Tree\Builder\NodeBuilder; |
||
| 6 | use Tree\Node\Node; |
||
| 7 | use Tree\Node\NodeInterface; |
||
| 8 | |||
| 9 | class AssociationTreeBuilder |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var NodeBuilder |
||
| 13 | */ |
||
| 14 | protected $nodeBuilder; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var NodeInterface |
||
| 18 | */ |
||
| 19 | protected $rootNode; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool |
||
| 23 | */ |
||
| 24 | protected $isAllowedToAlterAssociation; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var NodeInterface[] |
||
| 28 | */ |
||
| 29 | protected $divergeNodes = []; |
||
| 30 | |||
| 31 | public function __construct() |
||
| 32 | { |
||
| 33 | $this->rootNode = new Node(); |
||
| 34 | $this->isAllowedToAlterAssociation = false; |
||
| 35 | $this->nodeBuilder = new NodeBuilder($this->rootNode); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @throws \Exception |
||
| 40 | */ |
||
| 41 | public function associate(string $relationshipName): self |
||
| 42 | { |
||
| 43 | $this->nodeBuilder->tree(new Association($relationshipName)); |
||
| 44 | $this->isAllowedToAlterAssociation = true; |
||
| 45 | |||
| 46 | return $this; |
||
| 47 | } |
||
| 48 | |||
| 49 | public function diverge(): self |
||
| 50 | { |
||
| 51 | $this->divergeNodes[] = $this->nodeBuilder->getNode(); |
||
| 52 | $this->isAllowedToAlterAssociation = false; |
||
| 53 | |||
| 54 | return $this; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @throws \Exception |
||
| 59 | */ |
||
| 60 | public function endDiverge(): self |
||
| 61 | { |
||
| 62 | if (!$this->divergeNodes) { |
||
|
0 ignored issues
–
show
|
|||
| 63 | throw new \Exception(); |
||
| 64 | } |
||
| 65 | |||
| 66 | $lastDivergeNode = array_pop($this->divergeNodes); |
||
| 67 | while ($this->nodeBuilder->getNode() !== $lastDivergeNode) { |
||
| 68 | $this->nodeBuilder->end(); |
||
| 69 | } |
||
| 70 | $this->isAllowedToAlterAssociation = false; |
||
| 71 | |||
| 72 | return $this; |
||
| 73 | } |
||
| 74 | |||
| 75 | public function create(): AssociationTree |
||
| 76 | { |
||
| 77 | return new AssociationTree($this->rootNode); |
||
| 78 | } |
||
| 79 | } |
||
| 80 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.