Completed
Push — master ( 9ad54b...895d73 )
by Federico
03:21
created

CodeVisitor::visitCodeConditional()   D

Complexity

Conditions 9
Paths 12

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 9
eloc 20
c 1
b 0
f 1
nc 12
nop 1
dl 0
loc 33
rs 4.909
1
<?php
2
3
namespace Jade\Compiler;
4
5
use Jade\Nodes\Code;
6
7
abstract class CodeVisitor extends TagVisitor
8
{
9
    /**
10
     * @param Nodes\Code $node
0 ignored issues
show
Bug introduced by
There is no parameter named $node. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
11
     */
12
    protected function visitCodeConditional(array $matches)
13
    {
14
        $code = trim($matches[2], '; ');
15
        while (($len = strlen($code)) > 1 && ($code[0] === '(' || $code[0] === '{') && ord($code[0]) === ord(substr($code, -1)) - 1) {
16
            $code = trim(substr($code, 1, $len - 2));
17
        }
18
19
        $index = count($this->buffer) - 1;
0 ignored issues
show
Bug introduced by
The property buffer does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
        $conditional = '';
21
22
        if (isset($this->buffer[$index]) && false !== strpos($this->buffer[$index], $this->createCode('}'))) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Jade\Compiler\CodeVisitor as the method createCode() does only exist in the following sub-classes of Jade\Compiler\CodeVisitor: Jade\Compiler, Pug\Compiler. 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...
23
            // the "else" statement needs to be in the php block that closes the if
24
            $this->buffer[$index] = null;
25
            $conditional .= '} ';
26
        }
27
28
        $conditional .= '%s';
29
30
        if (strlen($code) > 0) {
31
            $conditional .= '(%s) {';
32
            $conditional = $matches[1] === 'unless'
33
                ? sprintf($conditional, 'if', '!(%s)')
34
                : sprintf($conditional, $matches[1], '%s');
35
            $this->buffer($this->createCode($conditional, $code));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Jade\Compiler\CodeVisitor as the method createCode() does only exist in the following sub-classes of Jade\Compiler\CodeVisitor: Jade\Compiler, Pug\Compiler. 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...
Bug introduced by
The method buffer() does not exist on Jade\Compiler\CodeVisitor. Did you maybe mean bufferCustomKeyword()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
36
37
            return;
38
        }
39
40
        $conditional .= ' {';
41
        $conditional = sprintf($conditional, $matches[1]);
42
43
        $this->buffer($this->createCode($conditional));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Jade\Compiler\CodeVisitor as the method createCode() does only exist in the following sub-classes of Jade\Compiler\CodeVisitor: Jade\Compiler, Pug\Compiler. 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...
Bug introduced by
The method buffer() does not exist on Jade\Compiler\CodeVisitor. Did you maybe mean bufferCustomKeyword()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
44
    }
45
46
    /**
47
     * @param Nodes\Code $node
48
     */
49
    protected function visitCodeOpening(Code $node)
50
    {
51
        $code = trim($node->value);
52
53
        if ($node->buffer) {
54
            $this->buffer($this->escapeIfNeeded($node->escape, $code));
0 ignored issues
show
Bug introduced by
The method buffer() does not exist on Jade\Compiler\CodeVisitor. Did you maybe mean bufferCustomKeyword()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
55
56
            return;
57
        }
58
59
        $phpOpen = implode('|', $this->phpOpenBlock);
60
61
        if (preg_match("/^[[:space:]]*({$phpOpen})(.*)/", $code, $matches)) {
62
            $this->visitCodeConditional($matches);
63
64
            return;
65
        }
66
67
        $this->buffer($this->createCode('%s', $code));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Jade\Compiler\CodeVisitor as the method createCode() does only exist in the following sub-classes of Jade\Compiler\CodeVisitor: Jade\Compiler, Pug\Compiler. 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...
Bug introduced by
The method buffer() does not exist on Jade\Compiler\CodeVisitor. Did you maybe mean bufferCustomKeyword()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
68
    }
69
70
    /**
71
     * @param Nodes\Code $node
72
     */
73 View Code Duplication
    protected function visitCode(Code $node)
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...
74
    {
75
        $this->visitCodeOpening($node);
76
77
        if (isset($node->block)) {
78
            $this->indents++;
79
            $this->visit($node->block);
0 ignored issues
show
Bug introduced by
The property block does not seem to exist in Jade\Nodes\Code.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
80
            $this->indents--;
81
82
            if (!$node->buffer) {
83
                $this->buffer($this->createCode('}'));
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Jade\Compiler\CodeVisitor as the method createCode() does only exist in the following sub-classes of Jade\Compiler\CodeVisitor: Jade\Compiler, Pug\Compiler. 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...
Bug introduced by
The method buffer() does not exist on Jade\Compiler\CodeVisitor. Did you maybe mean bufferCustomKeyword()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
84
            }
85
        }
86
    }
87
}
88