LaraCrafts /
laravel-geo-routes
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace LaraCrafts\GeoRoutes; |
||||
| 4 | |||||
| 5 | use BadMethodCallException; |
||||
| 6 | use Illuminate\Routing\Route; |
||||
| 7 | use LaraCrafts\GeoRoutes\Support\Facades\CallbackRegistrar; |
||||
|
0 ignored issues
–
show
|
|||||
| 8 | |||||
| 9 | /** |
||||
| 10 | * @mixin \Illuminate\Routing\Route |
||||
| 11 | */ |
||||
| 12 | class GeoRoute |
||||
| 13 | { |
||||
| 14 | use Concerns\HasCallback; |
||||
| 15 | use Concerns\ControlsAccess; |
||||
| 16 | |||||
| 17 | /** |
||||
| 18 | * Rule is applied. |
||||
| 19 | * |
||||
| 20 | * @var bool |
||||
| 21 | */ |
||||
| 22 | protected $applied; |
||||
| 23 | |||||
| 24 | /** |
||||
| 25 | * The countries to apply the rule for. |
||||
| 26 | * |
||||
| 27 | * @var array |
||||
| 28 | */ |
||||
| 29 | protected $countries; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * The route. |
||||
| 33 | * |
||||
| 34 | * @var \Illuminate\Routing\Route |
||||
| 35 | */ |
||||
| 36 | protected $route; |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * The rule's strategy. |
||||
| 40 | * |
||||
| 41 | * @var string |
||||
| 42 | */ |
||||
| 43 | protected $strategy; |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * Create a new GeoRoute instance. |
||||
| 47 | * |
||||
| 48 | * @param \Illuminate\Routing\Route $route |
||||
| 49 | * @param array $countries |
||||
| 50 | * @param string $strategy |
||||
| 51 | */ |
||||
| 52 | 80 | public function __construct(Route $route, array $countries, string $strategy) |
|||
| 53 | { |
||||
| 54 | 80 | $this->applied = false; |
|||
| 55 | 80 | $this->countries = array_map('strtoupper', $countries); |
|||
| 56 | 80 | $this->route = $route; |
|||
| 57 | 80 | $this->strategy = $strategy; |
|||
| 58 | 80 | } |
|||
| 59 | |||||
| 60 | /** |
||||
| 61 | * Dynamically call the underlying route. |
||||
| 62 | * |
||||
| 63 | * @param string $method |
||||
| 64 | * @param array $arguments |
||||
| 65 | * |
||||
| 66 | * @return mixed |
||||
| 67 | */ |
||||
| 68 | public function __call(string $method, array $arguments) |
||||
| 69 | { |
||||
| 70 | if (method_exists($this->route, $method) || Route::hasMacro($method)) { |
||||
| 71 | return $this->route->$method(...$arguments); |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | if (CallbackRegistrar::hasProxy($method)) { |
||||
| 75 | return $this->setCallback(CallbackRegistrar::callback($method), $arguments); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | throw new BadMethodCallException("Undefined method '$method'"); |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | /** |
||||
| 82 | * Destruct the GeoRoute instance and apply the middleware. |
||||
| 83 | */ |
||||
| 84 | 80 | public function __destruct() |
|||
| 85 | { |
||||
| 86 | 80 | $this->applyConstraint(); |
|||
| 87 | 80 | } |
|||
| 88 | |||||
| 89 | /** |
||||
| 90 | * Apply the geo-constraint to the route. |
||||
| 91 | */ |
||||
| 92 | 80 | protected function applyConstraint() |
|||
| 93 | { |
||||
| 94 | 80 | if ($this->applied || !$this->countries) { |
|||
|
0 ignored issues
–
show
The expression
$this->countries of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||||
| 95 | return; |
||||
| 96 | } |
||||
| 97 | |||||
| 98 | 80 | $action = $this->route->getAction(); |
|||
| 99 | 80 | $action['middleware'][] = 'geo'; |
|||
| 100 | 80 | $action['geo'] = [ |
|||
| 101 | 80 | 'strategy' => $this->strategy, |
|||
| 102 | 80 | 'countries' => (array)$this->countries, |
|||
| 103 | 80 | 'callback' => $this->callback, |
|||
| 104 | ]; |
||||
| 105 | |||||
| 106 | 80 | $this->route->setAction($action); |
|||
|
0 ignored issues
–
show
It seems like
$action can also be of type null; however, parameter $action of Illuminate\Routing\Route::setAction() 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...
|
|||||
| 107 | |||||
| 108 | 80 | $this->applied = true; |
|||
| 109 | 80 | } |
|||
| 110 | } |
||||
| 111 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: