filestack/filestack-php

View on GitHub
filestack/UploadProcessor.php

Summary

Maintainability
D
1 day
Test Coverage

File UploadProcessor.php has 349 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
namespace Filestack;

use Filestack\FilestackConfig;
use Filestack\HttpStatusCodes;
Severity: Minor
Found in filestack/UploadProcessor.php - About 4 hrs to fix

    The class UploadProcessor has an overall complexity of 52 which is very high. The configured complexity threshold is 50.
    Open

    class UploadProcessor
    {
        use Mixins\CommonMixin;
    
        public $api_key;
    Severity: Minor
    Found in filestack/UploadProcessor.php by phpmd

    Function processChunks has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
    Open

        protected function processChunks($part, $chunks)
        {
            $upload_url = $this->getCustomUrl(FilestackConfig::UPLOAD_URL) . '/multipart/upload';
            $max_retries = FilestackConfig::MAX_RETRIES;
    
    
    Severity: Minor
    Found in filestack/UploadProcessor.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 createParts has 52 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        protected function createParts($api_key, $metadata, $upload_data)
        {
            $parts = [];
            $max_part_size = FilestackConfig::UPLOAD_PART_SIZE;
            $max_chunk_size = FilestackConfig::UPLOAD_CHUNK_SIZE;
    Severity: Major
    Found in filestack/UploadProcessor.php - About 2 hrs to fix

      Function createParts has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
      Open

          protected function createParts($api_key, $metadata, $upload_data)
          {
              $parts = [];
              $max_part_size = FilestackConfig::UPLOAD_PART_SIZE;
              $max_chunk_size = FilestackConfig::UPLOAD_CHUNK_SIZE;
      Severity: Minor
      Found in filestack/UploadProcessor.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 processChunks has 44 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          protected function processChunks($part, $chunks)
          {
              $upload_url = $this->getCustomUrl(FilestackConfig::UPLOAD_URL) . '/multipart/upload';
              $max_retries = FilestackConfig::MAX_RETRIES;
      
      
      Severity: Minor
      Found in filestack/UploadProcessor.php - About 1 hr to fix

        Method processParts has 29 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            protected function processParts($parts)
            {
                $num_parts = count($parts);
                $parts_etags = [];
                $parts_completed = 0;
        Severity: Minor
        Found in filestack/UploadProcessor.php - About 1 hr to fix

          Method registerComplete has 26 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              protected function registerComplete($api_key, $parts_etags, $upload_data,
                                                    $metadata)
              {
                  $data = [];
                  $this->appendData($data, 'apikey',          $api_key);
          Severity: Minor
          Found in filestack/UploadProcessor.php - About 1 hr to fix

            Function handleS3PromisesResult has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
            Open

                protected function handleS3PromisesResult($s3_results)
                {
                    foreach ($s3_results as $promise) {
                        if ($promise['state'] !== 'fulfilled') {
                            $code = HttpStatusCodes::HTTP_SERVICE_UNAVAILABLE;
            Severity: Minor
            Found in filestack/UploadProcessor.php - About 25 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

            Avoid using static access to class '\Filestack\HttpStatusCodes' in method 'processChunks'.
            Open

                                    HttpStatusCodes::isServerError($status_code)) {
            Severity: Minor
            Found in filestack/UploadProcessor.php by phpmd

            StaticAccess

            Since: 1.4.0

            Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

            Example

            class Foo
            {
                public function bar()
                {
                    Bar::baz();
                }
            }

            Source https://phpmd.org/rules/cleancode.html#staticaccess

            The method createParts uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
            Open

                        else {
                            // 1 part = 1 chunk
                            array_push($chunks, [
                                'offset'        => 0,
                                'seek_point'    => $seek_point,
            Severity: Minor
            Found in filestack/UploadProcessor.php by phpmd

            ElseExpression

            Since: 1.4.0

            An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

            Example

            class Foo
            {
                public function bar($flag)
                {
                    if ($flag) {
                        // one branch
                    } else {
                        // another branch
                    }
                }
            }

            Source https://phpmd.org/rules/cleancode.html#elseexpression

            Avoid using static access to class '\Filestack\HttpStatusCodes' in method 'processChunks'.
            Open

                                if (HttpStatusCodes::isServerError($status_code)) {
            Severity: Minor
            Found in filestack/UploadProcessor.php by phpmd

            StaticAccess

            Since: 1.4.0

            Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

            Example

            class Foo
            {
                public function bar()
                {
                    Bar::baz();
                }
            }

            Source https://phpmd.org/rules/cleancode.html#staticaccess

            Avoid using static access to class '\Filestack\HttpStatusCodes' in method 'processChunks'.
            Open

                                if (HttpStatusCodes::isNetworkError($status_code) ||
            Severity: Minor
            Found in filestack/UploadProcessor.php by phpmd

            StaticAccess

            Since: 1.4.0

            Static access causes unexchangeable dependencies to other classes and leads to hard to test code. Avoid using static access at all costs and instead inject dependencies through the constructor. The only case when static access is acceptable is when used for factory methods.

            Example

            class Foo
            {
                public function bar()
                {
                    Bar::baz();
                }
            }

            Source https://phpmd.org/rules/cleancode.html#staticaccess

            The method processParts uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
            Open

                        else {
                            $part_num = array_key_exists('part_num', $part) ?
                                $part['part_num'] : 1;
            
                            $this->multipartGetTags($part_num, $s3_results, $parts_etags);
            Severity: Minor
            Found in filestack/UploadProcessor.php by phpmd

            ElseExpression

            Since: 1.4.0

            An if expression with an else branch is basically not necessary. You can rewrite the conditions in a way that the else clause is not necessary and the code becomes simpler to read. To achieve this, use early return statements, though you may need to split the code it several smaller methods. For very simple assignments you could also use the ternary operations.

            Example

            class Foo
            {
                public function bar($flag)
                {
                    if ($flag) {
                        // one branch
                    } else {
                        // another branch
                    }
                }
            }

            Source https://phpmd.org/rules/cleancode.html#elseexpression

            Avoid unused local variables such as '$part_num'.
            Open

                    $part_num = 1;
            Severity: Minor
            Found in filestack/UploadProcessor.php by phpmd

            UnusedLocalVariable

            Since: 0.2

            Detects when a local variable is declared and/or assigned, but not used.

            Example

            class Foo {
                public function doSomething()
                {
                    $i = 5; // Unused
                }
            }

            Source https://phpmd.org/rules/unusedcode.html#unusedlocalvariable

            Avoid unused local variables such as '$max_retries'.
            Open

                    $max_retries = FilestackConfig::MAX_RETRIES;
            Severity: Minor
            Found in filestack/UploadProcessor.php by phpmd

            UnusedLocalVariable

            Since: 0.2

            Detects when a local variable is declared and/or assigned, but not used.

            Example

            class Foo {
                public function doSomething()
                {
                    $i = 5; // Unused
                }
            }

            Source https://phpmd.org/rules/unusedcode.html#unusedlocalvariable

            Avoid using Filestack\count() function in for loops.
            Open

                    for($i=0; $i<count($chunks); $i++) {
                        $current_chunk = $chunks[$i];
                        $seek_point = $current_chunk['seek_point'];
            
                        $chunk_content = $this->getChunkContent($part['filepath'], $seek_point,
            Severity: Minor
            Found in filestack/UploadProcessor.php by phpmd

            CountInLoopExpression

            Since: 2.7.0

            Using count/sizeof in loops expressions is considered bad practice and is a potential source of many bugs, especially when the loop manipulates an array, as count happens on each iteration.

            Example

            class Foo {
            
              public function bar()
              {
                $array = array();
            
                for ($i = 0; count($array); $i++) {
                  // ...
                }
              }
            }

            Source https://phpmd.org/rules/design.html#countinloopexpression

            There are no issues that match your filters.

            Category
            Status