bavix /
router
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bavix\Router; |
||
| 4 | |||
| 5 | use Bavix\Exceptions; |
||
| 6 | use Bavix\Exceptions\Runtime; |
||
| 7 | use Bavix\Router\Rules\PatternRule; |
||
| 8 | use Bavix\Router\Rules\PrefixRule; |
||
| 9 | |||
| 10 | class Loader |
||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected $rules = [ |
||
| 17 | 'http' => Rules\HttpRule::class, |
||
| 18 | 'prefix' => Rules\PrefixRule::class, |
||
| 19 | 'pattern' => Rules\PatternRule::class, |
||
| 20 | ]; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var null|Rule |
||
| 24 | */ |
||
| 25 | protected $parent; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $config; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Loader constructor. |
||
| 34 | * |
||
| 35 | * @param iterable $config |
||
| 36 | * @param null|Rule $parent |
||
| 37 | */ |
||
| 38 | 7 | public function __construct($config, ?Rule $parent = null) |
|
| 39 | { |
||
| 40 | 7 | $this->config = $config; |
|
|
0 ignored issues
–
show
|
|||
| 41 | 7 | $this->parent = $parent; |
|
| 42 | 7 | } |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @return PatternRule[] |
||
| 46 | */ |
||
| 47 | 7 | public function simplify(): array |
|
| 48 | { |
||
| 49 | 7 | $routes = []; |
|
| 50 | 7 | foreach ($this->routes() as $key => $route) { |
|
| 51 | 5 | $routes[] = $this->_simplify($route, $key); |
|
| 52 | } |
||
| 53 | |||
| 54 | 5 | return \array_merge(...$routes); |
|
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return \Generator |
||
| 59 | */ |
||
| 60 | 7 | public function routes(): \Generator |
|
| 61 | { |
||
| 62 | 7 | foreach ($this->config as $key => $item) { |
|
| 63 | 7 | if (empty($item['type'])) { |
|
| 64 | 1 | throw new Exceptions\NotFound\Data( |
|
| 65 | 1 | \sprintf('The type parameter for key `%s` is not found', $this->way($key)) |
|
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | 6 | yield $key => $this->rule($item['type'], $key, $item); |
|
| 70 | } |
||
| 71 | 5 | } |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param string $key |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | 1 | protected function way(string $key): string |
|
| 79 | { |
||
| 80 | 1 | if ($this->parent) { |
|
| 81 | return $this->parent->getName() . '.' . $key; |
||
| 82 | } |
||
| 83 | 1 | return $key; |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $type |
||
| 88 | * @param string $key |
||
| 89 | * @param array $item |
||
| 90 | * |
||
| 91 | * @return Rule |
||
| 92 | */ |
||
| 93 | 6 | public function rule(string $type, string $key, array $item): Rule |
|
| 94 | { |
||
| 95 | 6 | if (!$this->validate($type)) { |
|
| 96 | throw new Runtime(\sprintf('Undefined type `%s`', $type)); |
||
| 97 | } |
||
| 98 | |||
| 99 | 6 | $class = $this->rules[$type]; |
|
| 100 | 6 | return new $class($key, $item, $this->parent); |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $type |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | 6 | protected function validate(string $type): bool |
|
| 109 | { |
||
| 110 | 6 | return \array_key_exists($type, $this->rules); |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param Rule $rule |
||
| 115 | * @param string $key |
||
| 116 | * |
||
| 117 | * @return PatternRule[] |
||
| 118 | */ |
||
| 119 | 5 | protected function _simplify(Rule $rule, string $key): array |
|
| 120 | { |
||
| 121 | 5 | if ($rule instanceof PatternRule) { |
|
| 122 | 5 | return [$key => $rule]; |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var PrefixRule $rule |
||
| 127 | */ |
||
| 128 | 4 | $rules = []; |
|
| 129 | 4 | foreach ($rule->resolver() as $index => $item) { |
|
| 130 | 4 | $location = $key . '.' . $index; |
|
| 131 | 4 | $rules[] = $this->_simplify($item, $location); |
|
| 132 | } |
||
| 133 | |||
| 134 | 4 | return \array_merge(...$rules); |
|
| 135 | } |
||
| 136 | |||
| 137 | } |
||
| 138 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..