Test Failed
Push — master ( ecd78b...d05c81 )
by Kirill
02:43
created

OpenState   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 12 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Backend\State;
11
12
use Railt\Io\Readable;
13
use Railt\Reflection\Document;
14
use Railt\SDL\Exception\VmException;
15
use Railt\SDL\Frontend\IR\JoinedOpcode;
16
use Railt\SDL\Frontend\IR\OpcodeInterface;
17
18
/**
19
 * Class OpenState
20
 */
21
class OpenState extends State
22
{
23
    /**
24
     * @param OpcodeInterface|JoinedOpcode $opcode
25
     * @return mixed|Document
26
     * @throws VmException
27
     */
28
    protected function execute(OpcodeInterface $opcode)
29
    {
30
        /** @var Readable $file */
31
        $file = $opcode->getOperand(0);
32
33
        if (! $file instanceof Readable) {
34
            $error = 'First operand of (#%d) %s should be instance of %s';
35
            throw new VmException($opcode, $error, $opcode->getId(), $opcode->getName(), Readable::class);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\SDL\Frontend\IR\OpcodeInterface as the method getId() does only exist in the following implementations of said interface: Railt\SDL\Frontend\IR\JoinedOpcode.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
36
        }
37
38
        return new Document($this->getReflection(), $file);
0 ignored issues
show
Compatibility introduced by
$this->getReflection() of type object<Railt\Reflection\Contracts\Reflection> is not a sub-type of object<Railt\Reflection\Reflection>. It seems like you assume a concrete implementation of the interface Railt\Reflection\Contracts\Reflection to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
39
    }
40
}
41