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

JoinedOpcode   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getOffset() 0 4 1
A getDescription() 0 6 1
A getFile() 0 4 1
A getPosition() 0 4 1
A getLine() 0 4 1
A getColumn() 0 4 1
A getId() 0 4 1
A __toString() 0 9 1
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\Frontend\IR;
11
12
use Railt\Io\PositionInterface;
13
use Railt\Io\Readable;
14
15
/**
16
 * Class JoinableOpcode
17
 */
18
class JoinedOpcode extends Opcode implements PositionInterface
19
{
20
    /**
21
     * @var int
22
     */
23
    private $id;
24
25
    /**
26
     * @var Readable
27
     */
28
    private $file;
29
30
    /**
31
     * @var int
32
     */
33
    private $offset;
34
35
    /**
36
     * @var \Closure
37
     */
38
    private $description;
39
40
    /**
41
     * JoinableOpcode constructor.
42
     * @param OpcodeInterface|self $opcode
43
     * @param int $id
44
     * @param Readable $file
45
     * @param int $offset
46
     */
47
    public function __construct(OpcodeInterface $opcode, int $id, Readable $file, int $offset = 0)
48
    {
49
        parent::__construct($opcode->getOperation(), ...$opcode->operands);
0 ignored issues
show
Bug introduced by
Accessing operands on the interface Railt\SDL\Frontend\IR\OpcodeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
50
51
        $this->id          = $id;
52
        $this->file        = $file;
53
        $this->offset      = $offset;
54
55
        $this->description = function () use ($opcode): string {
56
            return \trim((string)(new \ReflectionObject($opcode))->getDocComment(), " \t\n\r\0\x0B/*");
57
        };
58
    }
59
60
    /**
61
     * @return int
62
     */
63
    public function getOffset(): int
64
    {
65
        return $this->offset;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getDescription(): string
72
    {
73
        return \preg_replace_callback('/\$(\d+)/iu', function (array $m): string {
74
            return $this->operandToString($this->operands[(int)$m[1]] ?? null);
75
        }, ($this->description)());
76
    }
77
78
    /**
79
     * @return Readable
80
     */
81
    public function getFile(): Readable
82
    {
83
        return $this->file;
84
    }
85
86
    /**
87
     * @return PositionInterface
88
     */
89
    private function getPosition(): PositionInterface
90
    {
91
        return $this->file->getPosition($this->offset);
92
    }
93
94
    /**
95
     * @return int
96
     */
97
    public function getLine(): int
98
    {
99
        return $this->getPosition()->getLine();
100
    }
101
102
    /**
103
     * @return int
104
     */
105
    public function getColumn(): int
106
    {
107
        return $this->getPosition()->getColumn();
108
    }
109
110
    /**
111
     * @return int
112
     */
113
    public function getId(): int
114
    {
115
        return $this->id;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function __toString(): string
122
    {
123
        return \vsprintf('%4s | %-80s %s:%d', [
124
            '#' . $this->getId(),
125
            parent::__toString(),
126
            $this->getFile()->getPathname(),
127
            $this->getLine(),
128
        ]);
129
    }
130
}
131