HasProperties::newProperty()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 1
nop 1
1
<?php
2
3
namespace ByTIC\Models\SmartProperties\Definitions\Definition;
4
5
use ByTIC\Models\SmartProperties\Properties\AbstractProperty\Generic;
6
use ByTIC\Models\SmartProperties\Properties\AbstractProperty\Generic as Property;
7
use ByTIC\Models\SmartProperties\Properties\PropertiesFactory;
8
use Exception;
9
10
/**
11
 * Trait HasProperties
12
 * @package ByTIC\Models\SmartProperties\Definitions\Definition
13
 */
14
trait HasProperties
15
{
16
    protected $items = null;
17
    protected $itemsAliases = [];
18
19
    /**
20
     * @param $name
21
     *
22
     * @return Property
23
     * @throws Exception
24
     */
25
    public function getItem($name): Property
26
    {
27
        $items = $this->getItems();
28
        if (!$this->hasItem($name)) {
29
            throw new Exception(
30
                'Bad Item [' . $name . '] for smart property 
31
                [' . $this->getManager()->getController() . '::' . $this->getName() . ']
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

31
                [' . $this->getManager()->getController() . '::' . $this->/** @scrutinizer ignore-call */ getName() . ']
Loading history...
Bug introduced by
It seems like getManager() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

31
                [' . $this->/** @scrutinizer ignore-call */ getManager()->getController() . '::' . $this->getName() . ']
Loading history...
32
                [' . implode(',', array_keys($items)) . ']'
0 ignored issues
show
Bug introduced by
It seems like $items can also be of type null; however, parameter $array of array_keys() does only seem to accept array, 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

32
                [' . implode(',', array_keys(/** @scrutinizer ignore-type */ $items)) . ']'
Loading history...
33
            );
34
        }
35
        if (isset($this->itemsAliases[$name])) {
36
            $name = $this->itemsAliases[$name];
37
        }
38
        return $items[$name];
39
    }
40
41
    /**
42
     * @return null|Property[]
43
     */
44
    public function getItems(): ?array
45
    {
46
        $this->checkBuild();
0 ignored issues
show
Bug introduced by
It seems like checkBuild() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
        $this->/** @scrutinizer ignore-call */ 
47
               checkBuild();
Loading history...
47
48
        if ($this->items == null) {
49
            $this->initItems();
50
        }
51
52
        return $this->items;
53
    }
54
55
    public function initItems()
56
    {
57
        $names = $this->getPlaces();
0 ignored issues
show
Bug introduced by
It seems like getPlaces() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

57
        /** @scrutinizer ignore-call */ 
58
        $names = $this->getPlaces();
Loading history...
58
        $this->items = [];
59
        foreach ($names as $name) {
60
            $object = $this->newProperty($name);
61
            $this->addItem($object);
62
        }
63
    }
64
65
    /**
66
     * @param string $type
67
     *
68
     * @return Property
69
     */
70
    public function newProperty($type = null): Property
71
    {
72
        $type = $type ?: $this->getDefaultValue();
0 ignored issues
show
Bug introduced by
It seems like getDefaultValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

72
        $type = $type ?: $this->/** @scrutinizer ignore-call */ getDefaultValue();
Loading history...
73
        return PropertiesFactory::forDefinition($this, $type);
0 ignored issues
show
Bug introduced by
The call to ByTIC\Models\SmartProper...actory::forDefinition() has too few arguments starting with baseNamespace. ( Ignorable by Annotation )

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

73
        return PropertiesFactory::/** @scrutinizer ignore-call */ forDefinition($this, $type);

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...
74
    }
75
76
    /**
77
     * @param null $type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type is correct as it would always require null to be passed?
Loading history...
78
     *
79
     * @return string
80
     */
81
    public function getPropertyClass($type = null): string
82
    {
83
        $type = $type ?: $this->getDefaultValue();
0 ignored issues
show
introduced by
$type is of type null, thus it always evaluated to false.
Loading history...
84
        $type = str_replace(DIRECTORY_SEPARATOR, '\\', $type);
85
86
        return $this->getPropertyItemsRootNamespace() . inflector()->classify($type);
0 ignored issues
show
Bug introduced by
It seems like getPropertyItemsRootNamespace() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

86
        return $this->/** @scrutinizer ignore-call */ getPropertyItemsRootNamespace() . inflector()->classify($type);
Loading history...
87
    }
88
89
    /**
90
     * @param Generic $object
91
     */
92
    public function addItem(Property $object)
93
    {
94
        $this->items[$object->getName()] = $object;
95
        $this->addPlace($object->getName());
0 ignored issues
show
Bug introduced by
It seems like addPlace() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

95
        $this->/** @scrutinizer ignore-call */ 
96
               addPlace($object->getName());
Loading history...
96
        $aliases = $object->getAliases();
97
        foreach ($aliases as $alias) {
98
            $this->itemsAliases[$alias] = $object->getName();
99
        }
100
    }
101
102
    /**
103
     * @param $name
104
     * @return bool
105
     */
106
    public function hasItem($name): bool
107
    {
108
        $items = $this->getItems();
109
110
        return isset($items[$name]) || isset($this->itemsAliases[$name]);
111
    }
112
113
114
}
115