Completed
Push — master ( 8470e3...ad08f4 )
by Danilo
03:56
created

LocalizedString   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 0
dl 0
loc 44
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
getLanguageRedis() 0 1 ?
loadCurrentLocalization() 0 1 ?
A getStr() 0 15 3
1
<?php
2
3
/*
4
 * This file is part of the PhpBotFramework.
5
 *
6
 * PhpBotFramework is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, version 3.
9
 *
10
 * PhpBotFramework is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace PhpBotFramework\Localization;
20
21
/**
22
 * \addtogroup Modules
23
 * @{
24
 */
25
26
/** \class LocalizedString
27
 */
28
trait LocalizedString
29
{
30
    /** @} */
31
32
    abstract public function getLanguageRedis(string $default_language = 'en', int $expiring_time = 86400) : string;
33
    abstract public function loadCurrentLocalization(string $dir = '') : bool;
34
35
    /**
36
     * \addtogroup Localization Localization
37
     * @{
38
     */
39
40
    /** \brief Current user/group language. */
41
    public $language;
42
43
    /** \brief (<i>Internal</i>) True if the bot is using webhook? */
44
    protected $_is_webhook;
45
46
    /**
47
     * \brief Get a localized string giving an index.
48
     * \details Using LocalizedString::language this method get the string of the index given localized string in language of the current user/group.
49
     * This method will load the language first, using PhpBotFramework\Localization\Language::getLanguage(), if the language has not been set.
50
     * If the localization file for the user/group language has not been load yet, it will load it (load only the single localization file if the bot is using webhook, load all otherwise).
51
     * @param string $index Index of the localized string to get.
52
     * @return string Localized string in the current user/group language.
53
     */
54
    public function getStr($index)
55
    {
56
        if (!isset($this->language)) {
57
            $this->getLanguage();
0 ignored issues
show
Bug introduced by
The method getLanguage() does not exist on PhpBotFramework\Localization\LocalizedString. Did you maybe mean getLanguageRedis()?

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...
58
        }
59
60
        $this->loadLocalization();
0 ignored issues
show
Bug introduced by
It seems like loadLocalization() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
61
62
        // Check if the requested string exists
63
        if (isset($this->local[$this->language][$index])) {
64
            return $this->local[$this->language][$index];
0 ignored issues
show
Bug introduced by
The property local 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...
65
        }
66
67
        throw BotException("Index '$index' is not set for {$this->language}");
68
    }
69
70
    /** @} */
71
}
72