Passed
Push — master ( 0aa43e...eaba1f )
by Jean Paul
06:20
created

i18n::getText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 17
rs 10
1
<?php
2
3
namespace Coco\SourceWatcher\Utils;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
class i18n
8
{
9
    /**
10
     * @var i18n|null
11
     */
12
    private static ?i18n $instance = null;
13
14
    /**
15
     * @return i18n
16
     */
17
    public static function getInstance () : i18n
18
    {
19
        if ( is_null( static::$instance ) ) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instance to at least protected.
Loading history...
20
            static::$instance = new static;
21
        }
22
23
        return static::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::instance could return the type null which is incompatible with the type-hinted return Coco\SourceWatcher\Utils\i18n. Consider adding an additional type-check to rule them out.
Loading history...
24
    }
25
26
    public function getText ( string $language = "en_US", string $className, string $entry ) : string
27
    {
28
        $result = "";
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
29
30
        $data = Yaml::parseFile( __DIR__ . sprintf( "/../../resources/Locales/translations_%s.yml", $language ) );
31
32
        $classPathParts = explode( "\\", $className );
33
34
        $dataCopy = $data;
35
36
        foreach ( $classPathParts as $currentPart ) {
37
            $dataCopy = $dataCopy[$currentPart];
38
        }
39
40
        $result = $dataCopy[$entry];
41
42
        return $result;
43
    }
44
}
45