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

PhpCsFixerToolHandler::execute()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 16
nop 7
crap 5
1
<?php
2
3
namespace PhpGitHooks\Module\PhpCsFixer\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\PhpCsFixer\Contract\Exception\PhpCsFixerViolationsException;
10
use PhpGitHooks\Module\PhpCsFixer\Model\PhpCsFixerToolProcessorInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class PhpCsFixerToolHandler implements CommandHandlerInterface
14
{
15
    /**
16
     * @var OutputInterface
17
     */
18
    private $output;
19
    /**
20
     * @var PhpCsFixerToolProcessorInterface
21
     */
22
    private $phpCsFixerToolProcessor;
23
24
    /**
25
     * PhpCsFixerTool constructor.
26
     *
27
     * @param OutputInterface $output
28
     * @param PhpCsFixerToolProcessorInterface $phpCsFixerToolProcessor
29
     */
30 2
    public function __construct(OutputInterface $output, PhpCsFixerToolProcessorInterface $phpCsFixerToolProcessor)
31
    {
32 2
        $this->output = $output;
33 2
        $this->phpCsFixerToolProcessor = $phpCsFixerToolProcessor;
34 2
    }
35
36
    /**
37
     * @param array $files
38
     * @param bool $psr0
39
     * @param bool $psr1
40
     * @param bool $psr2
41
     * @param bool $symfony
42
     * @param string $options
43
     * @param string $errorMessage
44
     */
45 2
    private function execute(array $files, $psr0, $psr1, $psr2, $symfony, $options, $errorMessage)
46
    {
47 2
        if (true === $psr0) {
48 2
            $this->executeTool($files, 'PSR0', $options, $errorMessage);
49
        }
50
51 1
        if (true === $psr1) {
52 1
            $this->executeTool($files, 'PSR1', $options, $errorMessage);
53
        }
54
55 1
        if (true === $psr2) {
56 1
            $this->executeTool($files, 'PSR2', $options, $errorMessage);
57
        }
58
59 1
        if (true === $symfony) {
60 1
            $this->executeTool($files, 'SYMFONY', $options, $errorMessage);
61
        }
62 1
    }
63
64
    /**
65
     * @param array $files
66
     * @param string $level
67
     * @param string $options
68
     * @param string $errorMessage
69
     *
70
     * @throws PhpCsFixerViolationsException
71
     */
72 2
    private function executeTool(array $files, $level, $options, $errorMessage)
73
    {
74 2
        $outputMessage = new PreCommitOutputWriter(sprintf('Checking %s code style with PHP-CS-FIXER', $level));
75 2
        $this->output->write($outputMessage->getMessage());
76
77 2
        $errors = [];
78 2
        foreach ($files as $file) {
79 2
            $errors[] = $this->phpCsFixerToolProcessor->process($file, $level, $options);
80
        }
81
82 2
        $errors = array_filter($errors);
83
84 2
        if (!empty($errors)) {
85 1
            $this->output->writeln($outputMessage->getFailMessage());
86 1
            $errorsText = $outputMessage->setError(implode('', $errors));
87 1
            $this->output->writeln($errorsText);
88 1
            $this->output->writeln(BadJobLogoResponse::paint($errorMessage));
89 1
            throw  new PhpCsFixerViolationsException();
90
        }
91
92 1
        $this->output->writeln($outputMessage->getSuccessfulMessage());
93 1
    }
94
95
    /**
96
     * @param CommandInterface|PhpCsFixerTool $command
97
     */
98 2
    public function handle(CommandInterface $command)
99
    {
100 2
        $this->execute(
101 2
            $command->getFiles(),
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...
102 2
            $command->isPsr0(),
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 isPsr0() does only exist in the following implementations of said interface: PhpGitHooks\Module\PhpCs...\Command\PhpCsFixerTool.

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...
103 2
            $command->isPsr1(),
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 isPsr1() does only exist in the following implementations of said interface: PhpGitHooks\Module\PhpCs...\Command\PhpCsFixerTool.

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...
104 2
            $command->isPsr2(),
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 isPsr2() does only exist in the following implementations of said interface: PhpGitHooks\Module\PhpCs...\Command\PhpCsFixerTool.

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...
105 2
            $command->isSymfony(),
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 isSymfony() does only exist in the following implementations of said interface: PhpGitHooks\Module\PhpCs...\Command\PhpCsFixerTool.

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...
106 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...
107 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...
108
        );
109 1
    }
110
}
111