Completed
Push — master ( efc44e...835840 )
by Pablo
02:59
created

PrePushToolHandler   B

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 23.93 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 16
dl 28
loc 117
rs 8.4614
ccs 40
cts 40
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B execute() 28 44 5
A executeOriginalHook() 0 10 2
A handle() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PhpGitHooks\Module\Git\Contract\Command;
4
5
use Bruli\EventBusBundle\CommandBus\CommandBus;
6
use Bruli\EventBusBundle\CommandBus\CommandHandlerInterface;
7
use Bruli\EventBusBundle\CommandBus\CommandInterface;
8
use Bruli\EventBusBundle\QueryBus\QueryBus;
9
use PhpGitHooks\Module\Configuration\Contract\Query\ConfigurationDataFinder;
10
use PhpGitHooks\Module\Configuration\Contract\Response\ConfigurationDataResponse;
11
use PhpGitHooks\Module\Git\Contract\Exception\InvalidPushException;
12
use PhpGitHooks\Module\Git\Contract\Response\BadJobLogoResponse;
13
use PhpGitHooks\Module\Git\Contract\Response\GoodJobLogoResponse;
14
use PhpGitHooks\Module\Git\Model\PrePushOriginalExecutorInterface;
15
use PhpGitHooks\Module\PhpUnit\Contract\Command\GuardCoverageCommand;
16
use PhpGitHooks\Module\PhpUnit\Contract\Command\PhpUnitToolCommand;
17
use PhpGitHooks\Module\PhpUnit\Contract\Command\StrictCoverageCommand;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class PrePushToolHandler implements CommandHandlerInterface
21
{
22
    const PRE_PUSH_HOOK = '<comment>Pre-push hook</comment>';
23
    /**
24
     * @var QueryBus
25
     */
26
    private $queryBus;
27
    /**
28
     * @var PrePushOriginalExecutorInterface
29
     */
30
    private $prePushOriginalExecutor;
31
    /**
32
     * @var OutputInterface
33
     */
34
    private $output;
35
    /**
36
     * @var CommandBus
37
     */
38
    private $commandBus;
39
40
    /**
41
     * PrePushTool constructor.
42
     *
43
     * @param QueryBus                         $queryBus
44
     * @param PrePushOriginalExecutorInterface $prePushOriginalExecutor
45
     * @param OutputInterface                  $output
46
     * @param CommandBus                       $commandBus
47
     */
48 3
    public function __construct(
49
        QueryBus $queryBus,
50
        PrePushOriginalExecutorInterface $prePushOriginalExecutor,
51
        OutputInterface $output,
52
        CommandBus $commandBus
53
    ) {
54 3
        $this->queryBus = $queryBus;
55 3
        $this->prePushOriginalExecutor = $prePushOriginalExecutor;
56 3
        $this->output = $output;
57 3
        $this->commandBus = $commandBus;
58 3
    }
59
60
    /**
61
     * @param string $remote
62
     * @param string $url
63
     *
64
     * @throws InvalidPushException
65
     */
66 3
    private function execute($remote, $url)
67
    {
68
        /** @var ConfigurationDataResponse $configurationData */
69 3
        $configurationData = $this->queryBus->handle(new ConfigurationDataFinder());
70 3
        $prePushResponse = $configurationData->getPrePush();
71
72 3
        if (true === $prePushResponse->isPrePush()) {
73 2
            $this->output->writeln(self::PRE_PUSH_HOOK);
74 2
            $this->executeOriginalHook($remote, $url, $prePushResponse->getErrorMessage());
75
76 1
            $phpunitResponse = $prePushResponse->getPhpUnit();
77
78 1 View Code Duplication
            if (true === $phpunitResponse->isPhpunit()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79 1
                $this->commandBus->handle(
80 1
                    new PhpUnitToolCommand(
81 1
                        $phpunitResponse->isPhpunitRandomMode(),
82 1
                        $phpunitResponse->getPhpunitOptions(),
83 1
                        $prePushResponse->getErrorMessage()
84
                    )
85
                );
86
87 1
                $phpunitStrictCoverageResponse = $prePushResponse->getPhpUnitStrictCoverage();
88
89 1
                if (true === $phpunitStrictCoverageResponse->isPhpunitStrictCoverage()) {
90 1
                    $this->commandBus->handle(
91 1
                        new StrictCoverageCommand(
92 1
                            $phpunitStrictCoverageResponse->getMinimum(),
93 1
                            $prePushResponse->getErrorMessage()
94
                        )
95
                    );
96
                }
97
98 1
                $phpunitGuardCoverageResponse = $prePushResponse->getPhpUnitGuardCoverage();
99
100 1
                if (true === $phpunitGuardCoverageResponse->isEnabled()) {
101 1
                    $this->commandBus->handle(
102 1
                        new GuardCoverageCommand($phpunitGuardCoverageResponse->getWarningMessage())
103
                    );
104
                }
105
            }
106
107 1
            $this->output->writeln(GoodJobLogoResponse::paint($prePushResponse->getRightMessage()));
108
        }
109 2
    }
110
111
    /**
112
     * @param string $remote
113
     * @param string $url
114
     * @param string $errorMessage
115
     *
116
     * @throws InvalidPushException
117
     */
118 2
    private function executeOriginalHook($remote, $url, $errorMessage)
119
    {
120 2
        $response = $this->prePushOriginalExecutor->execute($remote, $url);
121
122 2
        if (null != $response) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $response of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
123 1
            $this->output->writeln(BadJobLogoResponse::paint($errorMessage));
124
125 1
            throw new InvalidPushException();
126
        }
127 1
    }
128
129
    /**
130
     * @param CommandInterface|PrePushTool $command
131
     */
132 3
    public function handle(CommandInterface $command)
133
    {
134 3
        $this->execute($command->getRemote(), $command->getUrl());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Bruli\EventBusBundle\CommandBus\CommandInterface as the method getRemote() does only exist in the following implementations of said interface: PhpGitHooks\Module\Git\C...act\Command\PrePushTool.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
Bug introduced by
It seems like you code against a concrete implementation and not the interface Bruli\EventBusBundle\CommandBus\CommandInterface as the method getUrl() does only exist in the following implementations of said interface: PhpGitHooks\Module\Git\C...act\Command\PrePushTool.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
135 2
    }
136
}
137