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

StrictCoverageToolHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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\Configuration\Domain\MinimumStrictCoverage;
8
use PhpGitHooks\Module\Git\Service\PreCommitOutputWriter;
9
use PhpGitHooks\Module\PhpUnit\Service\StrictCoverageTool;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class StrictCoverageToolHandler implements CommandHandlerInterface
13
{
14
    const EXECUTE_MESSAGE = 'Checking minimum coverage';
15
    /**
16
     * @var OutputInterface
17
     */
18
    private $output;
19
    /**
20
     * @var StrictCoverageTool
21
     */
22
    private $strictCoverageTool;
23
24
    /**
25
     * StrictCoverageToolExecutor constructor.
26
     * @param OutputInterface $output
27
     * @param StrictCoverageTool $strictCoverageTool
28
     */
29 2
    public function __construct(OutputInterface $output, StrictCoverageTool $strictCoverageTool)
30
    {
31 2
        $this->output = $output;
32 2
        $this->strictCoverageTool = $strictCoverageTool;
33 2
    }
34
35
    /**
36
     * @param MinimumStrictCoverage $minimumStrictCoverage
37
     * @param string $errorMessage
38
     */
39 2
    private function execute(MinimumStrictCoverage $minimumStrictCoverage, $errorMessage)
40
    {
41 2
        $outputMessage = new PreCommitOutputWriter(self::EXECUTE_MESSAGE);
42 2
        $this->output->write($outputMessage->getMessage());
43 2
        $currentCoverage = $this->strictCoverageTool->run($minimumStrictCoverage, $errorMessage);
44 1
        $this->output->writeln($outputMessage->getSuccessfulMessage() . $this->printCurrentCoverage($currentCoverage));
45 1
    }
46
47
    /**
48
     * @param string $currentCoverage
49
     * @return string
50
     */
51 1
    private function printCurrentCoverage($currentCoverage)
52
    {
53 1
        return ' <comment>[' . round($currentCoverage, 0) . '%]</comment>';
54
    }
55
56
    /**
57
     * @param CommandInterface|StrictCoverage $command
58
     */
59 2
    public function handle(CommandInterface $command)
60
    {
61 2
        $this->execute(
62 2
            new MinimumStrictCoverage($command->getMinimumCoverage()),
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 getMinimumCoverage() does only exist in the following implementations of said interface: 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...
63 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...
64
        );
65 1
    }
66
}
67