EscolaLMS/Cart

View on GitHub
src/Services/ProductService.php

Summary

Maintainability
F
4 days
Test Coverage

File ProductService.php has 534 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php

namespace EscolaLms\Cart\Services;

use EscolaLms\Cart\Contracts\Productable;
Severity: Major
Found in src/Services/ProductService.php - About 1 day to fix

    ProductService has 29 functions (exceeds 20 allowed). Consider refactoring.
    Open

    class ProductService implements ProductServiceContract
    {
        protected array $productables = [];
        protected array $productablesMorphs = [];
    
    
    Severity: Minor
    Found in src/Services/ProductService.php - About 3 hrs to fix

      Method update has 77 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          public function update(Product $product, array $data): Product
          {
              if (ProductType::isSubscriptionType($product->type) && !ProductType::isSubscriptionType($data['type'])) {
                  throw new Exception(__('Product with subscription type cannot have type changed'));
              }
      Severity: Major
      Found in src/Services/ProductService.php - About 3 hrs to fix

        Function attachProductToUser has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
        Open

            public function attachProductToUser(Product $product, User $user, int $quantity = 1): void
            {
                Log::debug(__('Attaching product to user'), [
                    'product' => [
                        'id' => $product->getKey(),
        Severity: Minor
        Found in src/Services/ProductService.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

        Method attachProductToUser has 69 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            public function attachProductToUser(Product $product, User $user, int $quantity = 1): void
            {
                Log::debug(__('Attaching product to user'), [
                    'product' => [
                        'id' => $product->getKey(),
        Severity: Major
        Found in src/Services/ProductService.php - About 2 hrs to fix

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

              public function update(Product $product, array $data): Product
              {
                  if (ProductType::isSubscriptionType($product->type) && !ProductType::isSubscriptionType($data['type'])) {
                      throw new Exception(__('Product with subscription type cannot have type changed'));
                  }
          Severity: Minor
          Found in src/Services/ProductService.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

          Function searchAndPaginateProducts has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
          Open

              public function searchAndPaginateProducts(ProductsSearchDto $searchDto, ?OrderDto $orderDto = null): LengthAwarePaginator
              {
                  $query = Product::query();
          
                  if (!is_null($searchDto->getName())) {
          Severity: Minor
          Found in src/Services/ProductService.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

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

              public function listAllProductables(): Collection
              {
                  $collection = Collection::empty();
                  foreach ($this->listRegisteredProductableClasses() as $productableClass) {
                      /** @var Model&Productable $model */
          Severity: Minor
          Found in src/Services/ProductService.php - About 1 hr to fix

            Method searchAndPaginateProducts has 33 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                public function searchAndPaginateProducts(ProductsSearchDto $searchDto, ?OrderDto $orderDto = null): LengthAwarePaginator
                {
                    $query = Product::query();
            
                    if (!is_null($searchDto->getName())) {
            Severity: Minor
            Found in src/Services/ProductService.php - About 1 hr to fix

              Method saveProductProductables has 30 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  private function saveProductProductables(Product $product, array $productables)
                  {
                      if ($product->type === ProductType::SINGLE && count($productables) > 1) {
                          throw new Exception(__('Product with type SINGLE can contain only one single Productable'));
                      }
              Severity: Minor
              Found in src/Services/ProductService.php - About 1 hr to fix

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

                    private function saveProductProductables(Product $product, array $productables)
                    {
                        if ($product->type === ProductType::SINGLE && count($productables) > 1) {
                            throw new Exception(__('Product with type SINGLE can contain only one single Productable'));
                        }
                Severity: Minor
                Found in src/Services/ProductService.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

                Method attachProductableToUser has 27 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                    public function attachProductableToUser(Productable $productable, User $user, int $quantity = 1, ?Product $product = null): void
                    {
                        Log::debug(__('Attaching productable to user'), [
                            'productable' => [
                                'id' => $productable->getKey(),
                Severity: Minor
                Found in src/Services/ProductService.php - About 1 hr to fix

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

                      public function detachProductFromUser(Product $product, User $user, int $quantity = 1): void
                      {
                          $productUserPivot = ProductUser::where(['user_id' => $user->getKey(), 'product_id' => $product->getKey()])->first();
                  
                          if (!$productUserPivot) {
                  Severity: Minor
                  Found in src/Services/ProductService.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

                  Function listAllProductables has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                  Open

                      public function listAllProductables(): Collection
                      {
                          $collection = Collection::empty();
                          foreach ($this->listRegisteredProductableClasses() as $productableClass) {
                              /** @var Model&Productable $model */
                  Severity: Minor
                  Found in src/Services/ProductService.php - About 35 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

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

                          if (!is_null($searchDto->getProductableType())) {
                              $class = $searchDto->getProductableType();
                              /** @var Model $model */
                              $model = new $class();
                              if (!is_null($searchDto->getProductableId())) {
                  Severity: Minor
                  Found in src/Services/ProductService.php and 1 other location - About 45 mins to fix
                  src/Services/OrderService.php on lines 230..239

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

                  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