Completed
Push — master ( 30f395...1b2b07 )
by Aitor Riba
02:13
created

Translation::__toString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 0
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, $vars = [])
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
        $this->vars = $vars;
0 ignored issues
show
Bug introduced by
The property vars does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
37
        // Checking whether from_lang or to_lang are set as app_locale.
38
39
        if ($this->from == 'app_locale') {
40
            $this->from = App::getLocale();
41
        }
42
43
        if ($this->to == 'app_locale') {
44
            $this->to = App::getLocale();
45
        }
46
    }
47
48
    /**
49
     * Setup debug value.
50
     *
51
     * @param bool $debug
52
     */
53
    public function debug($debug)
54
    {
55
        $this->debug = $debug;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Setup fromLang value.
62
     *
63
     * @param string $lang
64
     */
65
    public function from($lang)
66
    {
67
        $this->from = $lang;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Setup tolang value.
74
     *
75
     * @param string $lang
76
     */
77
    public function to($lang)
78
    {
79
        $this->to = $lang;
80
81
        return $this;
82
    }
83
84
    /**
85
     * Setup translator.
86
     *
87
     * @param string $translator
88
     */
89
    public function translator($translator)
90
    {
91
        $this->translator = $translator;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Setup save option.
98
     *
99
     * @param bool $save
100
     */
101
    public function Save($save)
102
    {
103
        $this->save = $save;
104
105
        return $this;
106
    }
107
108
    public function loadIfExists()
109
    {
110
        $existing = DB_Translation::where([
111
            'string' => $this->string,
112
            'from_lang' => $this->from,
113
            'to_lang' => $this->to,
114
            'translator' => $this->translator
115
        ])->get();
116
117
        if (count($existing) == 0) {
118
            return false;
119
        }
120
        if ($this->debug === true) {
121
            $this->translation = "<font style='color:#00CC00;'>Translation loaded from DB</font>";
122
        } else {
123
            $this->translation = ($existing[0]->translation);
124
        }
125
126
        return true;
127
    }
128
129
    /**
130
     * Function to save translations to DB.
131
     */
132
    public function checkSave()
133
    {
134
        if ($this->save === true) {
135
            $trans = new DB_Translation();
136
            $trans->string = $this->string;
0 ignored issues
show
Documentation introduced by
The property string does not exist on object<Aitor24\Laralang\Models\DB_Translation>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
137
            $trans->from_lang = $this->from;
0 ignored issues
show
Documentation introduced by
The property from_lang does not exist on object<Aitor24\Laralang\Models\DB_Translation>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
138
            $trans->to_lang = $this->to;
0 ignored issues
show
Documentation introduced by
The property to_lang does not exist on object<Aitor24\Laralang\Models\DB_Translation>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139
            $trans->translator = $this->translator;
0 ignored issues
show
Documentation introduced by
The property translator does not exist on object<Aitor24\Laralang\Models\DB_Translation>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
140
            $trans->translation = $this->translation;
0 ignored issues
show
Documentation introduced by
The property translation does not exist on object<Aitor24\Laralang\Models\DB_Translation>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
141
            $trans->save();
142
143
            // Checking debug setting to determinate how to output translation
144
145
            if ($this->debug === true) {
146
                $this->translation = "<font style='color:#00CC00;'> Translation saved on DB </font>";
147
            }
148
        } else {
149
            if ($this->debug === true) {
150
                $this->translation = "<font style='color:orange;'> Translation not saved on DB </font>";
151
            }
152
        }
153
    }
154
155
    /**
156
     * This fuction is called to know the status of host, and it would set translation if debug is true.
157
     *
158
     * @param string $host
159
     */
160
    public function checkHost($host)
161
    {
162
        $socket = @fsockopen($host, 80, $errno, $errstr, 30);
163
164
        if ($socket) {
165
            return true;
166
            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...
167
        } else {
168
            if ($this->debug === true) {
169
                $this->translation = "<font style='color:red;'>$this->translator host is down! </font>";
170
            }
171
        }
172
    }
173
174
    private function replaceVars() {
175
        foreach ($this->vars as $key => $var) {
176
            $this->translation = str_replace('VAR'.$key, $var, $this->translation);
177
        }
178
    }
179
180
    /**
181
     * This fuction is called by trans() function of Fadade Laralang
182
     * It would call run() function of this class and returns the translation.
183
     */
184
    public function __toString()
185
    {
186
        if ($this->from == $this->to) {
187
            if ($this->debug) {
188
                return "<font style='color:orange;'>Same in <> out language</font>";
189
            }
190
191
            return $this->string;
192
        } elseif (!$this->loadIfExists()) {
193
            $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\GoogleTrans, 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...
194
        }
195
        $this->replaceVars();
196
        return $this->translation;
197
    }
198
}
199