TryGhost/Ghost

View on GitHub
ghost/collections/src/CollectionsService.ts

Summary

Maintainability
F
4 days
Test Coverage

File CollectionsService.ts has 516 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import logging from '@tryghost/logging';
import tpl from '@tryghost/tpl';
import isEqual from 'lodash/isEqual';
import {Knex} from 'knex';
import {
Severity: Major
Found in ghost/collections/src/CollectionsService.ts - About 1 day to fix

    Function subscribeToEvents has 59 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        subscribeToEvents() {
            // @NOTE: event handling should be moved to the client - Ghost app
            //        Leaving commented out handlers here to move them all together
    
            // this.DomainEvents.subscribe(PostDeletedEvent, async (event: PostDeletedEvent) => {
    Severity: Major
    Found in ghost/collections/src/CollectionsService.ts - About 2 hrs to fix

      CollectionsService has 22 functions (exceeds 20 allowed). Consider refactoring.
      Open

      export class CollectionsService {
          private collectionsRepository: CollectionRepository;
          private postsRepository: PostsRepository;
          private DomainEvents: {
              // eslint-disable-next-line @typescript-eslint/no-explicit-any
      Severity: Minor
      Found in ghost/collections/src/CollectionsService.ts - About 2 hrs to fix

        Function edit has 39 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            async edit(data: any): Promise<CollectionDTO | null> {
                return await this.collectionsRepository.createTransaction(async (transaction) => {
                    const collection = await this.collectionsRepository.getById(data.id, {transaction});
        
                    if (!collection) {
        Severity: Minor
        Found in ghost/collections/src/CollectionsService.ts - About 1 hr to fix

          Function updatePostsInCollections has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
          Open

              async updatePostsInCollections(postIds: string[], collections: Collection[], transaction: Knex.Transaction) {
                  const posts = await this.postsRepository.getAll({
                      filter: `id:[${postIds.join(',')}]`,
                      transaction: transaction
                  });
          Severity: Minor
          Found in ghost/collections/src/CollectionsService.ts - About 55 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

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

              async updateFeaturedPosts(postIds: string[]) {
                  return await this.collectionsRepository.createTransaction(async (transaction) => {
                      let collections = await this.collectionsRepository.getAll({
                          filter: 'type:automatic+slug:-latest',
                          transaction
          Severity: Major
          Found in ghost/collections/src/CollectionsService.ts and 1 other location - About 4 hrs to fix
          ghost/collections/src/CollectionsService.ts on lines 448..464

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

          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

              async updateUnpublishedPosts(postIds: string[]) {
                  return await this.collectionsRepository.createTransaction(async (transaction) => {
                      let collections = await this.collectionsRepository.getAll({
                          filter: 'type:automatic+slug:-latest+slug:-featured',
                          transaction
          Severity: Major
          Found in ghost/collections/src/CollectionsService.ts and 1 other location - About 4 hrs to fix
          ghost/collections/src/CollectionsService.ts on lines 466..482

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

          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

                  this.DomainEvents.subscribe(PostsBulkFeaturedEvent, async (event: PostsBulkFeaturedEvent) => {
                      logging.info(`PostsBulkFeaturedEvent received, updating collection posts ${event.data}`);
                      try {
                          await this.updateFeaturedPosts(event.data);
                          /* c8 ignore next 3 */
          Severity: Major
          Found in ghost/collections/src/CollectionsService.ts and 1 other location - About 3 hrs to fix
          ghost/collections/src/CollectionsService.ts on lines 250..258

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

          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

                  this.DomainEvents.subscribe(PostsBulkUnfeaturedEvent, async (event: PostsBulkUnfeaturedEvent) => {
                      logging.info(`PostsBulkUnfeaturedEvent received, updating collection posts ${event.data}`);
                      try {
                          await this.updateFeaturedPosts(event.data);
                          /* c8 ignore next 3 */
          Severity: Major
          Found in ghost/collections/src/CollectionsService.ts and 1 other location - About 3 hrs to fix
          ghost/collections/src/CollectionsService.ts on lines 240..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 106.

          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

                  const previous = {
                      id: postEditEvent.previous.id,
                      featured: postEditEvent.previous.featured,
                      published_at: postEditEvent.previous.published_at,
                      tags: postEditEvent.previous.tags
          Severity: Major
          Found in ghost/collections/src/CollectionsService.ts and 1 other location - About 1 hr to fix
          ghost/collections/src/CollectionsService.ts on lines 282..287

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

          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

                  const current = {
                      id: postEditEvent.current.id,
                      featured: postEditEvent.current.featured,
                      published_at: postEditEvent.current.published_at,
                      tags: postEditEvent.current.tags
          Severity: Major
          Found in ghost/collections/src/CollectionsService.ts and 1 other location - About 1 hr to fix
          ghost/collections/src/CollectionsService.ts on lines 288..293

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

          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

              async getBySlug(slug: string, options?: {transaction: Knex.Transaction}): Promise<CollectionDTO | null> {
                  const collection = await this.collectionsRepository.getBySlug(slug, options);
                  if (!collection) {
                      return null;
                  }
          Severity: Minor
          Found in ghost/collections/src/CollectionsService.ts and 1 other location - About 45 mins to fix
          ghost/collections/src/CollectionsService.ts on lines 572..578

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

          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

              async getById(id: string, options?: {transaction: Knex.Transaction}): Promise<CollectionDTO | null> {
                  const collection = await this.collectionsRepository.getById(id, options);
                  if (!collection) {
                      return null;
                  }
          Severity: Minor
          Found in ghost/collections/src/CollectionsService.ts and 1 other location - About 45 mins to fix
          ghost/collections/src/CollectionsService.ts on lines 580..586

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

          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

                      try {
                          await this.updateAllAutomaticCollections();
                          /* c8 ignore next 3 */
                      } catch (err) {
                          logging.error({err, message: 'Error handling PostsBulkAddTagsEvent'});
          Severity: Minor
          Found in ghost/collections/src/CollectionsService.ts and 1 other location - About 40 mins to fix
          ghost/collections/src/CollectionsService.ts on lines 262..267

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

          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

                      try {
                          await this.updateAllAutomaticCollections();
                          /* c8 ignore next 3 */
                      } catch (err) {
                          logging.error({err, message: 'Error handling TagDeletedEvent'});
          Severity: Minor
          Found in ghost/collections/src/CollectionsService.ts and 1 other location - About 40 mins to fix
          ghost/collections/src/CollectionsService.ts on lines 272..277

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

          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