ElementReferenceImpl   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 69
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getReferenceSource() 0 4 1
A __construct() 0 3 1
A getReferenceSourceChild() 0 3 1
A setReferenceTargetElement() 0 13 3
A clearReferenceTargetElement() 0 3 1
A setReferenceSource() 0 5 1
A getReferenceTargetElement() 0 15 3
1
<?php
2
3
namespace Xml\Impl\Type\Reference;
4
5
use Xml\Exception\{
6
    ModelException,
7
    ModelReferenceException
8
};
9
use Xml\Instance\ModelElementInstanceInterface;
10
use Xml\Impl\Instance\ModelElementInstanceImpl;
11
use Xml\Type\Child\ChildElementInterface;
12
use Xml\Type\Reference\ElementReferenceInterface;
13
14
class ElementReferenceImpl extends ElementReferenceCollectionImpl implements ElementReferenceInterface
15
{
16
    public function __construct(ChildElementInterface $referenceSourceCollection)
17
    {
18
        parent::__construct($referenceSourceCollection);
19
    }
20
21
    private function getReferenceSourceChild(): ChildElementInterface
22
    {
23
        return $this->getReferenceSourceCollection();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getReferenceSourceCollection() returns the type Xml\Type\Child\ChildElementCollectionInterface which includes types incompatible with the type-hinted return Xml\Type\Child\ChildElementInterface.
Loading history...
24
    }
25
26
    public function getReferenceSource(
27
        ModelElementInstanceInterface $referenceSourceParent
28
    ): ?ModelElementInstanceInterface {
29
        return $this->getReferenceSourceChild()->getChild($referenceSourceParent);
30
    }
31
32
    private function setReferenceSource(
33
        ModelElementInstanceInterface $referenceSourceParent,
34
        ModelElementInstanceInterface $referenceSource
35
    ): void {
36
        $this->getReferenceSourceChild()->setChild($referenceSourceParent, $referenceSource);
37
    }
38
39
    /**
40
     * @return mixed
41
     */
42
    public function getReferenceTargetElement(
43
        ModelElementInstanceInterface $referenceSourceParentElement
44
    ) {
45
        $referenceSource = $this->getReferenceSource($referenceSourceParentElement);
46
        if ($referenceSource !== null) {
47
            $identifier = $this->getReferenceIdentifier($referenceSource);
48
            $referenceTargetElement = $referenceSourceParentElement->getModelInstance()
49
                ->getModelElementById($identifier);
50
            if ($referenceTargetElement !== null) {
51
                return $referenceTargetElement;
52
            } else {
53
                throw new ModelException(sprintf("Unable to find a model element instance for id %s", $identifier));
54
            }
55
        } else {
56
            return null;
57
        }
58
    }
59
60
    /**
61
     * @param ModelElementInstanceInterface $referenceSourceElement
62
     * @param mixed $referenceTargetElement
63
     */
64
    public function setReferenceTargetElement(
65
        ModelElementInstanceInterface $referenceSourceParentElement,
66
        $referenceTargetElement
67
    ): void {
68
        $modelInstance = $referenceSourceParentElement->getModelInstance();
69
        $identifier = $this->referenceTargetAttribute->getValue($referenceTargetElement);
70
        $existingElement = $modelInstance->getModelElementById($identifier);
71
        if ($existingElement === null || !$existingElement->equals($referenceTargetElement)) {
72
            throw new ModelReferenceException("Cannot create reference to model element");
73
        } else {
74
            $referenceSourceElement = $modelInstance->newInstance($this->getReferenceSourceElementType());
0 ignored issues
show
Bug introduced by
The call to Xml\ModelInstanceInterface::newInstance() has too few arguments starting with id. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

74
            /** @scrutinizer ignore-call */ 
75
            $referenceSourceElement = $modelInstance->newInstance($this->getReferenceSourceElementType());

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
75
            $this->setReferenceSource($referenceSourceParentElement, $referenceSourceElement);
76
            $this->setReferenceIdentifier($referenceSourceElement, $identifier);
77
        }
78
    }
79
80
    public function clearReferenceTargetElement(ModelElementInstanceImpl $referenceSourceParentElement): void
81
    {
82
        $this->getReferenceSourceChild()->removeChild($referenceSourceParentElement);
83
    }
84
}
85