components.PasswordEntropy   F
last analyzed

Complexity

Conditions 21

Size

Total Lines 80
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 62
nop 2
dl 0
loc 80
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like components.PasswordEntropy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
// Code generated by templ - DO NOT EDIT.
2
3
// templ: version: v0.2.663
4
package components
5
6
//lint:file-ignore SA4006 This context is only used if a nested component is present.
7
8
import "github.com/a-h/templ"
9
import "context"
10
import "io"
11
import "bytes"
12
13
import "strconv"
14
15
func RegisterComponent() templ.Component {
16
	return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
17
		templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
18
		if !templ_7745c5c3_IsBuffer {
19
			templ_7745c5c3_Buffer = templ.GetBuffer()
20
			defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
21
		}
22
		ctx = templ.InitializeContext(ctx)
23
		templ_7745c5c3_Var1 := templ.GetChildren(ctx)
24
		if templ_7745c5c3_Var1 == nil {
25
			templ_7745c5c3_Var1 = templ.NopComponent
26
		}
27
		ctx = templ.ClearChildren(ctx)
28
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div class=\"flex flex-col gap-4 p-4 md:p-8\" data-testid=\"registerComponent\"><form hx-post=\"/register\" hx-swap=\"afterend\"><div class=\"\"><label class=\"label\"><span id=\"email-error\" class=\"label-text\">Email</span></label> <input class=\"input-bordered input w-full transition duration-100\" placeholder=\"Enter your email\" type=\"email\" name=\"email\" required maxlength=\"254\"></div><div class=\"\"><label class=\"label\"><span id=\"username-error\" class=\"label-text\">Username</span></label> <input class=\"input-bordered input w-full transition duration-100\" placeholder=\"Enter your username\" required maxlength=\"50\" minlength=\"3\" type=\"username\" name=\"username\"></div><div class=\"\"><label class=\"label\"><span id=\"password-error\" class=\"label-text\">Password</span></label> <input class=\"input-bordered input w-full transition duration-100\" hx-post=\"/register/password\" hx-trigger=\"input changed delay:500ms\" hx-swap=\"afterend\" placeholder=\"Enter your password\" type=\"password\" name=\"password\" minlength=\"10\" maxlength=\"72\" required><div id=\"password-entropy\"><span class=\"label-text\">Strength of password</span> <progress id=\"pb\" class=\"progress progress-error w-full\" value=\"6\" max=\"100\"></progress></div></div><div class=\"pt-6\"><button class=\"btn-primary btn transition duration-100 w-full\">Register</button></div></form></div>")
29
		if templ_7745c5c3_Err != nil {
30
			return templ_7745c5c3_Err
31
		}
32
		if !templ_7745c5c3_IsBuffer {
33
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
34
		}
35
		return templ_7745c5c3_Err
36
	})
37
}
38
39
func UsernameError(err string) templ.Component {
40
	return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
41
		templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
42
		if !templ_7745c5c3_IsBuffer {
43
			templ_7745c5c3_Buffer = templ.GetBuffer()
44
			defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
45
		}
46
		ctx = templ.InitializeContext(ctx)
47
		templ_7745c5c3_Var2 := templ.GetChildren(ctx)
48
		if templ_7745c5c3_Var2 == nil {
49
			templ_7745c5c3_Var2 = templ.NopComponent
50
		}
51
		ctx = templ.ClearChildren(ctx)
52
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div id=\"#username-error\" hx-swap-oob=\"true\" class=\"text-red-500 text-xs mt-1\">Username - ")
53
		if templ_7745c5c3_Err != nil {
54
			return templ_7745c5c3_Err
55
		}
56
		var templ_7745c5c3_Var3 string
57
		templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(err)
58
		if templ_7745c5c3_Err != nil {
59
			return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/v2/views/components/register.component.templ`, Line: 66, Col: 96}
60
		}
61
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
62
		if templ_7745c5c3_Err != nil {
63
			return templ_7745c5c3_Err
64
		}
65
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div>")
66
		if templ_7745c5c3_Err != nil {
67
			return templ_7745c5c3_Err
68
		}
69
		if !templ_7745c5c3_IsBuffer {
70
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
71
		}
72
		return templ_7745c5c3_Err
73
	})
74
}
75
76
func PasswordEntropy(value float64, err error) templ.Component {
77
	return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
78
		templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
79
		if !templ_7745c5c3_IsBuffer {
80
			templ_7745c5c3_Buffer = templ.GetBuffer()
81
			defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
82
		}
83
		ctx = templ.InitializeContext(ctx)
84
		templ_7745c5c3_Var4 := templ.GetChildren(ctx)
85
		if templ_7745c5c3_Var4 == nil {
86
			templ_7745c5c3_Var4 = templ.NopComponent
87
		}
88
		ctx = templ.ClearChildren(ctx)
89
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<div id=\"password-entropy\" hx-swap-oob=\"true\"><span class=\"label-text\">Strength of password</span> ")
90
		if templ_7745c5c3_Err != nil {
91
			return templ_7745c5c3_Err
92
		}
93
		var templ_7745c5c3_Var5 = []any{"progress w-full transition-all ease-out duration-[6s]", templ.KV("progress-error", value < 72),
94
			templ.KV("progress-warning", value >= 72 && value < 80), templ.KV("progress-success", value >= 80)}
95
		templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var5...)
96
		if templ_7745c5c3_Err != nil {
97
			return templ_7745c5c3_Err
98
		}
99
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<progress id=\"pb\" class=\"")
100
		if templ_7745c5c3_Err != nil {
101
			return templ_7745c5c3_Err
102
		}
103
		var templ_7745c5c3_Var6 string
104
		templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(templ.CSSClasses(templ_7745c5c3_Var5).String())
105
		if templ_7745c5c3_Err != nil {
106
			return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/v2/views/components/register.component.templ`, Line: 1, Col: 0}
107
		}
108
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6))
109
		if templ_7745c5c3_Err != nil {
110
			return templ_7745c5c3_Err
111
		}
112
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" value=\"")
113
		if templ_7745c5c3_Err != nil {
114
			return templ_7745c5c3_Err
115
		}
116
		var templ_7745c5c3_Var7 string
117
		templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(int(value)))
118
		if templ_7745c5c3_Err != nil {
119
			return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/v2/views/components/register.component.templ`, Line: 76, Col: 35}
120
		}
121
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7))
122
		if templ_7745c5c3_Err != nil {
123
			return templ_7745c5c3_Err
124
		}
125
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\" max=\"100\"></progress> ")
126
		if templ_7745c5c3_Err != nil {
127
			return templ_7745c5c3_Err
128
		}
129
		if err != nil {
130
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<span class=\"text-xs text-gray-500\">")
131
			if templ_7745c5c3_Err != nil {
132
				return templ_7745c5c3_Err
133
			}
134
			var templ_7745c5c3_Var8 string
135
			templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(err.Error())
136
			if templ_7745c5c3_Err != nil {
137
				return templ.Error{Err: templ_7745c5c3_Err, FileName: `app/v2/views/components/register.component.templ`, Line: 81, Col: 17}
138
			}
139
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8))
140
			if templ_7745c5c3_Err != nil {
141
				return templ_7745c5c3_Err
142
			}
143
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</span>")
144
			if templ_7745c5c3_Err != nil {
145
				return templ_7745c5c3_Err
146
			}
147
		}
148
		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</div>")
149
		if templ_7745c5c3_Err != nil {
150
			return templ_7745c5c3_Err
151
		}
152
		if !templ_7745c5c3_IsBuffer {
153
			_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
154
		}
155
		return templ_7745c5c3_Err
156
	})
157
}
158