Completed
Push — master ( 8ee32e...efc44e )
by Pablo
02:33
created

PhpFilesExtractorHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 4
dl 44
loc 44
rs 10
ccs 12
cts 12
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 6 6 1
A getPhpFiles() 12 12 3
A handle() 6 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PhpGitHooks\Module\Files\Contract\Query;
4
5
use Bruli\EventBusBundle\QueryBus\QueryHandlerInterface;
6
use Bruli\EventBusBundle\QueryBus\QueryInterface;
7
use PhpGitHooks\Module\Files\Contract\Response\PhpFilesResponse;
8
use PhpGitHooks\Module\Files\Domain\FilesCollection;
9
10 View Code Duplication
class PhpFilesExtractorHandler extends AbstractFilesExtractorQueryHandler implements QueryHandlerInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
{
12
    /**
13
     * @param FilesCollection $filesCollection
14
     *
15
     * @return PhpFilesResponse
16
     */
17 1
    private function extract(FilesCollection $filesCollection)
18
    {
19 1
        $phFiles = $this->getPhpFiles($filesCollection);
20
21 1
        return new PhpFilesResponse($phFiles);
22
    }
23
24
    /**
25
     * @param FilesCollection $filesCollection
26
     *
27
     * @return array
28
     */
29 1
    private function getPhpFiles(FilesCollection $filesCollection)
30
    {
31 1
        $phpFiles = [];
32
33 1
        foreach ($filesCollection->getFiles() as $file) {
34 1
            if (true === (bool) preg_match('/^(.*)(\.php)|(\.inc)$/', $file->value())) {
35 1
                $phpFiles[] = $file->value();
36
            }
37
        }
38
39 1
        return $phpFiles;
40
    }
41
42
    /**
43
     * @param QueryInterface|PhpFilesExtractorQuery $query
44
     *
45
     * @return PhpFilesResponse
46
     */
47 1
    public function handle(QueryInterface $query)
48
    {
49 1
        $files = $this->getFiles($query->getFiles());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Bruli\EventBusBundle\QueryBus\QueryInterface as the method getFiles() does only exist in the following implementations of said interface: PhpGitHooks\Module\Files...\ComposerFilesExtractor, PhpGitHooks\Module\Files...uery\JsonFilesExtractor, PhpGitHooks\Module\Files...Query\PhpFilesExtractor.

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...
50
51 1
        return $this->extract(new FilesCollection($files));
52
    }
53
}
54