rugk/xenforo-threema-gateway

View on GitHub
src/library/ThreemaGateway/Handler/Action/Callback.php

Summary

Maintainability
D
1 day
Test Coverage

validatePreConditions accesses the super-global variable $_SERVER.
Open

    public function validatePreConditions(&$errorString)
    {
        /** @var XenForo_Options $options */
        $options = XenForo_Application::getOptions();

Superglobals

Since: 0.2

Accessing a super-global variable directly is considered a bad practice. These variables should be encapsulated in objects that are provided by a framework, for instance.

Example

class Foo {
    public function bar() {
        $name = $_POST['foo'];
    }
}

Source

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

class ThreemaGateway_Handler_Action_Callback extends ThreemaGateway_Handler_Action_Abstract
{
    /**
     * @var string used by strtotime to allow messages sent in the future
     */

File Callback.php has 286 lines of code (exceeds 250 allowed). Consider refactoring.
Open

<?php
/**
 * Allows XenForo to receive Threema messages by providing a callback.
 *
 * @package ThreemaGateway
Severity: Minor
Found in src/library/ThreemaGateway/Handler/Action/Callback.php - About 2 hrs to fix

    Method processMessage has 51 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        public function processMessage($downloadPath, $debugMode = false)
        {
            /** @var string $output */
            $output = '';
    
    
    Severity: Major
    Found in src/library/ThreemaGateway/Handler/Action/Callback.php - About 2 hrs to fix

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

          protected function getLogData(
              Threema\MsgApi\Helpers\ReceiveMessageResult $receiveResult,
              Threema\MsgApi\Messages\ThreemaMessage $threemaMsg
          ) {
              $eol = PHP_EOL;
      Severity: Minor
      Found in src/library/ThreemaGateway/Handler/Action/Callback.php - About 1 hr to fix

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

            protected function saveMessage(
                Threema\MsgApi\Helpers\ReceiveMessageResult $receiveResult,
                Threema\MsgApi\Messages\ThreemaMessage $threemaMsg
            ) {
                $dataWriter = XenForo_DataWriter::create('ThreemaGateway_DataWriter_Messages');
        Severity: Minor
        Found in src/library/ThreemaGateway/Handler/Action/Callback.php - About 1 hr to fix

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

              public function validatePreConditions(&$errorString)
              {
                  /** @var XenForo_Options $options */
                  $options = XenForo_Application::getOptions();
          
          
          Severity: Minor
          Found in src/library/ThreemaGateway/Handler/Action/Callback.php - About 1 hr to fix

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

                public function validateRequest(&$errorString)
                {
                    // access token validation (authentication of Gateway server)
                    /** @var XenForo_Options $options */
                    $options = XenForo_Application::getOptions();
            Severity: Minor
            Found in src/library/ThreemaGateway/Handler/Action/Callback.php - About 1 hr to fix

              Consider simplifying this complex logical expression.
              Open

                      if (!$this->input->inRequest('from') ||
                          !$this->input->inRequest('to') ||
                          !$this->input->inRequest('messageId') ||
                          !$this->input->inRequest('date') ||
                          !$this->input->inRequest('nonce') ||
              Severity: Major
              Found in src/library/ThreemaGateway/Handler/Action/Callback.php - About 1 hr to fix

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

                    public function validatePreConditions(&$errorString)
                    {
                        /** @var XenForo_Options $options */
                        $options = XenForo_Application::getOptions();
                
                
                Severity: Minor
                Found in src/library/ThreemaGateway/Handler/Action/Callback.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 saveMessage has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                    protected function saveMessage(
                        Threema\MsgApi\Helpers\ReceiveMessageResult $receiveResult,
                        Threema\MsgApi\Messages\ThreemaMessage $threemaMsg
                    ) {
                        $dataWriter = XenForo_DataWriter::create('ThreemaGateway_DataWriter_Messages');
                Severity: Minor
                Found in src/library/ThreemaGateway/Handler/Action/Callback.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

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

                    public function addLog(&$log, $stringToAdd, $stringToAddDetail = null)
                    {
                        // convert to array if necessary or just add string
                        if (is_string($log)) {
                            if ($stringToAddDetail) {
                Severity: Minor
                Found in src/library/ThreemaGateway/Handler/Action/Callback.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

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

                    public function processMessage($downloadPath, $debugMode = false)
                    {
                        /** @var string $output */
                        $output = '';
                
                
                Severity: Minor
                Found in src/library/ThreemaGateway/Handler/Action/Callback.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

                Avoid too many return statements within this method.
                Open

                        return true;
                Severity: Major
                Found in src/library/ThreemaGateway/Handler/Action/Callback.php - About 30 mins to fix

                  The method validatePreConditions() has a Cyclomatic Complexity of 13. The configured cyclomatic complexity threshold is 10.
                  Open

                      public function validatePreConditions(&$errorString)
                      {
                          /** @var XenForo_Options $options */
                          $options = XenForo_Application::getOptions();
                  
                  

                  CyclomaticComplexity

                  Since: 0.1

                  Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.

                  Example

                  // Cyclomatic Complexity = 11
                  class Foo {
                  1   public function example() {
                  2       if ($a == $b) {
                  3           if ($a1 == $b1) {
                                  fiddle();
                  4           } elseif ($a2 == $b2) {
                                  fiddle();
                              } else {
                                  fiddle();
                              }
                  5       } elseif ($c == $d) {
                  6           while ($c == $d) {
                                  fiddle();
                              }
                  7        } elseif ($e == $f) {
                  8           for ($n = 0; $n < $h; $n++) {
                                  fiddle();
                              }
                          } else {
                              switch ($z) {
                  9               case 1:
                                      fiddle();
                                      break;
                  10              case 2:
                                      fiddle();
                                      break;
                  11              case 3:
                                      fiddle();
                                      break;
                                  default:
                                      fiddle();
                                      break;
                              }
                          }
                      }
                  }

                  Source https://phpmd.org/rules/codesize.html#cyclomaticcomplexity

                  The class ThreemaGateway_Handler_Action_Callback has a coupling between objects value of 18. Consider to reduce the number of dependencies under 13.
                  Open

                  class ThreemaGateway_Handler_Action_Callback extends ThreemaGateway_Handler_Action_Abstract
                  {
                      /**
                       * @var string used by strtotime to allow messages sent in the future
                       */

                  CouplingBetweenObjects

                  Since: 1.1.0

                  A class with too many dependencies has negative impacts on several quality aspects of a class. This includes quality criteria like stability, maintainability and understandability

                  Example

                  class Foo {
                      /**
                       * @var \foo\bar\X
                       */
                      private $x = null;
                  
                      /**
                       * @var \foo\bar\Y
                       */
                      private $y = null;
                  
                      /**
                       * @var \foo\bar\Z
                       */
                      private $z = null;
                  
                      public function setFoo(\Foo $foo) {}
                      public function setBar(\Bar $bar) {}
                      public function setBaz(\Baz $baz) {}
                  
                      /**
                       * @return \SplObjectStorage
                       * @throws \OutOfRangeException
                       * @throws \InvalidArgumentException
                       * @throws \ErrorException
                       */
                      public function process(\Iterator $it) {}
                  
                      // ...
                  }

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

                  Missing class import via use statement (line '244', column '23').
                  Open

                              throw new XenForo_Exception('Message cannot be processed: [ResultErrors] ' . implode('|', $receiveResult->getErrors()));

                  MissingImport

                  Since: 2.7.0

                  Importing all external classes in a file through use statements makes them clearly visible.

                  Example

                  function make() {
                      return new \stdClass();
                  }

                  Source http://phpmd.org/rules/cleancode.html#MissingImport

                  Missing class import via use statement (line '342', column '25').
                  Open

                          $receiver = new ThreemaGateway_Handler_Action_Receiver(true, true);

                  MissingImport

                  Since: 2.7.0

                  Importing all external classes in a file through use statements makes them clearly visible.

                  Example

                  function make() {
                      return new \stdClass();
                  }

                  Source http://phpmd.org/rules/cleancode.html#MissingImport

                  Missing class import via use statement (line '240', column '23').
                  Open

                              throw new XenForo_Exception('Message cannot be processed: [' . get_class($e) . '] ' . $e->getMessage());

                  MissingImport

                  Since: 2.7.0

                  Importing all external classes in a file through use statements makes them clearly visible.

                  Example

                  function make() {
                      return new \stdClass();
                  }

                  Source http://phpmd.org/rules/cleancode.html#MissingImport

                  Missing class import via use statement (line '277', column '23').
                  Open

                              throw new XenForo_Exception('Message could not be saved: [' . get_class($e) . '] ' . $e->getMessage());

                  MissingImport

                  Since: 2.7.0

                  Importing all external classes in a file through use statements makes them clearly visible.

                  Example

                  function make() {
                      return new \stdClass();
                  }

                  Source http://phpmd.org/rules/cleancode.html#MissingImport

                  Missing class import via use statement (line '346', column '23').
                  Open

                              throw new XenForo_Exception('Message "' . $messageId . '" has already been received and is already saved. This may indicate a replay attack.');

                  MissingImport

                  Since: 2.7.0

                  Importing all external classes in a file through use statements makes them clearly visible.

                  Example

                  function make() {
                      return new \stdClass();
                  }

                  Source http://phpmd.org/rules/cleancode.html#MissingImport

                  Missing class import via use statement (line '41', column '28').
                  Open

                          $this->input = new XenForo_Input($request);

                  MissingImport

                  Since: 2.7.0

                  Importing all external classes in a file through use statements makes them clearly visible.

                  Example

                  function make() {
                      return new \stdClass();
                  }

                  Source http://phpmd.org/rules/cleancode.html#MissingImport

                  The method processMessage has a boolean flag argument $debugMode, which is a certain sign of a Single Responsibility Principle violation.
                  Open

                      public function processMessage($downloadPath, $debugMode = false)

                  BooleanArgumentFlag

                  Since: 1.4.0

                  A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.

                  Example

                  class Foo {
                      public function bar($flag = true) {
                      }
                  }

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

                  Missing class import via use statement (line '225', column '23').
                  Open

                              throw new XenForo_Exception('Download dir ' . $downloadPath . ' cannot be accessed.');

                  MissingImport

                  Since: 2.7.0

                  Importing all external classes in a file through use statements makes them clearly visible.

                  Example

                  function make() {
                      return new \stdClass();
                  }

                  Source http://phpmd.org/rules/cleancode.html#MissingImport

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

                              } else {
                                  $log .= PHP_EOL . $stringToAdd;
                                  return;
                              }

                  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

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

                              } else {
                                  $this->saveMessageId($receiveResult->getMessageId());
                              }

                  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 'XenForo_DataWriter' in method 'saveMessageId'.
                  Open

                          $dataWriter = XenForo_DataWriter::create('ThreemaGateway_DataWriter_Messages');

                  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 'XenForo_Application' in method 'validateFormalities'.
                  Open

                          $options   = XenForo_Application::getOptions();

                  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 'XenForo_Error' in method 'processMessage'.
                  Open

                              XenForo_Error::logException($e);

                  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 'XenForo_DataWriter' in method 'saveMessage'.
                  Open

                          $dataWriter = XenForo_DataWriter::create('ThreemaGateway_DataWriter_Messages');

                  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 'XenForo_Error' in method 'processMessage'.
                  Open

                              XenForo_Error::logException($e);

                  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 'XenForo_CodeEvent' in method 'processMessage'.
                  Open

                          XenForo_CodeEvent::fire('threemagw_message_callback_presave', [
                              $this,
                              $receiveResult,
                              $threemaMsg,
                              &$output,

                  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 'XenForo_CodeEvent' in method 'processMessage'.
                  Open

                          XenForo_CodeEvent::fire('threemagw_message_callback_postsave', [
                              $this,
                              $receiveResult,
                              $threemaMsg,
                              &$output,

                  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 'XenForo_Application' in method 'validatePreConditions'.
                  Open

                          $options = XenForo_Application::getOptions();

                  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 'XenForo_Application' in method 'validateRequest'.
                  Open

                          $options = XenForo_Application::getOptions();

                  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 'ThreemaGateway_Handler_Validation' in method 'processMessage'.
                  Open

                          if (!ThreemaGateway_Handler_Validation::checkDir($downloadPath)) {

                  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

                  There are no issues that match your filters.

                  Category
                  Status