Completed
Push — master ( 6daa93...1805b3 )
by Vitaly
04:00
created

IfGenerator::code()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 9

Duplication

Lines 3
Ratio 13.64 %

Importance

Changes 0
Metric Value
dl 3
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 9
nc 4
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
            ->defDefinition($definition);
29
30
        // Change flag
31
        $this->isFirstCondition = false;
32
33
        return $conditionGenerator;
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39
    public function code(int $indentation = 0): string
40
    {
41
        // Close condition statement
42
        $this->code[] = $this->indentation($indentation) . '}';
43
44
        $code = implode("\n" . $this->indentation($indentation), $this->code);
45
46
        // Add conditions code
47
        if (array_key_exists(ConditionGenerator::class, $this->generatedCode)) {
48
            $code = $this->generatedCode[ConditionGenerator::class] . "\n" . $code;
49
        }
50
51
        // Add comments
52 View Code Duplication
        if (array_key_exists(FunctionCommentsGenerator::class, $this->generatedCode)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
            $code = $this->generatedCode[FunctionCommentsGenerator::class] . "\n" . $code;
54
        }
55
56
        // Pass code to parent to preserve order
57
        $this->parent->defLine($code);
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...
58
59
        return $code;
60
    }
61
}
62