Passed
Branch master (c65ffc)
by Dāvis
03:08
created

BaseEntity   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 99
Duplicated Lines 26.26 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 26
loc 99
rs 10
wmc 21

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 14 14 3
A __set() 10 10 2
A getClassName() 0 8 2
C getVariableByLocale() 0 25 8
A cleanText() 0 3 1
A __construct() 0 4 1
A getTranslations() 0 4 2
A getLocaleVar() 0 3 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Sludio\HelperBundle\Translatable\Entity;
4
5
use Sludio\HelperBundle\Translatable\Repository\TranslatableRepository as Sludio;
6
7
abstract class BaseEntity
8
{
9
    public $className;
10
    public $translates;
11
12
    public $localeArr = [
13
        'lv' => 'lv_LV',
14
        'en' => 'en_US',
15
        'ru' => 'ru_RU',
16
    ];
17
18
    abstract public function getId();
19
20
    public function __construct()
21
    {
22
        $this->translates = $this->getTranslations();
23
        $this->getClassName();
24
    }
25
26
    public function getClassName()
27
    {
28
        if (!$this->className) {
29
            $className = explode('\\', get_called_class());
30
            $this->className = strtolower(end($className));
31
        }
32
33
        return $this->className;
34
    }
35
36
    public function getLocaleVar($locale)
37
    {
38
        return isset($this->localeArr[$locale]) ? $this->localeArr[$locale] : $locale;
39
    }
40
41 View Code Duplication
    public function __get($property)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        if (!method_exists($this, 'get'.ucfirst($property))) {
44
            $locale = Sludio::getDefaultLocale();
45
        } else {
46
            $locale = strtolower(substr($property, -2));
47
            $property = substr($property, 0, -2);
48
        }
49
50
        if (in_array($locale, array_keys($this->localeArr))) {
51
            return $this->getVariableByLocale($property, $this->localeArr[$locale]);
52
        }
53
54
        return $this->{$property};
55
    }
56
57 View Code Duplication
    public function __set($property, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $locale = strtolower(substr($property, -2));
60
        if (in_array($locale, array_keys($this->localeArr))) {
61
            $property = substr($property, 0, -2);
62
            Sludio::updateTranslations(get_class($this), $this->localeArr[$locale], $property, $value, $this->getId());
63
        }
64
        $this->{$property} = $value;
65
66
        return $this;
67
    }
68
69
    protected function getTranslations()
70
    {
71
        if ($this->getId()) {
72
            return Sludio::getTranslations(get_called_class(), $this->getId());
73
        }
74
    }
75
76
    public function getVariableByLocale($variable, $locale = null, $returnOriginal = false)
77
    {
78
        $locale = $this->getLocaleVar($locale ?: Sludio::getDefaultLocale());
79
80
        if (!$this->translates && $this->getId()) {
81
            $this->translates = $this->getTranslations();
82
        }
83
84
        if (isset($this->translates[$locale][$variable])) {
85
            return $this->translates[$locale][$variable];
86
        }
87
88
        if ($returnOriginal) {
89
            $variables = explode('_', $variable);
90
            foreach ($variables as &$var) {
91
                $var = ucfirst($var);
92
            }
93
            $variable = implode('', $variables);
94
            $result = $this->{'get'.$variable}();
95
            if (is_numeric($result)) {
96
                return $result;
97
            }
98
        }
99
100
        return '';
101
    }
102
103
    public function cleanText($text)
104
    {
105
        return html_entity_decode(preg_replace('/\s+/S', ' ', str_replace(' ?', '', mb_convert_encoding(strip_tags($text), "UTF-8", "UTF-8"))));
106
    }
107
}
108