Passed
Push — nln-php7 ( 5c4f20...6ce259 )
by Nicolas
03:34
created

Hydrate   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 53.57 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 30
loc 56
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getProcessor() 0 4 1
A launchConfigurationAction() 0 6 1
A warnForUnusedVariables() 16 16 2
A warnForUnvaluedVariables() 14 14 2

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
declare(strict_types = 1);
4
5
namespace Karma\Console;
6
7
use Karma\ConfigurableProcessor;
8
use Karma\Application;
9
use Karma\Hydrator;
10
11
class Hydrate extends ConfigureActionCommand
12
{
13 36
    public function __construct(Application $app)
14
    {
15 36
        parent::__construct(
16 36
            $app,
17 36
            'hydrate',
18 36
            'Hydrate dist files',
19 36
            'Hydrate'
20
        );
21 36
    }
22
23 10
    protected function getProcessor(): ConfigurableProcessor
24
    {
25 10
        return $this->app['hydrator'];
26
    }
27
28 10
    protected function launchConfigurationAction(ConfigurableProcessor $processor): void
29
    {
30 10
        $processor->hydrate($this->environment);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Karma\ConfigurableProcessor as the method hydrate() does only exist in the following implementations of said interface: Karma\Hydrator.

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...
31 10
        $this->warnForUnusedVariables($processor);
0 ignored issues
show
Compatibility introduced by
$processor of type object<Karma\ConfigurableProcessor> is not a sub-type of object<Karma\Hydrator>. It seems like you assume a concrete implementation of the interface Karma\ConfigurableProcessor to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
32 10
        $this->warnForUnvaluedVariables($processor);
0 ignored issues
show
Compatibility introduced by
$processor of type object<Karma\ConfigurableProcessor> is not a sub-type of object<Karma\Hydrator>. It seems like you assume a concrete implementation of the interface Karma\ConfigurableProcessor to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
33 10
    }
34
35 10 View Code Duplication
    private function warnForUnusedVariables(Hydrator $processor)
0 ignored issues
show
Duplication introduced by
This method 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...
36
    {
37 10
        $unusedVariables = $processor->getUnusedVariables();
38
39 10
        if(! empty($unusedVariables))
40
        {
41 8
            $logger = $this->app['logger'];
42
43 8
            $logger->warning('You have unused variables : you should remove them or check if you have not mispelled them');
44
45 8
            $logger->warning(sprintf(
46 8
                'Unused variables : %s',
47 8
                implode(', ', $unusedVariables)
48
            ));
49
        }
50 10
    }
51
52 10 View Code Duplication
    private function warnForUnvaluedVariables(Hydrator $processor): void
0 ignored issues
show
Duplication introduced by
This method 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...
53
    {
54 10
        $unvaluedVariables = $processor->getUnvaluedVariables();
55
56 10
        if(! empty($unvaluedVariables))
57
        {
58 1
            $logger = $this->app['logger'];
59
60 1
            $logger->warning(sprintf(
61 1
                'Missing values for variables : %s (TODO markers found)',
62 1
                implode(', ', $unvaluedVariables)
63
            ));
64
        }
65 10
    }
66
}
67