requirement_exit()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the tarantool/client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
use Tarantool\Client\Client;
15
use Tarantool\Client\Connection\StreamConnection;
16
use Tarantool\Client\Handler\DefaultHandler;
17
use Tarantool\Client\Packer\Packer;
18
use Tarantool\Client\Packer\PurePacker;
19
20
return require __DIR__.'/../vendor/autoload.php';
21
22
function create_client(?Packer $packer = null) : Client
23
{
24
    $connection = isset($_SERVER['argv'][1])
25
        ? StreamConnection::create($_SERVER['argv'][1])
26
        : StreamConnection::createTcp();
27
28
    return new Client(new DefaultHandler($connection, $packer ?? PurePacker::fromAvailableExtensions()));
29
}
30
31
function server_version_at_least(string $version, Client $client) : bool
32
{
33
    [$info] = $client->call('box.info');
34
    $actualVersion = preg_replace('/-[^-]+$/', '', $info['version']);
35
36
    return version_compare($actualVersion, $version, '>=');
0 ignored issues
show
Bug Best Practice introduced by
The expression return version_compare($...ersion, $version, '>=') could return the type integer which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
37
}
38
39
function ensure_server_version_at_least(string $version, Client $client) : void
40
{
41
    if (server_version_at_least($version, $client)) {
42
        return;
43
    }
44
45
    /** @psalm-suppress PossiblyUndefinedArrayOffset */
46
    requirement_exit('Tarantool version >= %s is required to run "%s"', $version, $_SERVER['SCRIPT_FILENAME']);
47
}
48
49
function ensure_extension(string $name) : void
50
{
51
    if (extension_loaded($name)) {
52
        return;
53
    }
54
55
    /** @psalm-suppress PossiblyUndefinedArrayOffset */
56
    requirement_exit('PHP extension "%s" is required to run "%s"', $name, $_SERVER['SCRIPT_FILENAME']);
57
}
58
59
function requirement_exit(string $message, ...$args) : void
60
{
61
    echo "Unfulfilled requirement:\n";
62
    echo $args ? sprintf($message, ...$args) : $message, "\n";
63
    exit(1);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
64
}
65