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

PhpLintToolHandler::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 9.2
c 0
b 0
f 0
cc 3
eloc 13
nc 4
nop 2
crap 3
1
<?php
2
3
namespace PhpGitHooks\Module\PhpLint\Contract\Command;
4
5
use Bruli\EventBusBundle\CommandBus\CommandHandlerInterface;
6
use Bruli\EventBusBundle\CommandBus\CommandInterface;
7
use PhpGitHooks\Module\Git\Contract\Response\BadJobLogoResponse;
8
use PhpGitHooks\Module\Git\Service\PreCommitOutputWriter;
9
use PhpGitHooks\Module\PhpLint\Contract\Exception\PhpLintViolationsException;
10
use PhpGitHooks\Module\PhpLint\Model\PhpLintToolProcessorInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class PhpLintToolHandler implements CommandHandlerInterface
14
{
15
    const RUNNING_PHPLINT = 'Running PHPLINT';
16
    /**
17
     * @var PhpLintToolProcessorInterface
18
     */
19
    private $phpLintTool;
20
    /**
21
     * @var OutputInterface
22
     */
23
    private $output;
24
25
    /**
26
     * PhpLintTool constructor.
27
     *
28
     * @param PhpLintToolProcessorInterface $phpLintTool
29
     * @param OutputInterface               $output
30
     */
31 2
    public function __construct(PhpLintToolProcessorInterface $phpLintTool, OutputInterface $output)
32
    {
33 2
        $this->phpLintTool = $phpLintTool;
34 2
        $this->output = $output;
35 2
    }
36
37
    /**
38
     * @param array $files
39
     * @param string $errorMessage
40
     *
41
     * @throws PhpLintViolationsException
42
     */
43 2
    private function execute(array $files, $errorMessage)
44
    {
45 2
        $outputMessage = new PreCommitOutputWriter(self::RUNNING_PHPLINT);
46 2
        $this->output->write($outputMessage->getMessage());
47
48 2
        $errors = [];
49 2
        foreach ($files as $file) {
50 2
            $errors[] = $this->phpLintTool->process($file);
51
        }
52
53 2
        $errors = array_filter($errors);
54
55 2
        if (!empty($errors)) {
56 1
            $this->output->writeln($outputMessage->getFailMessage());
57 1
            $this->output->writeln($outputMessage->setError(implode('', $errors)));
58 1
            $this->output->writeln(BadJobLogoResponse::paint($errorMessage));
59
60 1
            throw new PhpLintViolationsException();
61
        }
62
63 1
        $this->output->writeln($outputMessage->getSuccessfulMessage());
64 1
    }
65
66
    /**
67
     * @param CommandInterface|PhpLintTool $command
68
     */
69 2
    public function handle(CommandInterface $command)
70
    {
71 2
        $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...
72 1
    }
73
}
74