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

JsonLintToolHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 56
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A execute() 0 8 2
A jsonFilesExists() 0 4 1
A handle() 0 4 1
1
<?php
2
3
namespace PhpGitHooks\Module\JsonLint\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\Files\Contract\Query\JsonFilesExtractor;
9
use PhpGitHooks\Module\JsonLint\Service\JsonLintToolExecutor;
10
11
class JsonLintToolHandler implements CommandHandlerInterface
12
{
13
    /**
14
     * @var JsonLintToolExecutor
15
     */
16
    private $jsonLintToolExecutor;
17
    /**
18
     * @var QueryBus
19
     */
20
    private $queryBus;
21
22
    /**
23
     * JsonLintTool constructor.
24
     *
25
     * @param JsonLintToolExecutor $jsonLintToolExecutor
26
     * @param QueryBus             $queryBus
27
     */
28 3
    public function __construct(
29
        JsonLintToolExecutor $jsonLintToolExecutor,
30
        QueryBus $queryBus
31
    ) {
32 3
        $this->jsonLintToolExecutor = $jsonLintToolExecutor;
33 3
        $this->queryBus = $queryBus;
34 3
    }
35
36
    /**
37
     * @param array  $files
38
     * @param string $errorMessage
39
     */
40 3
    private function execute(array $files, $errorMessage)
41
    {
42 3
        $jsonFilesResponse = $this->queryBus->handle(new JsonFilesExtractor($files));
43
44 3
        if (true === $this->jsonFilesExists($jsonFilesResponse->getFiles())) {
45 2
            $this->jsonLintToolExecutor->execute($jsonFilesResponse->getFiles(), $errorMessage);
46
        }
47 2
    }
48
49
    /**
50
     * @param array $files
51
     *
52
     * @return bool
53
     */
54 3
    private function jsonFilesExists(array $files)
55
    {
56 3
        return 0 < count($files);
57
    }
58
59
    /**
60
     * @param CommandInterface|JsonLintTool $command
61
     */
62 3
    public function handle(CommandInterface $command)
63
    {
64 3
        $this->execute($command->getFiles(), $command->getErrorMessage());
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 getFiles() does only exist in the following implementations of said interface: PhpGitHooks\Module\Compo...ct\Command\ComposerTool, PhpGitHooks\Module\JsonL...ct\Command\JsonLintTool, PhpGitHooks\Module\PhpCs...\Command\PhpCsFixerTool, PhpGitHooks\Module\PhpCs...tract\Command\PhpCsTool, PhpGitHooks\Module\PhpLi...act\Command\PhpLintTool, PhpGitHooks\Module\PhpMd...tract\Command\PhpMdTool.

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 getErrorMessage() does only exist in the following implementations of said interface: PhpGitHooks\Module\Compo...ct\Command\ComposerTool, PhpGitHooks\Module\JsonL...ct\Command\JsonLintTool, PhpGitHooks\Module\PhpCs...\Command\PhpCsFixerTool, PhpGitHooks\Module\PhpCs...tract\Command\PhpCsTool, PhpGitHooks\Module\PhpLi...act\Command\PhpLintTool, PhpGitHooks\Module\PhpMd...tract\Command\PhpMdTool, PhpGitHooks\Module\PhpUn...act\Command\PhpUnitTool, PhpGitHooks\Module\PhpUn...\Command\StrictCoverage.

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...
65 2
    }
66
}
67