AttributeReferenceBuilderImpl   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A performModelBuild() 0 15 2
A __construct() 0 5 1
A build() 0 4 1
1
<?php
2
3
namespace Xml\Impl\Type\Reference;
4
5
use Xml\ModelInterface;
6
use Xml\Exception\ModelException;
7
use Xml\Impl\ModelBuildOperationInterface;
8
use Xml\Impl\Type\Attribute\AttributeImpl;
9
use Xml\Type\Reference\{
10
    AttributeReferenceBuilderInterface,
11
    AttributeReferenceInterface
12
};
13
14
class AttributeReferenceBuilderImpl implements AttributeReferenceBuilderInterface, ModelBuildOperationInterface
15
{
16
    private $referenceSourceAttribute;
17
    protected $attributeReferenceImpl;
18
    private $referenceTargetElement;
19
20
    public function __construct(AttributeImpl $referenceSourceAttribute, string $referenceTargetElement)
21
    {
22
        $this->referenceSourceAttribute = $referenceSourceAttribute;
23
        $this->referenceTargetElement = $referenceTargetElement;
24
        $this->attributeReferenceImpl = new AttributeReferenceImpl($referenceSourceAttribute);
25
    }
26
27
    public function build(): AttributeReferenceInterface
28
    {
29
        $this->referenceSourceAttribute->registerOutgoingReference($this->attributeReferenceImpl);
30
        return $this->attributeReferenceImpl;
31
    }
32
33
    public function performModelBuild(ModelInterface $model): void
34
    {
35
        $referenceTargetType = $model->getType($this->referenceTargetElement);
36
        $this->attributeReferenceImpl->setReferenceTargetElementType($referenceTargetType);
0 ignored issues
show
Bug introduced by
It seems like $referenceTargetType can also be of type null; however, parameter $referenceTargetElementType of Xml\Impl\Type\Reference\...enceTargetElementType() does only seem to accept Xml\Impl\Type\ModelElementTypeImpl, maybe add an additional type check? ( Ignorable by Annotation )

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

36
        $this->attributeReferenceImpl->setReferenceTargetElementType(/** @scrutinizer ignore-type */ $referenceTargetType);
Loading history...
37
38
        $idAttribute = $referenceTargetType->getAttribute("id");
39
        if ($idAttribute !== null) {
40
            $idAttribute->registerIncoming($this->attributeReferenceImpl);
0 ignored issues
show
Bug introduced by
The method registerIncoming() does not exist on Xml\Type\Attribute\AttributeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Xml\Type\Attribute\AttributeInterface. ( Ignorable by Annotation )

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

40
            $idAttribute->/** @scrutinizer ignore-call */ 
41
                          registerIncoming($this->attributeReferenceImpl);
Loading history...
41
            $this->attributeReferenceImpl->setReferenceTargetAttribute($idAttribute);
42
        } else {
43
            throw new ModelException(
44
                sprintf(
45
                    "Element type %s:%s has no id attribute",
46
                    $referenceTargetType->getTypeNamespace(),
47
                    $referenceTargetType->getTypeName()
48
                )
49
            );
50
        }
51
    }
52
}
53