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

PhpUnitToolHandler::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
crap 2
1
<?php
2
3
namespace PhpGitHooks\Module\PhpUnit\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\PhpUnit\Contract\Command\PhpUnitTool;
10
use PhpGitHooks\Module\PhpUnit\Contract\Exception\PhpUnitViolationException;
11
use PhpGitHooks\Module\PhpUnit\Model\PhpUnitProcessorInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class PhpUnitToolHandler implements CommandHandlerInterface
15
{
16
    const EXECUTING_MESSAGE = 'Running unit tests';
17
    /**
18
     * @var OutputInterface
19
     */
20
    private $output;
21
    /**
22
     * @var PhpUnitProcessorInterface
23
     */
24
    private $phpUnitProcessor;
25
    /**
26
     * @var PhpUnitProcessorInterface
27
     */
28
    private $phpUnitRandomizerProcessor;
29
30
    /**
31
     * PhpUnitTool constructor.
32
     *
33
     * @param OutputInterface $output
34
     * @param PhpUnitProcessorInterface $phpUnitProcessor
35
     * @param PhpUnitProcessorInterface $phpUnitRandomizerProcessor
36
     */
37 2
    public function __construct(
38
        OutputInterface $output,
39
        PhpUnitProcessorInterface $phpUnitProcessor,
40
        PhpUnitProcessorInterface $phpUnitRandomizerProcessor
41
    ) {
42 2
        $this->output = $output;
43 2
        $this->phpUnitProcessor = $phpUnitProcessor;
44 2
        $this->phpUnitRandomizerProcessor = $phpUnitRandomizerProcessor;
45 2
    }
46
47
    /**
48
     * @param string $randomMode
49
     * @param string $options
50
     * @param string $errorMessage
51
     *
52
     * @throws PhpUnitViolationException
53
     */
54 2
    private function execute($randomMode, $options, $errorMessage)
55
    {
56 2
        $outputMessage = new PreCommitOutputWriter(self::EXECUTING_MESSAGE);
57 2
        $this->output->writeln($outputMessage->getMessage());
58
59 2
        $testResult = $this->executeTool($randomMode, $options);
0 ignored issues
show
Documentation introduced by
$randomMode is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
61 2
        if (false === $testResult) {
62 1
            $this->output->writeln(BadJobLogoResponse::paint($errorMessage));
63
64 1
            throw new PhpUnitViolationException();
65
        }
66 1
    }
67
68
    /**
69
     * @param bool $randomMode
70
     * @param string $options
71
     *
72
     * @return bool
73
     */
74 2
    protected function executeTool($randomMode, $options)
75
    {
76 2
        return true === $randomMode ? $this->phpUnitRandomizerProcessor->process(
77 2
            $options
78 2
        ) : $this->phpUnitProcessor->process($options);
79
    }
80
81
    /**
82
     * @param CommandInterface|PhpUnitTool $command
83
     */
84 2
    public function handle(CommandInterface $command)
85
    {
86 2
        $this->execute(
87 2
            $command->isRandomMode(),
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 isRandomMode() does only exist in the following implementations of said interface: PhpGitHooks\Module\PhpUn...act\Command\PhpUnitTool.

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...
88 2
            $command->getOptions(),
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 getOptions() does only exist in the following implementations of said interface: PhpGitHooks\Module\PhpCs...\Command\PhpCsFixerTool, PhpGitHooks\Module\PhpMd...tract\Command\PhpMdTool, PhpGitHooks\Module\PhpUn...act\Command\PhpUnitTool.

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...
89 2
            $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 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...
90
        );
91 1
    }
92
}
93