File utils.js
has 590 lines of code (exceeds 250 allowed). Consider refactoring. Open
var events = require('events');
var request = require('request');
var zlib = require('zlib');
var iconv = require('iconv-lite');
var async = require('async');
Consider simplifying this complex logical expression. Open
if (meta) {
var isJetpack = meta.twitter && meta.twitter.card === 'jetpack';
var isWordpress = meta.generator && /wordpress/i.test(meta.generator);
var isShopify = false;
Function getWhitelistLogData
has a Cognitive Complexity of 31 (exceeds 5 allowed). Consider refactoring. Open
function getWhitelistLogData(meta, oembed) {
var r = {};
if (meta) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function filterLinks
has a Cognitive Complexity of 29 (exceeds 5 allowed). Consider refactoring. Open
exports.filterLinks = function(data, options) {
var links = data.links;
for(var i = 0; i < links.length;) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getImageMetadata
has 94 lines of code (exceeds 25 allowed). Consider refactoring. Open
exports.getImageMetadata = function(uri, options, callback){
if (typeof options === 'function') {
callback = options;
options = {};
Function getWhitelistLogData
has 61 lines of code (exceeds 25 allowed). Consider refactoring. Open
function getWhitelistLogData(meta, oembed) {
var r = {};
if (meta) {
Function getContentType
has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring. Open
exports.getContentType = function(uriForCache, uriOriginal, options, cb) {
cache.withCache("content-type:" + uriForCache, function(cb) {
var timeout, requestInstance, totalTime;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getUrl
has 50 lines of code (exceeds 25 allowed). Consider refactoring. Open
var getUrl = exports.getUrl = function(url, options) {
var req = new events.EventEmitter();
var options = options || {};
Function sendLogToWhitelist
has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring. Open
exports.sendLogToWhitelist = function(uri, meta, oembed, whitelistRecord) {
if (!CONFIG.WHITELIST_LOG_URL) {
return
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function getContentType
has 47 lines of code (exceeds 25 allowed). Consider refactoring. Open
exports.getContentType = function(uriForCache, uriOriginal, options, cb) {
cache.withCache("content-type:" + uriForCache, function(cb) {
var timeout, requestInstance, totalTime;
Function isYoutube
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
function isYoutube(meta) {
var video;
if (meta.og && (video = meta.og.video)) {
if (!(video instanceof Array)) {
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function filterLinks
has 39 lines of code (exceeds 25 allowed). Consider refactoring. Open
exports.filterLinks = function(data, options) {
var links = data.links;
for(var i = 0; i < links.length;) {
Function generateLinksHtml
has 35 lines of code (exceeds 25 allowed). Consider refactoring. Open
exports.generateLinksHtml = function(data, options) {
// Links may be grouped.
var links = data.links;
Function getUriStatus
has 34 lines of code (exceeds 25 allowed). Consider refactoring. Open
exports.getUriStatus = function(uri, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
Function sendLogToWhitelist
has 34 lines of code (exceeds 25 allowed). Consider refactoring. Open
exports.sendLogToWhitelist = function(uri, meta, oembed, whitelistRecord) {
if (!CONFIG.WHITELIST_LOG_URL) {
return
}
Function getHead
has 33 lines of code (exceeds 25 allowed). Consider refactoring. Open
var getHead = function(url, options) {
var req = new events.EventEmitter();
var options = options || {};
Function prepareRequestOptions
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
var prepareRequestOptions = exports.prepareRequestOptions = function(request_options) {
if (CONFIG.PROXY) {
var url = request_options.uri || request_options.url;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function finish
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
function finish(error, data) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
Function generateLinksHtml
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
exports.generateLinksHtml = function(data, options) {
// Links may be grouped.
var links = data.links;
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function unifyDate
has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring. Open
exports.unifyDate = function(date) {
if (typeof date === "string" && date.match(/^\d+$/)) {
date = parseInt(date);
}
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Avoid deeply nested control flow statements. Open
if (oembedLink.href.indexOf(SHOPIFY_OEMBED_URLS[i]) > -1) {
isShopify = true;
}
Function iterateLinks
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
function iterateLinks(links, func) {
if (links instanceof Array) {
return links.forEach(func);
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Function lowerCaseKeys
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
var lowerCaseKeys = exports.lowerCaseKeys = function(obj) {
for (var k in obj) {
var lowerCaseKey = k.toLowerCase();
if (lowerCaseKey != k) {
obj[lowerCaseKey] = obj[k];
- Read upRead up
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Similar blocks of code found in 2 locations. Consider refactoring. Open
(meta.twitter && meta.twitter.card === 'photo')
&&
(meta.og && meta.og.type !== "article")
&&
!isJetpack
- Read upRead up
Duplicated Code
Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).
Tuning
This issue has a mass of 46.
We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.
The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.
If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.
See codeclimate-duplication
's documentation for more information about tuning the mass threshold in your .codeclimate.yml
.
Refactorings
- Extract Method
- Extract Class
- Form Template Method
- Introduce Null Object
- Pull Up Method
- Pull Up Field
- Substitute Algorithm
Further Reading
- Don't Repeat Yourself on the C2 Wiki
- Duplicated Code on SourceMaking
- Refactoring: Improving the Design of Existing Code by Martin Fowler. Duplicated Code, p76