| 1 | <?php |
||
| 2 | |||
| 3 | namespace ByTIC\Models\SmartProperties\Definitions\Builders; |
||
| 4 | |||
| 5 | use ByTIC\Models\SmartProperties\Definitions\Definition; |
||
| 6 | use ByTIC\Models\SmartProperties\Properties\PropertiesFactory; |
||
| 7 | use Nip\Records\AbstractModels\RecordManager; |
||
| 8 | use Nip\Utility\Str; |
||
| 9 | use RecursiveDirectoryIterator; |
||
| 10 | use RecursiveIteratorIterator; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Class FolderBuilders |
||
| 14 | * @package ByTIC\Models\SmartProperties\Definitions\Builders |
||
| 15 | */ |
||
| 16 | class FolderBuilders |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var RecordManager |
||
| 20 | */ |
||
| 21 | protected $manager; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Definition |
||
| 25 | */ |
||
| 26 | protected $definition; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param RecordManager $manager |
||
| 30 | * @param string $field |
||
| 31 | * @param null|string $name |
||
| 32 | */ |
||
| 33 | public static function create($manager, $field, $name = null): Definition |
||
| 34 | { |
||
| 35 | $builder = new static(); |
||
| 36 | $builder->manager = $manager; |
||
| 37 | $builder->definition = $builder->newDefinition($manager); |
||
| 38 | $builder->definition->setField($field); |
||
| 39 | if ($name) { |
||
| 40 | $builder->definition->setName($name); |
||
| 41 | } |
||
| 42 | $builder->definition->setBuild(function () use ($builder) { |
||
| 43 | $items = $builder->buildItems(); |
||
| 44 | foreach ($items as $item) { |
||
| 45 | $builder->definition->addItem($item); |
||
| 46 | } |
||
| 47 | }); |
||
| 48 | return $builder->definition; |
||
| 49 | } |
||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * @param $manager |
||
| 54 | * @return Definition |
||
| 55 | */ |
||
| 56 | protected function newDefinition($manager): Definition |
||
| 57 | { |
||
| 58 | $definition = new Definition(); |
||
| 59 | $definition->setManager($manager); |
||
| 60 | |||
| 61 | return $definition; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | protected function buildItems() |
||
| 68 | { |
||
| 69 | $items = $this->getItemsFromFiles(); |
||
| 70 | |||
| 71 | $names = $this->getItemsNamesFromManager(); |
||
| 72 | if (!is_array($names)) { |
||
| 73 | return $items; |
||
| 74 | } |
||
| 75 | $return = []; |
||
| 76 | foreach ($names as $name) { |
||
| 77 | if (isset($items[$name])) { |
||
| 78 | $return[$name] = $items[$name]; |
||
| 79 | unset($items[$name]); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | return array_merge($return, $items); |
||
| 84 | } |
||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * @return array |
||
| 89 | */ |
||
| 90 | protected function getItemsFromFiles(): array |
||
| 91 | { |
||
| 92 | $directories = $this->definition->getPropertiesNamespaces(); |
||
|
0 ignored issues
–
show
|
|||
| 93 | $items = []; |
||
| 94 | foreach ($directories as $namespace => $directory) { |
||
|
0 ignored issues
–
show
|
|||
| 95 | $items = array_merge($items, $this->getItemsFromDirectory($directory, $namespace)); |
||
| 96 | } |
||
| 97 | return $items; |
||
| 98 | } |
||
| 99 | |||
| 100 | protected function getItemsFromDirectory($directory, $namespace): array |
||
| 101 | { |
||
| 102 | $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); |
||
| 103 | $names = []; |
||
| 104 | foreach ($files as $file) { |
||
| 105 | if ($file->isDir()) { |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | $name = str_replace($directory, '', $file->getPathname()); |
||
| 109 | $name = str_replace('.php', '', $name); |
||
| 110 | $name = trim($name, DIRECTORY_SEPARATOR . '\\'); |
||
| 111 | if ($this->isAbstractItemName($name)) { |
||
| 112 | continue; |
||
| 113 | } |
||
| 114 | $class = str_replace('/', '\\', $namespace . '' . $name); |
||
| 115 | if (class_exists($class)) { |
||
| 116 | $names[$name] = PropertiesFactory::forDefinition($this->definition, $class, $namespace); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | return array_unique($names); |
||
| 121 | } |
||
| 122 | |||
| 123 | |||
| 124 | /** |
||
| 125 | * @return array|boolean |
||
| 126 | */ |
||
| 127 | protected function getItemsNamesFromManager() |
||
| 128 | { |
||
| 129 | $methodName = 'get' . $this->definition->getName() . 'Names'; |
||
| 130 | if (method_exists($this->manager, $methodName)) { |
||
| 131 | return $this->manager->$methodName(); |
||
| 132 | } |
||
| 133 | |||
| 134 | return false; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $name |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | protected function isAbstractItemName(string $name): bool |
||
| 143 | { |
||
| 144 | if (in_array($name, ['Abstract', 'Generic'])) { |
||
| 145 | return true; |
||
| 146 | } |
||
| 147 | if (strpos($name, 'Abstract') === 0) { |
||
| 148 | return true; |
||
| 149 | } |
||
| 150 | if (Str::endsWith($name, 'Trait')) { |
||
| 151 | return true; |
||
| 152 | } |
||
| 153 | if (strpos($name, '\Abstract') !== false) { |
||
| 154 | return true; |
||
| 155 | } |
||
| 156 | if (strpos($name, DIRECTORY_SEPARATOR . 'Abstract') !== false) { |
||
| 157 | return true; |
||
| 158 | } |
||
| 159 | |||
| 160 | return false; |
||
| 161 | } |
||
| 162 | |||
| 163 | |||
| 164 | } |
||
| 165 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.