bencevans/node-sonos

View on GitHub
lib/helpers.js

Summary

Maintainability
F
3 days
Test Coverage

Function GenerateMetadata has 113 lines of code (exceeds 25 allowed). Consider refactoring.
Open

Helpers.GenerateMetadata = function (uri, title = '', region = '3079') {
  var parts = uri.split(':')
  if (!((parts.length === 2 && (parts[0] === 'radio' || parts[0] === 'x-sonosapi-stream' || parts[0] === 'x-rincon-cpcontainer')) || (parts.length >= 3 && parts[0] === 'spotify'))) {
    debug('Returning string because it isn\'t recognized')
    return Helpers.GenerateLocalMetadata(uri)
Severity: Major
Found in lib/helpers.js - About 4 hrs to fix

    Function GenerateMetadata has a Cognitive Complexity of 20 (exceeds 5 allowed). Consider refactoring.
    Open

    Helpers.GenerateMetadata = function (uri, title = '', region = '3079') {
      var parts = uri.split(':')
      if (!((parts.length === 2 && (parts[0] === 'radio' || parts[0] === 'x-sonosapi-stream' || parts[0] === 'x-rincon-cpcontainer')) || (parts.length >= 3 && parts[0] === 'spotify'))) {
        debug('Returning string because it isn\'t recognized')
        return Helpers.GenerateLocalMetadata(uri)
    Severity: Minor
    Found in lib/helpers.js - About 2 hrs to fix

    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

    File helpers.js has 252 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    /**
     * Sonos library to control (almost) everything from your sonos devices
     * @module sonos
     * @requires 'debug'
     * @requires 'xml2js
    Severity: Minor
    Found in lib/helpers.js - About 2 hrs to fix

      Function GenerateLocalMetadata has 30 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

      Helpers.GenerateLocalMetadata = function (uri, artUri = '') {
        var title = ''
        var match = /.*\/(.*)$/g.exec(uri.replace(/\.[a-zA-Z0-9]{3}$/, ''))
        if (match) {
          title = match[1]
      Severity: Minor
      Found in lib/helpers.js - About 1 hr to fix

        Function GenerateCustomMetadata has 8 arguments (exceeds 4 allowed). Consider refactoring.
        Open

        Helpers.GenerateCustomMetadata = function (streamUrl, itemId, duration = '00:00:00', title, artist, album, coverUrl, parentId) {
        Severity: Major
        Found in lib/helpers.js - About 1 hr to fix

          Function ParseDIDLItem has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
          Open

          Helpers.ParseDIDLItem = function (item, host, port, trackUri) {
            let albumArtURI = item['upnp:albumArtURI'] || null
            if (albumArtURI && Array.isArray(albumArtURI)) {
              albumArtURI = albumArtURI.length > 0 ? albumArtURI[0] : null
            }
          Severity: Minor
          Found in lib/helpers.js - About 45 mins to fix

          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

          Consider simplifying this complex logical expression.
          Open

            if (!((parts.length === 2 && (parts[0] === 'radio' || parts[0] === 'x-sonosapi-stream' || parts[0] === 'x-rincon-cpcontainer')) || (parts.length >= 3 && parts[0] === 'spotify'))) {
              debug('Returning string because it isn\'t recognized')
              return Helpers.GenerateLocalMetadata(uri)
            }
          Severity: Major
          Found in lib/helpers.js - About 40 mins to fix

            Avoid too many return statements within this function.
            Open

                return {
                  uri: 'x-rincon-cpcontainer:000e206c' + spotifyUri,
                  metadata: meta.replace('##SPOTIFYURI##', '000e206c' + spotifyUri)
                    .replace('##RESOURCETITLE##', '')
                    .replace('##SPOTIFYTYPE##', 'object.container.playlistContainer')
            Severity: Major
            Found in lib/helpers.js - About 30 mins to fix

              Avoid too many return statements within this function.
              Open

                  return {
                    uri: uri,
                    metadata: meta.replace('##SPOTIFYURI##', id)
                      .replace('##RESOURCETITLE##', title || 'Sound Cloud Playlist')
                      .replace('##SPOTIFYTYPE##', 'object.container.playlistContainer')
              Severity: Major
              Found in lib/helpers.js - About 30 mins to fix

                Avoid too many return statements within this function.
                Open

                    return {
                      uri: uri,
                      metadata: meta.replace('##SPOTIFYURI##', id)
                        .replace('##RESOURCETITLE##', title || 'Sound Cloud Likes')
                        .replace('##SPOTIFYTYPE##', 'object.container.albumList')
                Severity: Major
                Found in lib/helpers.js - About 30 mins to fix

                  Avoid too many return statements within this function.
                  Open

                      return {
                        uri: 'x-sonosapi-radio:' + spotifyUri + '?sid=12&flags=8300&sn=5',
                        metadata: meta.replace('##SPOTIFYURI##', '100c206c' + spotifyUri)
                          .replace('##RESOURCETITLE##', spotifyTitle)
                          .replace('##SPOTIFYTYPE##', 'object.item.audioItem.audioBroadcast.#artistRadio')
                  Severity: Major
                  Found in lib/helpers.js - About 30 mins to fix

                    Avoid too many return statements within this function.
                    Open

                        return {
                          uri: 'x-rincon-cpcontainer:0004206c' + spotifyUri,
                          metadata: meta.replace('##SPOTIFYURI##', '0004206c' + spotifyUri)
                            .replace('##RESOURCETITLE##', '')
                            .replace('##SPOTIFYTYPE##', 'object.container.album.musicAlbum')
                    Severity: Major
                    Found in lib/helpers.js - About 30 mins to fix

                      Avoid too many return statements within this function.
                      Open

                          return {
                            uri: String(uri).replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"'),
                            metadata: meta.replace('##SPOTIFYURI##', id)
                              .replace('##RESOURCETITLE##', title)
                              .replace('##SPOTIFYTYPE##', 'object.container.playlistContainer')
                      Severity: Major
                      Found in lib/helpers.js - About 30 mins to fix

                        Avoid too many return statements within this function.
                        Open

                            return {
                              uri: 'x-rincon-cpcontainer:1006206c' + spotifyUri,
                              metadata: meta.replace('##SPOTIFYURI##', '1006206c' + spotifyUri)
                                .replace('##RESOURCETITLE##', '')
                                .replace('##SPOTIFYTYPE##', 'object.container.album.playlistContainer')
                        Severity: Major
                        Found in lib/helpers.js - About 30 mins to fix

                          Avoid too many return statements within this function.
                          Open

                              return { uri: uri, metadata: '' }
                          Severity: Major
                          Found in lib/helpers.js - About 30 mins to fix

                            Avoid too many return statements within this function.
                            Open

                                return {
                                  uri: 'x-rincon-cpcontainer:10062a6c' + spotifyUri + '?sid=9&flags=10860&sn=7',
                                  metadata: meta.replace('##SPOTIFYURI##', '10062a6c' + spotifyUri)
                                    .replace('##RESOURCETITLE##', 'User playlist')
                                    .replace('##SPOTIFYTYPE##', 'object.container.playlistContainer')
                            Severity: Major
                            Found in lib/helpers.js - About 30 mins to fix

                              Similar blocks of code found in 2 locations. Consider refactoring.
                              Open

                                  return {
                                    uri: uri,
                                    metadata: meta.replace('##SPOTIFYURI##', id)
                                      .replace('##RESOURCETITLE##', title || 'Sound Cloud Likes')
                                      .replace('##SPOTIFYTYPE##', 'object.container.albumList')
                              Severity: Major
                              Found in lib/helpers.js and 1 other location - About 2 hrs to fix
                              lib/helpers.js on lines 238..245

                              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 76.

                              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

                              Further Reading

                              Similar blocks of code found in 2 locations. Consider refactoring.
                              Open

                                  return {
                                    uri: uri,
                                    metadata: meta.replace('##SPOTIFYURI##', id)
                                      .replace('##RESOURCETITLE##', title || 'Sound Cloud Playlist')
                                      .replace('##SPOTIFYTYPE##', 'object.container.playlistContainer')
                              Severity: Major
                              Found in lib/helpers.js and 1 other location - About 2 hrs to fix
                              lib/helpers.js on lines 228..235

                              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 76.

                              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

                              Further Reading

                              Similar blocks of code found in 4 locations. Consider refactoring.
                              Open

                                } else if (uri.startsWith('spotify:artistTopTracks:')) { // Artist top tracks
                                  return {
                                    uri: 'x-rincon-cpcontainer:000e206c' + spotifyUri,
                                    metadata: meta.replace('##SPOTIFYURI##', '000e206c' + spotifyUri)
                                      .replace('##RESOURCETITLE##', '')
                              Severity: Major
                              Found in lib/helpers.js and 3 other locations - About 1 hr to fix
                              lib/helpers.js on lines 166..248
                              lib/helpers.js on lines 174..248
                              lib/helpers.js on lines 190..248

                              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 64.

                              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

                              Further Reading

                              Similar blocks of code found in 4 locations. Consider refactoring.
                              Open

                                } else if (uri.startsWith('spotify:album:')) { // Album
                                  return {
                                    uri: 'x-rincon-cpcontainer:0004206c' + spotifyUri,
                                    metadata: meta.replace('##SPOTIFYURI##', '0004206c' + spotifyUri)
                                      .replace('##RESOURCETITLE##', '')
                              Severity: Major
                              Found in lib/helpers.js and 3 other locations - About 1 hr to fix
                              lib/helpers.js on lines 166..248
                              lib/helpers.js on lines 182..248
                              lib/helpers.js on lines 190..248

                              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 64.

                              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

                              Further Reading

                              Similar blocks of code found in 4 locations. Consider refactoring.
                              Open

                                if (uri.startsWith('spotify:track:')) { // Just one track
                                  return {
                                    uri: 'x-sonos-spotify:' + spotifyUri,
                                    metadata: meta.replace('##SPOTIFYURI##', '00032020' + spotifyUri)
                                      .replace('##RESOURCETITLE##', '')
                              Severity: Major
                              Found in lib/helpers.js and 3 other locations - About 1 hr to fix
                              lib/helpers.js on lines 174..248
                              lib/helpers.js on lines 182..248
                              lib/helpers.js on lines 190..248

                              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 64.

                              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

                              Further Reading

                              Similar blocks of code found in 4 locations. Consider refactoring.
                              Open

                                } else if (uri.startsWith('spotify:playlist:')) { // Playlist
                                  return {
                                    uri: 'x-rincon-cpcontainer:1006206c' + spotifyUri,
                                    metadata: meta.replace('##SPOTIFYURI##', '1006206c' + spotifyUri)
                                      .replace('##RESOURCETITLE##', '')
                              Severity: Major
                              Found in lib/helpers.js and 3 other locations - About 1 hr to fix
                              lib/helpers.js on lines 166..248
                              lib/helpers.js on lines 174..248
                              lib/helpers.js on lines 182..248

                              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 64.

                              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

                              Further Reading

                              There are no issues that match your filters.

                              Category
                              Status