Converter::converToPostfix()   C
last analyzed

Complexity

Conditions 22
Paths 26

Size

Total Lines 53
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 22.0766

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 53
ccs 35
cts 37
cp 0.9459
rs 6.1683
cc 22
eloc 29
nc 26
nop 1
crap 22.0766

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Converter.php
5
 *
6
 * @date 29.03.2015 0:22:18
7
 * @copyright Sklyarov Alexey <[email protected]>
8
 */
9
10
namespace Sufir\Calc;
11
12
use SplStack;
13
use Sufir\Calc\Token\AbstractToken;
14
15
/**
16
 * Converter
17
 *
18
 * Description of Converter
19
 *
20
 * @author Sklyarov Alexey <[email protected]>
21
 * @package Sufir\Calc
22
 */
23
final class Converter
24
{
25
    /**
26
     * @param AbstractToken[] $tokens
27
     * @return AbstractToken[]
28
     */
29 25
    public function converToPostfix(array $tokens)
30
    {
31 25
        $output = array();
32 25
        $stack = new SplStack;
33
34 25
        foreach ($tokens as $token) {
35 25
            if ($token->isNumber() || $token->isVariable()) {
36 25
                $output[] = $token;
37
38 25
            } elseif ($token->isFunction()) {
39 15
                $stack->push($token);
40
41 25
            } elseif ($token->isBracket() && $token->isOpen()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Sufir\Calc\Token\AbstractToken as the method isOpen() does only exist in the following sub-classes of Sufir\Calc\Token\AbstractToken: Sufir\Calc\Token\BracketToken. 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...
42 18
                $stack->push($token);
43
44 25
            } elseif ($token->isDelimiter()) {
45
46 6
                while (!$stack->top()->isBracket()) {
47
                    $output[] = $stack->pop();
48
                }
49
50 25
            } elseif ($token->isBracket() && $token->isClose()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Sufir\Calc\Token\AbstractToken as the method isClose() does only exist in the following sub-classes of Sufir\Calc\Token\AbstractToken: Sufir\Calc\Token\BracketToken. 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...
51
52 18
                while (!$stack->top()->isBracket()) {
53 7
                    $output[] = $stack->pop();
54 7
                }
55
56 18
                $stack->pop();
57
58 18
                if (!$stack->isEmpty() && $stack->top()->isFunction()) {
59 15
                    $output[] = $stack->pop();
60 15
                }
61
62 25
            } elseif ($token->isOperator()) {
63
                /* @var $token \Sufir\Calc\Token\OperatorToken */
64 21
                while (!$stack->isEmpty() &&
65 21
                    $stack->top()->isOperator() &&
66 17
                    (($token->isLeftAssoc() && $token->getPriority() <= $stack->top()->getPriority()) ||
67 11
                    ($token->isRightAssoc() && $token->getPriority() < $stack->top()->getPriority()))
68 21
                ) {
69 13
                    $output[] = $stack->pop();
70 13
                }
71
72 21
                $stack->push($token);
73 21
            }
74 25
        }
75
76 25
        while (!$stack->isEmpty()) {
77 21
            $output[] = $stack->pop();
78 21
        }
79
80 25
        return $output;
81
    }
82
}
83