Completed
Push — master ( 5b5e93...8b1958 )
by Vitaly
02:33
created

IfGenerator::code()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
cc 6
eloc 12
nc 16
nop 1
1
<?php declare(strict_types=1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 03.04.17 at 08:57
5
 */
6
7
namespace samsonframework\generator;
8
9
/**
10
 * Class IfGenerator
11
 *
12
 * @author Vitaly Egorov <[email protected]>
13
 */
14
class IfGenerator extends AbstractGenerator
15
{
16
    use CodeTrait;
17
18
    /** @var bool Flag that condition is first */
19
    protected $isFirstCondition = true;
20
21
    /**
22
     * @inheritdoc
23
     */
24
    public function defCondition(string $definition = ''): ConditionGenerator
25
    {
26
        $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...
27
            ->setIndentation($this->indentation)
28
            ->increaseIndentation()
29
            ->defDefinition($definition);
30
31
        // Change flag
32
        $this->isFirstCondition = false;
33
34
        return $conditionGenerator;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function code(int $indentation = 0): string
41
    {
42
        $code = '';
43
44
        // Add child if groups code
45
        if (array_key_exists(self::class, $this->generatedCode)) {
46
            $code .= $this->generatedCode[self::class];
47
        }
48
49
        // Add conditions code
50
        if (array_key_exists(ConditionGenerator::class, $this->generatedCode)) {
51
            $code .= $this->generatedCode[ConditionGenerator::class];
52
        }
53
54
        // Close condition block
55
        if ($code !== '') {
56
            $code .= "\n".'}';
57
        }
58
59
        // Separate code in lines and add to parent code
60
        if ($code !== '') {
61
            foreach (explode("\n", $code) as $codeLine) {
62
                $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\generato...nctionCommentsGenerator, 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...
63
            }
64
        }
65
66
        return $code;
67
    }
68
}
69