| Total Complexity | 98 |
| Complexity/F | 4.26 |
| Lines of Code | 375 |
| Function Count | 23 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 0 |
Complex classes like js/default_tests.js 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 | /** |
||
| 8 | const default_tests = [ |
||
|
1 ignored issue
–
show
|
|||
| 9 | { |
||
| 10 | name: 'error_pages', |
||
| 11 | title: 'ERROR PAGES', |
||
| 12 | headers: ['URL'], |
||
| 13 | type: 'success' |
||
| 14 | }, |
||
| 15 | |||
| 16 | { |
||
| 17 | name : 'h1_info', |
||
| 18 | title: 'H1 INFO', |
||
| 19 | headers: ['URL', 'Count', 'Text', 'Status'], |
||
| 20 | callback: function(cont, url, html){ |
||
| 21 | var h1 = html.find( 'h1' ), |
||
| 22 | link = crawler_painter.create_link(url, url), |
||
|
1 ignored issue
–
show
|
|||
| 23 | joined = [], status = undefined; |
||
| 24 | |||
| 25 | h1.each(function(){ joined.push(this.innerHTML); }); |
||
| 26 | |||
| 27 | if(h1.length != 1) |
||
| 28 | status = crawler_painter.create_status('error', (h1.length < 1) ? 'Missing H1' : 'Multiple H1 tags'); |
||
|
2 ignored issues
–
show
|
|||
| 29 | else status = crawler_painter.create_status('success', 'OK!'); |
||
| 30 | |||
| 31 | crawler_painter.add_row(this.name, [link, h1.length, joined.join(', '), status]); |
||
| 32 | } |
||
| 33 | }, |
||
| 34 | |||
| 35 | { |
||
| 36 | name : 'h2_info', |
||
| 37 | title: 'H2 INFO', |
||
| 38 | headers: ['URL', 'Count', 'Text', 'Status'], |
||
| 39 | callback: function(cont, url, html){ |
||
| 40 | var h2 = html.find( 'h2' ), |
||
| 41 | link = crawler_painter.create_link(url, url), |
||
|
1 ignored issue
–
show
|
|||
| 42 | joined = [], status = undefined; |
||
| 43 | |||
| 44 | h2.each(function(){ joined.push(this.innerHTML); }); |
||
| 45 | |||
| 46 | if(h2.length < 1) status = crawler_painter.create_status('warning', 'Missing H2'); |
||
|
2 ignored issues
–
show
|
|||
| 47 | else status = crawler_painter.create_status('success', 'OK!'); |
||
| 48 | |||
| 49 | crawler_painter.add_row(this.name, [link, h2.length, joined.join(', '), status]); |
||
| 50 | } |
||
| 51 | }, |
||
| 52 | |||
| 53 | { |
||
| 54 | name : 'word_count', |
||
| 55 | title: 'WORD COUNT', |
||
| 56 | headers: ['URL', 'Word Count', 'Article Word Count'], |
||
| 57 | callback: function(cont, url, html, headers, field_data, phrases){ |
||
| 58 | var link = crawler_painter.create_link(url, url), |
||
|
1 ignored issue
–
show
|
|||
| 59 | word_count = crawler.get_word_count(phrases), |
||
|
1 ignored issue
–
show
|
|||
| 60 | art_count = crawler.get_word_count(field_data[3]); |
||
| 61 | |||
| 62 | crawler_painter.add_row(this.name, [link, word_count, art_count]); |
||
| 63 | } |
||
| 64 | }, |
||
| 65 | |||
| 66 | { |
||
| 67 | name : 'int_link_info', |
||
| 68 | title: 'INTERNAL LINK INFO', |
||
| 69 | headers: ['URL', 'Article Links', 'Article Link Count', 'Article Density', |
||
| 70 | 'Total Link Count', 'Total Density', 'Status'], |
||
| 71 | type: 'info', |
||
| 72 | callback: function(cont, url, html, headers, field_data, phrases){ |
||
| 73 | var link = crawler_painter.create_link(url, url), |
||
|
1 ignored issue
–
show
|
|||
| 74 | art_links = [], links = []; |
||
| 75 | |||
| 76 | // Article links |
||
| 77 | for( var field in field_data[2] ) { |
||
|
1 ignored issue
–
show
|
|||
| 78 | $.each($(field_data[2][field]).find('a'), function () { |
||
| 79 | var href = $(this).attr('href'); |
||
| 80 | if(href && !crawler.is_external(href) && !crawler.is_anchor(href, url)) art_links.push(href); |
||
|
2 ignored issues
–
show
|
|||
| 81 | }); |
||
| 82 | } |
||
| 83 | |||
| 84 | // Full page links |
||
| 85 | $.each(html.find('a'), function () { |
||
| 86 | var href = $(this).attr('href'); |
||
| 87 | if(href && !crawler.is_external(href) && !crawler.is_anchor(href, url)) links.push(href); |
||
|
2 ignored issues
–
show
|
|||
| 88 | }); |
||
| 89 | |||
| 90 | var art_word_count = crawler.get_word_count(field_data[3]), |
||
| 91 | art_density = (art_links.length > 0) ? art_word_count / art_links.length : false, |
||
| 92 | art_dens_text = (art_density != false) ? art_density.toFixed(2) +' words/link' : 'No internal links', |
||
| 93 | word_count = crawler.get_word_count(phrases), |
||
| 94 | density = (links.length > 0) ? word_count / links.length : false, |
||
| 95 | dens_text = (density != false) ? density.toFixed(2) +' words/link' : 'No internal links', |
||
| 96 | status = undefined; |
||
| 97 | |||
| 98 | if( ( art_density !== false && art_density < 100 ) ) |
||
| 99 | status = crawler_painter.create_status('warning', 'This page might be considered spammy'); |
||
|
2 ignored issues
–
show
|
|||
| 100 | |||
| 101 | if(links.length > 0) |
||
| 102 | crawler_painter.add_row( this.name, [ |
||
|
1 ignored issue
–
show
|
|||
| 103 | link, art_links.join('<br />'), art_links.length, art_dens_text, links.length, dens_text, status |
||
| 104 | ]); |
||
| 105 | } |
||
| 106 | }, |
||
| 107 | |||
| 108 | { |
||
| 109 | name : 'ext_link_info', |
||
| 110 | title: 'EXTERNAL LINK INFO', |
||
| 111 | headers: ['URL', 'External Link Count', 'External Links'], |
||
| 112 | type: 'success', |
||
| 113 | callback: function(cont, url, html, headers, field_data){ |
||
| 114 | var link = crawler_painter.create_link(url, url), |
||
|
1 ignored issue
–
show
|
|||
| 115 | links = []; |
||
| 116 | |||
| 117 | for( var field in field_data[2] ) { |
||
|
1 ignored issue
–
show
|
|||
| 118 | $.each($(field_data[2][field]).find('a'), function () { |
||
| 119 | var $this = $(this), |
||
| 120 | href = $this.attr('href'); |
||
| 121 | if(href && crawler.is_external(href)){ |
||
|
1 ignored issue
–
show
|
|||
| 122 | var type = ( !$this.attr('rel') || $this.attr('rel').toLowerCase().indexOf('nofollow') < 0 ) |
||
| 123 | ? 'warning' : 'info'; |
||
| 124 | links.push( |
||
| 125 | $('<div class="clearfix"></div>').append([ |
||
| 126 | crawler_painter.create_status(type, href), |
||
|
1 ignored issue
–
show
|
|||
| 127 | '<p> </p>' |
||
| 128 | ]) |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | }); |
||
| 132 | } |
||
| 133 | |||
| 134 | if(links.length > 0) crawler_painter.add_row(this.name, [link, links.length, links]); |
||
|
1 ignored issue
–
show
|
|||
| 135 | } |
||
| 136 | }, |
||
| 137 | |||
| 138 | { |
||
| 139 | name : 'img_info', |
||
| 140 | title: 'IMAGE INFO', |
||
| 141 | headers: ['URL', 'Count', 'Missing Alt Tag', 'Missing Title Tag', 'Fields Missing Images', 'Status'], |
||
| 142 | type: 'success', |
||
| 143 | callback: function(cont, url, html, headers, field_data) { |
||
| 144 | var link = crawler_painter.create_link(url, url), |
||
|
1 ignored issue
–
show
|
|||
| 145 | imgs = html.find('img'), |
||
| 146 | alt = 0, title = 0, fields = [], status = ''; |
||
| 147 | |||
| 148 | // Check alt and title tags |
||
| 149 | $.each(imgs, function () { |
||
| 150 | var $this = $(this); |
||
| 151 | if (!$this.attr('alt') || $this.attr('alt').length < 1) alt += 1; |
||
|
1 ignored issue
–
show
|
|||
| 152 | if (!$this.attr('title') || $this.attr('title').length < 1) title += 1; |
||
|
1 ignored issue
–
show
|
|||
| 153 | }); |
||
| 154 | |||
| 155 | // Check the fields |
||
| 156 | for (var f in field_data[2]) if ($(field_data[2][f]).find('img').length < 1) fields.push(field_data[1][f]); |
||
|
2 ignored issues
–
show
|
|||
| 157 | |||
| 158 | // Construct Result |
||
| 159 | if (alt > 0) |
||
| 160 | status = crawler_painter.create_status('error', |
||
|
2 ignored issues
–
show
|
|||
| 161 | (alt > 1) ? alt + ' images missing alt tag' : '1 image missing alt tag'); |
||
| 162 | else if(fields.length > 0) |
||
| 163 | status = crawler_painter.create_status('warning', |
||
|
1 ignored issue
–
show
|
|||
| 164 | (fields.length > 1) ? fields.join(' and ') + ' are missing images' : fields[0] + ' is missing images'); |
||
| 165 | else if(title > 0) |
||
| 166 | status = crawler_painter.create_status('info', |
||
|
1 ignored issue
–
show
|
|||
| 167 | (title > 1) ? title + ' images missing title tag' : '1 image is missing title tag'); |
||
| 168 | else |
||
| 169 | status = crawler_painter.create_status('success', 'OK!'); |
||
| 170 | |||
| 171 | crawler_painter.add_row(this.name, [link, imgs.length, alt, title, fields.join(', '), status]); |
||
| 172 | } |
||
| 173 | }, |
||
| 174 | |||
| 175 | { |
||
| 176 | name: 'title_info', |
||
| 177 | title: 'META TITLE', |
||
| 178 | headers: ['URL', 'Meta Title', 'Length', 'Status'], |
||
| 179 | callback: function(cont, url, html){ |
||
|
1 ignored issue
–
show
|
|||
| 180 | var title = html.filter( 'title' ), |
||
| 181 | link = crawler_painter.create_link(url, url), |
||
|
1 ignored issue
–
show
|
|||
| 182 | text = '', len = 0, status = undefined; |
||
| 183 | |||
| 184 | if( title.length > 1 ){ |
||
| 185 | text = 'Multiple Titles'; |
||
| 186 | len = 'N/A'; |
||
| 187 | status = crawler_painter.create_status('error', 'Multiple title tags'); |
||
| 188 | }else if( title.length < 1 ){ |
||
| 189 | status = crawler_painter.create_status('error', 'Missing title tag'); |
||
| 190 | }else{ |
||
| 191 | text = title.html(); |
||
| 192 | len = text.length; |
||
| 193 | if(len < 40) status = crawler_painter.create_status('warning', 'Meta title is too short'); |
||
|
1 ignored issue
–
show
|
|||
| 194 | else if(len > 56) status = crawler_painter.create_status('warning', 'Meta title is too long'); |
||
|
1 ignored issue
–
show
|
|||
| 195 | else status = crawler_painter.create_status('success', 'OK!'); |
||
| 196 | } |
||
| 197 | |||
| 198 | crawler_painter.add_row(this.name, [link, text, len, status]); |
||
| 199 | if(!crawler.hasOwnProperty('meta_titles')) crawler.meta_titles = {}; |
||
|
2 ignored issues
–
show
|
|||
| 200 | if(!crawler.meta_titles.hasOwnProperty(text)) crawler.meta_titles[text] = [url]; |
||
|
1 ignored issue
–
show
|
|||
| 201 | else crawler.meta_titles[text].push(url); |
||
| 202 | } |
||
| 203 | }, |
||
| 204 | |||
| 205 | { |
||
| 206 | name: 'description_info', |
||
| 207 | title: 'META DESCRIPTION', |
||
| 208 | headers: ['URL', 'Meta Description', 'Length', 'Status'], |
||
| 209 | callback: function(cont, url, html){ |
||
|
1 ignored issue
–
show
|
|||
| 210 | var desc = html.filter( 'meta[name=description]' ), |
||
| 211 | link = crawler_painter.create_link(url, url), |
||
|
1 ignored issue
–
show
|
|||
| 212 | text = '', len = 0, status = undefined; |
||
| 213 | |||
| 214 | if( desc.length > 1 ){ |
||
| 215 | text = 'Multiple Meta Descriptions'; |
||
| 216 | len = 'N/A'; |
||
| 217 | status = crawler_painter.create_status('error', 'Multiple meta description tags'); |
||
| 218 | }else if( desc.length < 1 ){ |
||
| 219 | status = crawler_painter.create_status('error', 'Missing meta description tag'); |
||
| 220 | }else{ |
||
| 221 | text = desc.attr('content'); |
||
| 222 | len = text.length; |
||
| 223 | if(len < 70) status = crawler_painter.create_status('warning', 'Meta description is too short'); |
||
|
1 ignored issue
–
show
|
|||
| 224 | else if(len > 156) status = crawler_painter.create_status('warning', 'Meta description is too long'); |
||
|
1 ignored issue
–
show
|
|||
| 225 | else status = crawler_painter.create_status('success', 'OK!'); |
||
| 226 | |||
| 227 | if(!crawler.hasOwnProperty('descriptions')) crawler.descriptions = {}; |
||
|
2 ignored issues
–
show
|
|||
| 228 | if(!crawler.descriptions.hasOwnProperty(text)) crawler.descriptions[text] = [url]; |
||
|
1 ignored issue
–
show
|
|||
| 229 | else crawler.descriptions[text].push(url); |
||
| 230 | } |
||
| 231 | |||
| 232 | crawler_painter.add_row(this.name, [link, text, len, status]); |
||
| 233 | } |
||
| 234 | }, |
||
| 235 | |||
| 236 | { |
||
| 237 | name: 'canonical_info', |
||
| 238 | title: 'PAGES MISSING CANONICAL', |
||
| 239 | headers: ['URL'], |
||
| 240 | type: 'success', |
||
| 241 | callback: function(cont, url, html){ |
||
| 242 | var tags = html.filter( 'link' ), |
||
| 243 | canonical = undefined; |
||
| 244 | |||
| 245 | for( var i = 0; i < tags.length; i++ ) { |
||
| 246 | var rel = $(tags[i]).attr('rel'); |
||
| 247 | if( rel && rel.toLowerCase() === 'canonical' ) { |
||
| 248 | canonical = $(tags[i]).attr('rel'); |
||
| 249 | break; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | if(canonical === undefined || canonical.length < 1) { |
||
| 254 | crawler_painter.add_row(this.name, [crawler_painter.create_link(url, url)]); |
||
|
1 ignored issue
–
show
|
|||
| 255 | crawler_painter.set_type(this.name, 'error'); |
||
| 256 | } |
||
| 257 | |||
| 258 | canonical = url; // What Google will do |
||
| 259 | if(!crawler.hasOwnProperty('canonicals')) crawler.canonicals = {}; |
||
|
2 ignored issues
–
show
|
|||
| 260 | if(!crawler.canonicals.hasOwnProperty(canonical)) crawler.canonicals[canonical] = [url]; |
||
|
1 ignored issue
–
show
|
|||
| 261 | else crawler.canonicals[canonical].push(url); |
||
| 262 | } |
||
| 263 | }, |
||
| 264 | |||
| 265 | { |
||
| 266 | name: 'no-index_pages', |
||
| 267 | title: 'NO-INDEX PAGES', |
||
| 268 | headers: ['URL'], |
||
| 269 | type: 'success', |
||
| 270 | callback: function(cont, url, html){ |
||
| 271 | var tags = html.filter( 'meta' ); |
||
| 272 | for( var i = 0; i < tags.length; i++ ) |
||
| 273 | if( $(tags[i]).attr( 'name' ) && $(tags[i]).attr( 'name' ).toLowerCase() === 'robots' && |
||
|
1 ignored issue
–
show
|
|||
| 274 | $(tags[i]).attr('content').toLowerCase().indexOf( 'noindex' ) > -1 ) { |
||
| 275 | crawler_painter.add_row(this.name, [crawler_painter.create_link(url, url)]); |
||
|
1 ignored issue
–
show
|
|||
| 276 | crawler_painter.set_type(this.name, 'warning'); |
||
| 277 | return; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | }, |
||
| 281 | |||
| 282 | { |
||
| 283 | name: 'urls_test', |
||
| 284 | title: 'URL STRUCTURE', |
||
| 285 | headers: ['URL', 'Status'], |
||
| 286 | type: 'success', |
||
| 287 | callback: function(cont, url){ |
||
| 288 | var link = crawler_painter.create_link(url, url), |
||
|
1 ignored issue
–
show
|
|||
| 289 | type = 'warning', |
||
| 290 | msg = 'OK!'; |
||
| 291 | |||
| 292 | if( url.length > 115 ) msg = 'URL is too long'; |
||
|
1 ignored issue
–
show
|
|||
| 293 | else if( url.toLowerCase() != url ) msg = 'URL is not in lower case'; |
||
|
1 ignored issue
–
show
|
|||
| 294 | else if( url.replace('_','') !== url ) msg = 'URL contains under scores'; |
||
|
1 ignored issue
–
show
|
|||
| 295 | else return true; |
||
| 296 | |||
| 297 | crawler_painter.add_row(this.name, [link, crawler_painter.create_status(type, msg)]); |
||
| 298 | } |
||
| 299 | }, |
||
| 300 | |||
| 301 | { |
||
| 302 | name: 'duplicate_meta_tags', |
||
| 303 | title: 'DUPLICATE META TAGS', |
||
| 304 | headers: ['URL', 'Status'], |
||
| 305 | type: 'success', |
||
| 306 | callback: function(){ |
||
| 307 | var canonicals = crawler.canonicals, |
||
|
1 ignored issue
–
show
|
|||
| 308 | tests = { |
||
| 309 | 'meta_titles' : 'Urls have same meta title but different canonicals', |
||
| 310 | 'descriptions' : 'Urls have same meta description but different canonicals' |
||
| 311 | }; |
||
| 312 | |||
| 313 | // Reset table |
||
| 314 | crawler_painter.reset_table(this.name, 'success'); |
||
|
1 ignored issue
–
show
|
|||
| 315 | |||
| 316 | for(var test in tests){ |
||
|
1 ignored issue
–
show
|
|||
| 317 | for(var x in crawler[test]){ |
||
|
1 ignored issue
–
show
|
|||
| 318 | var urls = crawler[test][x]; |
||
| 319 | if( urls < 2 ) continue; |
||
|
1 ignored issue
–
show
|
|||
| 320 | var canonical = getKeyFromObject(canonicals, urls[0]); |
||
| 321 | for( var i in urls ) |
||
|
1 ignored issue
–
show
|
|||
| 322 | if( canonical != getKeyFromObject(canonicals, urls[i]) ) { |
||
|
1 ignored issue
–
show
|
|||
| 323 | crawler_painter.add_row( |
||
| 324 | this.name, |
||
| 325 | [urls.join(', '), crawler_painter.create_status('error', tests[test])] |
||
| 326 | ); |
||
| 327 | break; |
||
| 328 | } |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | function getKeyFromObject(object, search){ |
||
| 333 | for( var key in object ) if( object[key].indexOf(search) >= 0 ) return key; |
||
|
2 ignored issues
–
show
|
|||
| 334 | } |
||
| 335 | } |
||
| 336 | }, |
||
| 337 | |||
| 338 | { |
||
| 339 | name: 'href_langs', |
||
| 340 | title: 'LANG TAGS', |
||
| 341 | headers: ['URL', 'Tags'], |
||
| 342 | type: 'info', |
||
| 343 | callback: function(cont, url, html){ |
||
| 344 | var link = crawler_painter.create_link(url, url), |
||
|
1 ignored issue
–
show
|
|||
| 345 | tags = []; |
||
| 346 | |||
| 347 | $.each( html.filter( 'link' ), function(){ |
||
| 348 | if( $(this).attr( 'hreflang' ) ) |
||
| 349 | tags.push( $('<p>').text( $(this).clone().wrap('<p>').parent().html() ).html() ); |
||
|
1 ignored issue
–
show
|
|||
| 350 | }); |
||
| 351 | |||
| 352 | if( tags.length > 0 ) crawler_painter.add_row(this.name, [link, tags.join('<br />')] ); |
||
|
2 ignored issues
–
show
|
|||
| 353 | } |
||
| 354 | }, |
||
| 355 | |||
| 356 | { |
||
| 357 | name: 'orphan_pages', |
||
| 358 | title: 'ORPHAN PAGES', |
||
| 359 | headers: ['URL'], |
||
| 360 | callback: function(){ |
||
| 361 | if(crawler.que.length > 0) return true; |
||
|
2 ignored issues
–
show
|
|||
| 362 | crawler_painter.reset_table(this.name, 'success'); |
||
|
1 ignored issue
–
show
|
|||
| 363 | |||
| 364 | pages_loop: |
||
| 365 | for( var i in crawler.tested ){ |
||
|
1 ignored issue
–
show
|
|||
| 366 | var url = crawler.tested[i]; |
||
| 367 | if( crawler.linked_from.hasOwnProperty(url) ) { |
||
| 368 | for (var x in crawler.linked_from[url]) |
||
|
1 ignored issue
–
show
|
|||
| 369 | if (crawler.linked_from[url][x] != url) continue pages_loop; |
||
|
1 ignored issue
–
show
|
|||
| 370 | } |
||
| 371 | |||
| 372 | crawler.add_row(this.name, [crawler_painter.create_link(crawler.tested[i], crawler.tested[i])]); |
||
| 373 | crawler_painter.set_type(this.name, 'error'); |
||
| 374 | } |
||
| 375 | } |
||
| 376 | } |
||
| 377 | ]; |
||
| 378 | |||
| 379 | crawler.on('CRAWL_LOAD_FAILED', function(url){ |
||
| 380 | crawler_painter.add_row('error_pages', [url]); |
||
|
1 ignored issue
–
show
|
|||
| 381 | crawler_painter.set_type('error_pages', 'error'); |
||
| 382 | }); |
||
| 383 |