Completed
Push — master ( de2d91...9fc933 )
by Aitor Riba
01:56
created

Translation::from()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Aitor24\Laralang\Builder;
4
5
use Aitor24\Laralang\Models\DB_Translation;
6
use App;
7
8
class Translation
9
{
10
    /**
11
     * Setup public vars.
12
     */
13
    public $translation;
14
    public $translator;
15
    public $string;
16
    public $debug;
17
    public $from;
18
    public $to;
19
    public $save;
20
21
    /**
22
     * Setup default values.
23
     *
24
     * @param string $string
25
     */
26
    public function __construct($string)
27
    {
28
        $this->translator = config('laralang.default.translator');
29
        $this->debug = config('laralang.default.debug');
30
        $this->save = config('laralang.default.autosave');
31
        $this->from = config('laralang.default.from_lang');
32
        $this->to = config('laralang.default.to_lang');
33
        $this->string = $string;
34
        $this->translation = $string;
35
36
        // Checking whether from_lang or to_lang are set as app_locale.
37
38
        if ($this->from == 'app_locale') {
39
            $this->from = App::getLocale();
40
        }
41
42
        if ($this->to == 'app_locale') {
43
            $this->to = App::getLocale();
44
        }
45
    }
46
47
    /**
48
     * Setup debug value.
49
     *
50
     * @param bool $debug
51
     */
52
    public function debug($debug)
53
    {
54
        $this->debug = $debug;
55
56
        return $this;
57
    }
58
59
    /**
60
     * Setup fromLang value.
61
     *
62
     * @param string $lang
63
     */
64
    public function from($lang)
65
    {
66
        $this->from = $lang;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Setup tolang value.
73
     *
74
     * @param string $lang
75
     */
76
    public function to($lang)
77
    {
78
        $this->to = $lang;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Setup translator.
85
     *
86
     * @param string $translator
87
     */
88
    public function translator($translator)
89
    {
90
        $this->translator = $translator;
91
92
        return $this;
93
    }
94
95
    /**
96
     * Setup save option.
97
     *
98
     * @param bool $save
99
     */
100
    public function Save($save)
101
    {
102
        $this->save = $save;
103
104
        return $this;
105
    }
106
107
    public function loadIfExists()
108
    {
109
        $existing = DB_Translation::where('string', '=', $this->string)
110
        ->where('from_lang', '=', $this->from)
111
        ->where('to_lang', '=', $this->to)
112
        ->where('translator', '=', $this->translator)->get();
113
114
        if (count($existing) == 0) {
115
            return false;
116
        }
117
        if ($this->debug === true) {
118
            $this->translation = "<font style='color:#00CC00;'>Translation loaded from DB</font>";
119
        } else {
120
            $this->translation = ($existing[0]->translation);
121
        }
122
123
        return true;
124
    }
125
126
    /**
127
     * Function to save translations to DB.
128
     */
129
    public function checkSave()
130
    {
131
        if ($this->save === true) {
132
            $trans = new DB_Translation();
133
            $trans->string = $this->string;
134
            $trans->from_lang = $this->from;
135
            $trans->to_lang = $this->to;
136
            $trans->translator = $this->translator;
137
            $trans->translation = $this->translation;
138
            $trans->save();
139
140
            // Checking debug setting to determinate how to output translation
141
142
            if ($this->debug === true) {
143
                $this->translation = "<font style='color:#00CC00;'> Translation saved on DB </font>";
144
            }
145
        } else {
146
            if ($this->debug === true) {
147
                $this->translation = "<font style='color:orange;'> Translation not saved on DB </font>";
148
            }
149
        }
150
    }
151
152
    /**
153
     * This fuction is called to know the status of host, and it would set translation if debug is true.
154
     *
155
     * @param string $host
156
     */
157
    public function checkHost($host)
158
    {
159
        $socket = @fsockopen($host, 80, $errno, $errstr, 30);
160
161
        if ($socket) {
162
            return true;
163
            fclose($socket);
0 ignored issues
show
Unused Code introduced by
fclose($socket); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
164
        } else {
165
            if ($this->debug === true) {
166
                $this->translation = "<font style='color:red;'>$this->translator host is down! </font>";
167
            }
168
        }
169
    }
170
171
    /**
172
     * This fuction is called by trans() function of Fadade Laralang
173
     * It would call run() function of this class and returns the translation.
174
     */
175
    public function __toString()
176
    {
177
        if ($this->from == $this->to) {
178
            return $this->string;
179
        } elseif (!$this->loadIfExists()) {
180
            $this->main();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Aitor24\Laralang\Builder\Translation as the method main() does only exist in the following sub-classes of Aitor24\Laralang\Builder\Translation: Aitor24\Laralang\Builder\ApertiumTrans, Aitor24\Laralang\Builder\MymemoryTrans. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
181
        }
182
183
        return $this->translation;
184
    }
185
}
186