Test Failed
Push — master ( e081e8...a6938b )
by Kirill
02:31
created

Vm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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;
11
12
use Psr\Log\LoggerAwareInterface;
13
use Psr\Log\LoggerAwareTrait;
14
use Railt\Io\Readable;
15
use Railt\Reflection\Contracts\Document as DocumentInterface;
16
use Railt\Reflection\Contracts\Reflection;
17
use Railt\Reflection\Document;
18
use Railt\SDL\Exception\VmException;
19
use Railt\SDL\Frontend\IR\Opcode;
20
use Railt\SDL\Frontend\IR\OpcodeInterface;
21
22
/**
23
 * Class Vm
24
 */
25
class Vm implements LoggerAwareInterface
26
{
27
    use LoggerAwareTrait;
28
29
    /**
30
     * @var Reflection
31
     */
32
    private $root;
33
34
    /**
35
     * Process constructor.
36
     * @param Reflection $root
37
     */
38
    public function __construct(Reflection $root)
39
    {
40
        $this->root = $root;
41
    }
42
43
    /**
44
     * @param Readable $file
45
     * @param iterable $opcodes
46
     * @return Document|DocumentInterface
47
     * @throws \Railt\Io\Exception\ExternalFileException
48
     */
49
    public function run(Readable $file, iterable $opcodes): Document
0 ignored issues
show
Unused Code introduced by
The parameter $file is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        foreach ($opcodes as $opcode) {
0 ignored issues
show
Unused Code introduced by
This foreach statement is empty and can be removed.

This check looks for foreach loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
52
            //$opcode->exec();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
        }
54
        die(42);
55
56
        if ($result instanceof DocumentInterface) {
0 ignored issues
show
Unused Code introduced by
if ($result instanceof \...{ return $result; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
57
            return $result;
58
        }
59
60
        $message = 'Zero VM Stack index contains a non-compatible with %s value';
61
        throw new VmException($opcode, $message, DocumentInterface::class);
62
    }
63
64
    /**
65
     * @param OpcodeInterface $opcode
66
     * @param mixed $result
67
     */
68
    private function log(OpcodeInterface $opcode, $result): void
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
69
    {
70
        $value = \gettype($result);
71
72
        if (\is_object($result)) {
73
            $value = \get_class($result) . '#' . \spl_object_hash($result);
74
75
            if (\method_exists($result, '__toString')) {
76
                $value = (string)$result;
77
            }
78
        }
79
80
        $message = \sprintf('%4s = %s', '#' . $opcode->getId(), $value);
81
82
        $this->logger->debug($message);
83
    }
84
}
85