Test Failed
Push — master ( b804f2...c730e0 )
by Vitaly
02:49
created

IfGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 2
cbo 3
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A defCondition() 0 12 1
A code() 0 21 4
1
<?php declare(strict_types=1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 03.04.17 at 08:57
5
 */
6
namespace samsonframework\generator;
7
8
/**
9
 * Class IfGenerator
10
 *
11
 * @author Vitaly Egorov <[email protected]>
12
 */
13
class IfGenerator extends AbstractGenerator
14
{
15
    use CodeTrait;
16
17
    /** @var bool Flag that condition is first */
18
    protected $isFirstCondition = true;
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function defCondition(string $definition = ''): ConditionGenerator
24
    {
25
        $conditionGenerator = (new ConditionGenerator(!$this->isFirstCondition, $this))
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class samsonframework\generator\AbstractGenerator as the method defDefinition() does only exist in the following sub-classes of samsonframework\generator\AbstractGenerator: samsonframework\generator\ConditionGenerator. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
26
            ->setIndentation($this->indentation)
27
            ->increaseIndentation()
28
            ->defDefinition($definition);
29
30
        // Change flag
31
        $this->isFirstCondition = false;
32
33
        return $conditionGenerator;
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function code(): string
40
    {
41
        // Add child if groups code
42
        $code = $this->getNestedCode(self::class);
43
        // Add conditions code
44
        $code .= $this->getNestedCode(ConditionGenerator::class);
45
46
        // Close condition block
47
        if ($code !== '') {
48
            $code .= "\n" . '}';
49
        }
50
51
        // Separate code in lines and add to parent code
52
        if ($code !== '') {
53
            foreach (explode("\n", $code) as $codeLine) {
54
                $this->parent->defLine($codeLine);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class samsonframework\generator\AbstractGenerator as the method defLine() does only exist in the following sub-classes of samsonframework\generator\AbstractGenerator: samsonframework\generator\CommentsGenerator, samsonframework\generator\ConditionGenerator, samsonframework\generator\FunctionGenerator, samsonframework\generator\IfGenerator, samsonframework\generator\MethodGenerator. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
55
            }
56
        }
57
58
        return $code;
59
    }
60
}
61