Completed
Push — master ( 79320b...fce3d5 )
by Chin
06:22
created

helpers.php ➔ current_route()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Support\Facades\Route;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Route.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
4
use Illuminate\Support\Str;
5
6
if (! function_exists('current_route')) {
7
    /**
8
     * Retrieve the current route in another locale.
9
     *
10
     * @param  string  $locale
11
     * @return string
12
     */
13
    function current_route(string $locale = null): string
14
    {
15
        $route = Route::getCurrentRoute();
0 ignored issues
show
Bug introduced by
The method getCurrentRoute() does not exist on Illuminate\Support\Facades\Route. Did you maybe mean current()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
16
        $query = request()->getQueryString();
17
18
        if (! $route->getName() || ! in_array($locale, locales())) {
19
            return url($route->uri.($query ? "?{$query}" : null));
20
        }
21
22
        return localized_route(
23
            Str::replaceFirst(locale().'.', null, $route->getName()),
24
            $query,
25
            $locale
26
        );
27
    }
28
}
29
30
if (! function_exists('localized_route')) {
31
    /**
32
     * Retrieve a localized route.
33
     *
34
     * @param  string  $name
35
     * @param  mixed  $parameters
36
     * @param  string  $locale
37
     * @param  bool  $absolute
38
     * @return string
39
     */
40
    function localized_route(string $name, $parameters = [], string $locale = null, bool $absolute = true): string
41
    {
42
        if (! in_array($locale, locales())) {
43
            $locale = locale();
44
        }
45
46
        return route("$locale.$name", $parameters, $absolute);
47
    }
48
}
49