Passed
Push — master ( 57b2d8...55b64c )
by Raffael
22:23 queued 13s
created

Engine::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\V8;
13
14
use Psr\Log\LoggerInterface;
15
use V8Js;
16
17
class Engine extends V8Js
18
{
19
    protected $__result;
20
21
    /**
22
     * Create v8 engine with some default functionality.
23
     */
24 26
    public function __construct(LoggerInterface $logger)
25
    {
26 26
        parent::__construct('core', [], [], true, '');
27 26
        $this->logger = $logger;
0 ignored issues
show
Bug introduced by
The property logger does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28 26
        $this->registerFunctions();
29 26
        $this->setMemoryLimit(50000000);
30 26
    }
31
32
    public function result($result)
33
    {
34
        $this->__result = $result;
35
    }
36
37 2
    public function getLastResult()
38
    {
39 2
        $result = $this->__result;
40 2
        $this->__result = null;
41
42 2
        return $result;
43
    }
44
45
    /**
46
     * Convert to UTF16.
47
     */
48
    public function utf16(string $string): string
49
    {
50
        $len = strlen($string);
51
        $new = '';
52
53
        for ($i = 0; $i < $len; ++$i) {
54
            $new .= "{$string[$i]}\000";
55
        }
56
57
        return $new;
58
    }
59
60
    /**
61
     * Create UUIDv4.
62
     */
63
    public function uuidv4(): string
64
    {
65
        return sprintf(
66
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
67
            // 32 bits for "time_low"
68
            mt_rand(0, 0xffff),
69
            mt_rand(0, 0xffff),
70
71
            // 16 bits for "time_mid"
72
            mt_rand(0, 0xffff),
73
74
            // 16 bits for "time_hi_and_version",
75
            // four most significant bits holds version number 4
76
            mt_rand(0, 0x0fff) | 0x4000,
77
78
            // 16 bits, 8 bits for "clk_seq_hi_res",
79
            // 8 bits for "clk_seq_low",
80
            // two most significant bits holds zero and one for variant DCE1.1
81
            mt_rand(0, 0x3fff) | 0x8000,
82
83
            // 48 bits for "node"
84
            mt_rand(0, 0xffff),
85
            mt_rand(0, 0xffff),
86
            mt_rand(0, 0xffff)
87
        );
88
    }
89
90
    /**
91
     * Register functions.
92
     */
93 26
    protected function registerFunctions(): void
94
    {
95 26
        $this->crypt = [
0 ignored issues
show
Bug introduced by
The property crypt does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
96 26
            'hash' => function ($algo, $data, $raw_output = false) {
97
                return hash($algo, $data, $raw_output);
98 26
            },
99
        ];
100
    }
101
}
102