Completed
Push — master ( 4e8d1d...8d6f18 )
by Philipp
03:00
created

Tongue::speaking()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pmochine\LaravelTongue;
4
5
use Illuminate\Foundation\Application;
6
use Pmochine\LaravelTongue\Misc\Config;
7
use Pmochine\LaravelTongue\Localization\Localization;
8
use Illuminate\Routing\Redirector;
9
use Pmochine\LaravelTongue\Localization\Locale;
10
use Pmochine\LaravelTongue\Misc\ConfigList;
11
12
class Tongue
13
{
14
    /**
15
     * The class that handles the locale methods
16
     *
17
     * @var Pmochine\LaravelTongue\Localization\Locale
0 ignored issues
show
Bug introduced by
The type Pmochine\LaravelTongue\P...gue\Localization\Locale was not found. Did you mean Pmochine\LaravelTongue\Localization\Locale? If so, make sure to prefix the type with \.
Loading history...
18
     */
19
    protected $locale;
20
21
    public function __construct(Application $app)
22
    {
23
        $this->locale = new Locale($app);
0 ignored issues
show
Documentation Bug introduced by
It seems like new Pmochine\LaravelTong...calization\Locale($app) of type Pmochine\LaravelTongue\Localization\Locale is incompatible with the declared type Pmochine\LaravelTongue\P...gue\Localization\Locale of property $locale.

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..

Loading history...
24
    }
25
26
    /**
27
     * Detects the tongue, the locale
28
     * of the User.
29
     *
30
     * @return Tongue :P
31
     */
32
    public function detect(): self
33
    {
34
        $this->speaks($this->locale->find());
35
36
        return $this;
37
    }
38
39
    /**
40
     * Gets the current speaking tongue...
41
     * (language code).
42
     *
43
     * @return  string
44
     */
45
    public function current($key = null): string
46
    {
47
        if (!$key) {
48
            return $this->locale->get();
49
        }
50
51
        return $this->speaking($key, $this->locale->get());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->speaking($..., $this->locale->get()) could return the type array|null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
52
    }
53
54
    /**
55
     * Gets the twist of the tongue.
56
     * Return the direction left or right.
57
     * e.g. for arabic language.
58
     *
59
     * @return string
60
     */
61
    public function leftOrRight(): string
62
    {
63
        return $this->locale->scriptDirection($this->current('script'));
64
    }
65
66
    /**
67
     * A tongue-twister is a phrase that is
68
     * designed to be difficult to articulate properly,
69
     * So lets just asume the user just can't speak the
70
     * language...
71
     *
72
     * @return bool (yes if its not speakable)
73
     */
74
    public function twister(): bool
75
    {
76
        $locale = Localization::fromUrl();
77
78
        if (tongue()->speaking('subdomains', $locale)) {
0 ignored issues
show
Bug introduced by
It seems like $locale can also be of type false; however, parameter $locale of Pmochine\LaravelTongue\Tongue::speaking() does only seem to accept null|string, 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 ignore-type  annotation

78
        if (tongue()->speaking('subdomains', /** @scrutinizer ignore-type */ $locale)) {
Loading history...
79
            //whitelisted subdomains! like admin.domain.com
80
            return false;
81
        }
82
83
        //custom subdomains with locale. gewinnen.domain.com -> de as locale
84
        if ($customLocale = tongue()->speaking('aliases', $locale)) {
85
            //but we need to check again if it is spoken or not
86
            return $this->current() !== $customLocale;
87
        }
88
89
        //fallback language is the same as the current language
90
        if (Config::beautify() && $this->current() === Config::fallbackLocale()) {
91
            //didn't found locale means browser is set to exmaple.com
92
            if (!$locale) {
93
                return false;
94
            }
95
            //browser is set to en.example.com but should be forced back to example.com
96
            if ($locale === Config::fallbackLocale()) {
97
                return true;
98
            }
99
        }
100
101
        //decipher from
102
        return $this->current() !== $locale;
103
    }
104
105
    /**
106
     * The user speaks locale language.
107
     * Set the locale.
108
     *
109
     * @param  string $locale
110
     * @return Tongue :P
111
     */
112
    public function speaks(string $locale): self
113
    {
114
        if (!$this->isSpeaking($locale)) {
115
            //locale does not exist.
116
            return abort(404);
0 ignored issues
show
Bug introduced by
Are you sure the usage of abort(404) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return abort(404) returns the type void which is incompatible with the type-hinted return Pmochine\LaravelTongue\Tongue.
Loading history...
117
        }
118
119
        $this->locale->save($locale);
120
121
        return $this;
122
    }
123
124
    /**
125
     * Used to return back to previous url.
126
     * e.g. if you change the language. its usefull.
127
     *
128
     * @return Illuminate\Routing\Redirector
0 ignored issues
show
Bug introduced by
The type Pmochine\LaravelTongue\I...nate\Routing\Redirector was not found. Did you mean Illuminate\Routing\Redirector? If so, make sure to prefix the type with \.
Loading history...
129
     */
130
    public function back(): Redirector
131
    {
132
        return dialect()->redirect(dialect()->redirectUrl(url()->previous()));
0 ignored issues
show
Bug Best Practice introduced by
The expression return dialect()->redire...Url(url()->previous())) returns the type Illuminate\Http\RedirectResponse which is incompatible with the type-hinted return Illuminate\Routing\Redirector.
Loading history...
133
    }
134
135
    /**
136
     * Gets the collection list of all languages,
137
     * the website speaks. Or give us the specific keys.
138
     *
139
     * @return collection|string|array|null
0 ignored issues
show
Bug introduced by
The type Pmochine\LaravelTongue\collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
140
     */
141
    public function speaking(string $key = null, string $locale = null)
142
    {
143
        return (new ConfigList)->lookup($key, $locale);
144
    }
145
146
147
    /**
148
     * Checks if your page is speaking the language.
149
     *
150
     * @param  string  $locale
151
     * @return bool
152
     */
153
    public function isSpeaking(string $locale): bool
154
    {
155
        return $this->speaking()->has($locale);
156
    }
157
}
158