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

CommitMsgHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 8
dl 0
loc 78
rs 10
ccs 22
cts 22
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A run() 0 21 3
A isValidCommitMessage() 0 4 2
A handle() 0 4 1
1
<?php
2
3
namespace PhpGitHooks\Module\Git\Contract\Command;
4
5
use Bruli\EventBusBundle\CommandBus\CommandHandlerInterface;
6
use Bruli\EventBusBundle\CommandBus\CommandInterface;
7
use Bruli\EventBusBundle\QueryBus\QueryBus;
8
use PhpGitHooks\Module\Configuration\Contract\Query\ConfigurationDataFinder;
9
use PhpGitHooks\Module\Configuration\Contract\Response\ConfigurationDataResponse;
10
use PhpGitHooks\Module\Git\Contract\Exception\InvalidCommitMessageException;
11
use PhpGitHooks\Module\Git\Model\CommitMessageFinderInterface;
12
use PhpGitHooks\Module\Git\Model\MergeValidatorInterface;
13
use Symfony\Component\Console\Input\InputInterface;
14
15
class CommitMsgHandler implements CommandHandlerInterface
16
{
17
    /**
18
     * @var MergeValidatorInterface
19
     */
20
    private $mergeValidator;
21
    /**
22
     * @var QueryBus
23
     */
24
    private $queryBus;
25
    /**
26
     * @var CommitMessageFinderInterface
27
     */
28
    private $commitMessageFinder;
29
30
    /**
31
     * CommitMsgTool constructor.
32
     *
33
     * @param MergeValidatorInterface      $mergeValidator
34
     * @param QueryBus                     $queryBus
35
     * @param CommitMessageFinderInterface $commitMessageFinder
36
     */
37 2
    public function __construct(
38
        MergeValidatorInterface $mergeValidator,
39
        QueryBus $queryBus,
40
        CommitMessageFinderInterface $commitMessageFinder
41
    ) {
42 2
        $this->mergeValidator = $mergeValidator;
43 2
        $this->queryBus = $queryBus;
44 2
        $this->commitMessageFinder = $commitMessageFinder;
45 2
    }
46
47
    /**
48
     * @param InputInterface $input
49
     *
50
     * @throws InvalidCommitMessageException
51
     */
52 2
    private function run(InputInterface $input)
53
    {
54
        /** @var ConfigurationDataResponse $configurationDataResponse */
55 2
        $configurationDataResponse = $this->queryBus->handle(new ConfigurationDataFinder());
56
57 2
        if (true === $configurationDataResponse->getCommitMsg()->isCommitMsg()) {
58 1
            $commitContent = $this->commitMessageFinder->find($input->getFirstArgument());
59
60 1
            $regularExpression = $configurationDataResponse->getCommitMsg()->getRegularExpression();
61 1
            $validMessage = $this->isValidCommitMessage($regularExpression, $commitContent);
62
63 1
            if (false === $validMessage) {
64 1
                throw new InvalidCommitMessageException(
65 1
                    sprintf(
66 1
                        'Invalid Commit message: commit message does not match regex %s !',
67 1
                        $regularExpression
68
                    )
69
                );
70
            }
71
        }
72 1
    }
73
74
    /**
75
     * @param string $regularExpression
76
     * @param string $commitMessage
77
     *
78
     * @return bool
79
     */
80 1
    private function isValidCommitMessage($regularExpression, $commitMessage)
81
    {
82 1
        return $this->mergeValidator->isMerge() || preg_match(sprintf('/%s/', $regularExpression), $commitMessage);
83
    }
84
85
    /**
86
     * @param CommandInterface|CommitMsg $command
87
     */
88 2
    public function handle(CommandInterface $command)
89
    {
90 2
        $this->run($command->getInput());
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 getInput() does only exist in the following implementations of said interface: PhpGitHooks\Module\Confi...\ConfigurationProcessor, PhpGitHooks\Module\Git\Contract\Command\CommitMsg.

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...
91 1
    }
92
}
93