Completed
Push — master ( 7bf2c4...ed6650 )
by Danilo
03:26
created

LocalizatedString   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getStr() 0 40 5
1
<?php
2
3
namespace PhpBotFramework\Localization;
4
5
/**
6
 * \addtogroup Modules
7
 * @{
8
 */
9
10
/** \class LocalizatedString
11
 */
12
trait LocalizatedString {
13
14
    /** @} */
15
16
    /**
17
     * \addtogroup Localization Localization
18
     * @{
19
     */
20
21
    public function getStr($index) {
22
23
        if (!isset($this->language)) {
24
25
            throw BotException("Language not set");
26
27
        }
28
29
        // If the language of the user is already set in the array containing localizated strings
30
        if (!isset($this->local[$this->language])) {
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...
Bug introduced by
The property language 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...
31
32
            // Is the bot using webhook?
33
            if (isset($this->webhook)) {
34
35
                // Load only the user language in array
36
                $this->loadSingleLanguage($this->localization_dir);
0 ignored issues
show
Bug introduced by
The property localization_dir 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...
Bug introduced by
It seems like loadSingleLanguage() 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...
37
38
            } else {
39
40
                // Load all localization files
41
                $this->loadLocalization($this->localization_dir);
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...
42
43
            }
44
45
        }
46
47
48
49
        // Check if the string needed exists
50
        if (isset($this->local[$this->language][$index])) {
51
52
            // If yes, return it
53
            return $this->local[$this->language][$index];
54
55
        }
56
57
        // Throw an error
58
        throw BotException("$index is not set for {$this->language}");
59
60
    }
61
62
    /** @} */
63
}
64