Completed
Push — master ( 6d2eba...704618 )
by Enrico
13:50
created

OptionalParamBeforeRequired::pass()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
rs 9.2
1
<?php
2
3
/**
4
 * @author Medvedev Alexandr https://github.com/lexty <[email protected]>
5
 */
6
namespace PHPSA\Analyzer\Pass\Statement;
7
8
use PhpParser\Node\Stmt;
9
use PHPSA\Analyzer\Pass;
10
use PHPSA\Context;
11
12
class OptionalParamBeforeRequired implements Pass\AnalyzerPassInterface
13
{
14
    /**
15
     * @param Stmt $func
16
     * @param Context $context
17
     * @return bool
18
     */
19
    public function pass(Stmt $func, Context $context)
20
    {
21
        $prevIsOptional = false;
22
        foreach ($func->getParams() as $param) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class PhpParser\Node\Stmt as the method getParams() does only exist in the following sub-classes of PhpParser\Node\Stmt: PhpParser\Node\Stmt\ClassMethod, PhpParser\Node\Stmt\Function_. 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
            if ($prevIsOptional && $param->default === null) {
24
                $context->notice('optional-param-before-required', 'Optional parameter before required one is always required.', $func);
25
                return true;
26
            }
27
            $prevIsOptional = $param->default !== null;
28
        }
29
30
        return false;
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    public function getRegister()
37
    {
38
        return [
39
            Stmt\Function_::class,
40
            Stmt\ClassMethod::class,
41
        ];
42
    }
43
}
44