Passed
Push — master ( b15b71...ab24e8 )
by Paul
04:46
created

+/scripts/public/recaptcha.js   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 96

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 96
rs 8.3859

9 Functions

Rating   Name   Duplication   Size   Complexity  
B Recaptcha.search_ 0 14 5
A Recaptcha.onKeyup_ 0 5 2
A Recaptcha.getOverlay_ 0 12 1
A recaptcha.js ➔ Recaptcha 0 6 1
A Recaptcha.addListeners_ 0 6 2
A Recaptcha.getId_ 0 8 1
A Recaptcha.execute_ 0 9 2
A Recaptcha.removeListeners_ 0 4 1
A Recaptcha.reset_ 0 6 2

How to fix   Long Method   

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:

1
/** global: GLSR, grecaptcha */
2
;(function() {
3
4
	'use strict';
5
6
	var Recaptcha = function( form ) { // Form object
7
		this.Form = form;
8
		this.addListeners = this.addListeners_;
9
		this.execute = this.execute_;
10
		this.reset = this.reset_;
11
	};
12
13
	Recaptcha.prototype = {
14
15
		/** @return void */
16
		addListeners_: function() {
17
			var overlayEl = this.getOverlay_();
18
			if( overlayEl === -1 )return;
19
			overlayEl.addEventListener( 'click', this.Form.enableButton );
20
			window.addEventListener( 'keyup', this.onKeyup_.bind( this, overlayEl ));
21
		},
22
23
		/** @return void */
24
		execute_: function() {
25
			var recaptchaId = this.getId_();
26
			if( recaptchaId !== -1 ) {
27
				grecaptcha.execute( recaptchaId );
28
				return;
29
			}
30
			// recaptcha ID not found so pass through an error
31
			this.Form.submitForm_( false );
32
		},
33
34
		/** @return string|int (-1) */
35
		getId_: function() {
36
			return this.search_( function( value, id ) {
37
				if( Object.prototype.toString.call( value ) !== '[object HTMLDivElement]' )return;
38
				if( value.closest( 'form' ) === this.Form.form ) {
1 ignored issue
show
Complexity Best Practice introduced by
There is no return statement if value.closest("form") === this.Form.form is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
39
					return id;
40
				}
41
			});
42
		},
43
44
		/** @return HTMLDivElement|int (-1) */
45
		getOverlay_: function() {
46
			return this.search_( function( value ) {
47
				if( Object.prototype.toString.call( value ) !== '[object Object]' )return;
48
				for( var obj in value) {
49
					if( !value.hasOwnProperty( obj ) || Object.prototype.toString.call( value[obj] ) !== '[object HTMLDivElement]' )continue;
50
					if( value[obj].className === '' ) {
51
						return value[obj].firstChild;
52
					}
53
				}
54
				return false;
55
			});
56
		},
57
58
		/** @return void */
59
		onKeyup_: function( ev ) { // KeyboardEvent
60
			if( ev.keyCode !== 27 )return;
61
			this.Form.enableButton();
62
			this.removeListeners_( ev.target );
63
		},
64
65
		/** @return void */
66
		removeListeners_: function( overlayEl ) { // HTMLDivElement
67
			overlayEl.removeEventListener( 'click', this.Form.enableButton );
68
			window.removeEventListener( 'keyup', this.onKeyup_ );
69
		},
70
71
		/** @return void */
72
		reset_: function() {
73
			var recaptchaId = this.getId_();
74
			if( recaptchaId !== -1 ) {
75
				grecaptcha.reset( recaptchaId );
76
			}
77
		},
78
79
		/** @return mixed|int (-1) */
80
		search_: function( callback ) { // function
81
			var result = -1;
82
			if( window.hasOwnProperty( '___grecaptcha_cfg' )) {
83
				var clients = window.___grecaptcha_cfg.clients;
84
				var i, key;
85
				for( i in clients ) {
86
					for( key in clients[i] ) {
87
						if( !( result = callback( clients[i][key], i ).bind( this )))continue;
88
						return result;
89
					}
90
				}
91
			}
92
			return result;
93
		},
94
	};
95
96
	GLSR.Recaptcha = Recaptcha;
97
})();
98