SAREhub/php_component_worker

View on GitHub
src/SAREhub/Component/Worker/Manager/WorkerManager.php

Summary

Maintainability
A
1 hr
Test Coverage

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

    public function onStopAllCommand(Command $command, callable $replyCallback) {
        $workerList = $this->getWorkerList();
        $replyAll = [];
        $inputCommand = $command;
        $stopAllCallback = function (Command $command, CommandReply $reply) use ($inputCommand, &$replyAll, &$workerList, $replyCallback) {
Severity: Minor
Found in src/SAREhub/Component/Worker/Manager/WorkerManager.php - About 1 hr to fix

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

        public function onStopAllCommand(Command $command, callable $replyCallback) {
            $workerList = $this->getWorkerList();
            $replyAll = [];
            $inputCommand = $command;
            $stopAllCallback = function (Command $command, CommandReply $reply) use ($inputCommand, &$replyAll, &$workerList, $replyCallback) {
    Severity: Minor
    Found in src/SAREhub/Component/Worker/Manager/WorkerManager.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 using static access to class 'SAREhub\Component\Worker\Manager\ManagerCommands' in method 'doStop'.
    Open

            $this->processCommand(ManagerCommands::stopAll('doStopManager'.TimeProvider::get()->now()), function () {

    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 '\SAREhub\Component\Worker\Command\CommandReply' in method 'onUnknownCommand'.
    Open

            $replyCallback($command, CommandReply::error('unknown command', $command->getName()));

    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 'SAREhub\Component\Worker\Manager\ManagerCommands' in method 'onStopAllCommand'.
    Open

                $managerCommand = ManagerCommands::stop($correlationId.$workerId, $workerId);

    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 '\SAREhub\Component\Worker\Command\CommandReply' in method 'onStartCommand'.
    Open

                $reply = CommandReply::success($command->getCorrelationId(), $message);

    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 '\SAREhub\Component\Worker\Command\CommandReply' in method 'onStopAllCommand'.
    Open

                    $replyCallback($inputCommand, CommandReply::reply(
                      $inputCommand->getCorrelationId(),
                      $status,
                      $convertedReply)

    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 '\SAREhub\Component\Worker\Command\CommandReply' in method 'onStartCommand'.
    Open

                $reply = CommandReply::error($command->getCorrelationId(), $message);

    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 '\SAREhub\Component\Worker\WorkerCommands' in method 'onStopCommand'.
    Open

                  ->withCommand(WorkerCommands::stop($command->getCorrelationId()))

    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 onStartCommand uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
    Open

            } else {
                $this->getProcessService()->registerWorker($id);
                $message = 'worker started with PID: '.$this->getProcessService()->getWorkerPid($id);
                $this->getLogger()->info($message, $context);
                $reply = CommandReply::success($command->getCorrelationId(), $message);

    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 SAREhub\Component\Worker\Manager\count() function in while loops.
    Open

            while (count($this->getWorkerList())) {
                $this->doTick();
            }

    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