BookStackApp/BookStack

View on GitHub
app/Entities/Tools/BookContents.php

Summary

Maintainability
C
1 day
Test Coverage

Function isSortChangePermissible has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
Open

    protected function isSortChangePermissible(BookSortMapItem $sortMapItem, BookChild $model, ?Entity $currentParent, ?Entity $newBook, ?Entity $newChapter): bool
    {
        // Stop if we can't see the current parent or new book.
        if (!$currentParent || !$newBook) {
            return false;
Severity: Minor
Found in app/Entities/Tools/BookContents.php - 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

Consider simplifying this complex logical expression.
Open

        if ($model instanceof Page) {
            $parentPermission = ($currentParent instanceof Chapter) ? 'chapter-update' : 'book-update';
            $hasCurrentParentPermission = userCan($parentPermission, $currentParent);

            // This needs to check if there was an intended chapter location in the original sort map
Severity: Critical
Found in app/Entities/Tools/BookContents.php - About 2 hrs to fix

    Method isSortChangePermissible has 38 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        protected function isSortChangePermissible(BookSortMapItem $sortMapItem, BookChild $model, ?Entity $currentParent, ?Entity $newBook, ?Entity $newChapter): bool
        {
            // Stop if we can't see the current parent or new book.
            if (!$currentParent || !$newBook) {
                return false;
    Severity: Minor
    Found in app/Entities/Tools/BookContents.php - About 1 hr to fix

      Method applySortUpdates has 32 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          protected function applySortUpdates(BookSortMapItem $sortMapItem, array $modelMap): void
          {
              /** @var BookChild $model */
              $model = $modelMap[$sortMapItem->type . ':' . $sortMapItem->id] ?? null;
              if (!$model) {
      Severity: Minor
      Found in app/Entities/Tools/BookContents.php - About 1 hr to fix

        Method loadModelsFromSortMap has 31 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            protected function loadModelsFromSortMap(BookSortMap $sortMap): array
            {
                $modelMap = [];
                $ids = [
                    'chapter' => [],
        Severity: Minor
        Found in app/Entities/Tools/BookContents.php - About 1 hr to fix

          Function applySortUpdates has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
          Open

              protected function applySortUpdates(BookSortMapItem $sortMapItem, array $modelMap): void
              {
                  /** @var BookChild $model */
                  $model = $modelMap[$sortMapItem->type . ':' . $sortMapItem->id] ?? null;
                  if (!$model) {
          Severity: Minor
          Found in app/Entities/Tools/BookContents.php - About 1 hr 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 ($model instanceof Chapter) {
                      $hasPermission = userCan('book-update', $currentParent)
                          && userCan('book-update', $newBook)
                          && userCan('chapter-update', $model)
                          && (!$hasNewParent || userCan('chapter-create', $newBook))
          Severity: Major
          Found in app/Entities/Tools/BookContents.php - About 1 hr to fix

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

                protected function loadModelsFromSortMap(BookSortMap $sortMap): array
                {
                    $modelMap = [];
                    $ids = [
                        'chapter' => [],
            Severity: Minor
            Found in app/Entities/Tools/BookContents.php - 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

            Method isSortChangePermissible has 5 arguments (exceeds 4 allowed). Consider refactoring.
            Open

                protected function isSortChangePermissible(BookSortMapItem $sortMapItem, BookChild $model, ?Entity $currentParent, ?Entity $newBook, ?Entity $newChapter): bool
            Severity: Minor
            Found in app/Entities/Tools/BookContents.php - About 35 mins to fix

              Avoid too many return statements within this method.
              Open

                      return true;
              Severity: Major
              Found in app/Entities/Tools/BookContents.php - About 30 mins to fix

                There are no issues that match your filters.

                Category
                Status