Completed
Push — master ( 0c873a...10905b )
by Anılcan
04:51
created

FormerHelper   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 138
Duplicated Lines 15.22 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 21
loc 138
c 0
b 0
f 0
wmc 16
lcom 2
cbo 3
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getViewPath() 0 12 2
A config() 0 6 1
A getFieldClassFromRules() 0 14 4
A getThemeOrDefault() 0 4 2
A getLabel() 0 7 2
A getPlaceholder() 11 11 2
A getText() 10 10 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 AnilcanCakir\Former;
4
5
use AnilcanCakir\Former\Contracts\FormerHelper as Contract;
6
use Illuminate\Contracts\Config\Repository as Config;
7
use Illuminate\Contracts\Translation\Translator;
8
use Illuminate\Contracts\View\Factory as View;
9
10
class FormerHelper implements Contract
11
{
12
    /**
13
     * @var View
14
     */
15
    protected $view;
16
17
    /**
18
     * @var Translator
19
     */
20
    protected $translator;
21
22
    /**
23
     * @var Config
24
     */
25
    protected $config;
26
27
    public function __construct(View $view, Translator $translator, Config $config)
28
    {
29
        $this->view = $view;
30
        $this->translator = $translator;
31
        $this->config = $config;
32
    }
33
34
    /**
35
     * Get view path.
36
     *
37
     * @param string|null $view
38
     * @param null $theme
39
     * @return string
40
     */
41
    public function getViewPath($view = null, $theme = null): string
42
    {
43
        // Update the theme variable
44
        $theme = $this->getThemeOrDefault($theme);
45
46
        $path = "former.{$theme}.{$view}";
47
        if ($this->view->exists($path)) {
48
            return $path;
49
        }
50
51
        return "former::{$theme}.{$view}";
52
    }
53
54
    /**
55
     * Get config value by given key.
56
     *
57
     * @param string $key
58
     * @return mixed
59
     */
60
    public function config($key)
61
    {
62
        return $this->config->get(
63
            "former.{$key}"
64
        );
65
    }
66
67
    /**
68
     * Get field class from rules.
69
     *
70
     * @param array $rules
71
     * @return string
72
     */
73
    public function getFieldClassFromRules(array $rules): string
74
    {
75
        foreach ($this->config('fields') as $input => $types)
76
        {
77
            foreach ($rules as $rule)
78
            {
79
                if (in_array($rule, $types)) {
80
                    return $input;
81
                }
82
            }
83
        }
84
85
        return $this->config('default_field');
86
    }
87
88
    /**
89
     * Get given theme or default theme.
90
     *
91
     * @param null|string $theme
92
     * @return string
93
     */
94
    public function getThemeOrDefault($theme = null): string
95
    {
96
        return $theme ?: $this->config('theme');
97
    }
98
99
    /**
100
     * Get the label of field by name.
101
     *
102
     * @param string $name
103
     * @return string
104
     */
105
    public function getLabel($name): string
106
    {
107
        $key = "validation.labels.{$name}";
108
        $trans = $this->translator->trans($key);
109
110
        return $trans == $key ? $this->translator->trans("validation.attributes.{$name}") : $trans;
111
    }
112
113
    /**
114
     * Get the placeholder of field by name.
115
     *
116
     * @param string $name
117
     * @return string|null
118
     */
119 View Code Duplication
    public function getPlaceholder($name)
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...
120
    {
121
        $key = "validation.placeholders.{$name}";
122
        $trans = $this->translator->trans($key);
123
124
        if ($trans != $key) {
125
            return $trans;
126
        }
127
128
        return null;
129
    }
130
131
    /**
132
     * Get the text of field by name.
133
     *
134
     * @param string $name
135
     * @return string|null
136
     */
137 View Code Duplication
    public function getText($name)
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...
138
    {
139
        $key = "validation.texts.{$name}";
140
        $trans = $this->translator->trans($key);
141
142
        if ($trans != $key) {
143
            return $trans;
144
        }
145
        return null;
146
    }
147
}