| 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
Loading history...
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
Loading history...
|
|||||||
| 32 | [' . implode(',', array_keys($items)) . ']' |
||||||
|
0 ignored issues
–
show
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
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
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
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
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
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
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
Loading history...
|
|||||||
| 73 | return PropertiesFactory::forDefinition($this, $type); |
||||||
|
0 ignored issues
–
show
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
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
|
|||||||
| 78 | * |
||||||
| 79 | * @return string |
||||||
| 80 | */ |
||||||
| 81 | public function getPropertyClass($type = null): string |
||||||
| 82 | { |
||||||
| 83 | $type = $type ?: $this->getDefaultValue(); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 84 | $type = str_replace(DIRECTORY_SEPARATOR, '\\', $type); |
||||||
| 85 | |||||||
| 86 | return $this->getPropertyItemsRootNamespace() . inflector()->classify($type); |
||||||
|
0 ignored issues
–
show
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
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
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
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 |