Completed
Push — master ( 8101fa...e3475b )
by Bogdan
03:03
created

CallbackGuard::guard()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 23
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 25
nc 1
nop 1
crap 56
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the pinepain/js-sandbox PHP library.
5
 *
6
 * Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
7
 *
8
 * Licensed under the MIT license: http://opensource.org/licenses/MIT
9
 *
10
 * For the full copyright and license information, please view the
11
 * LICENSE file that was distributed with this source or visit
12
 * http://opensource.org/licenses/MIT
13
 */
14
15
16
namespace Pinepain\JsSandbox\Wrappers\CallbackGuards;
17
18
19
use Pinepain\JsSandbox\Exceptions\EscapableException;
20
use Pinepain\JsSandbox\Exceptions\NativeException;
21
use Throwable;
22
use V8\CallbackInfoInterface;
23
use V8\ExceptionManager;
24
use V8\Exceptions\TryCatchException;
25
use V8\ObjectValue;
26
use V8\StringValue;
27
28
29
class CallbackGuard implements CallbackGuardInterface
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function guard(callable $callback): callable
35
    {
36
        return function (...$arguments) use ($callback) {
37
            /** @var CallbackInfoInterface $args */
38
            $args = end($arguments); // By convention, CallbackInfoInterface (FunctionCallbackInfo or PropertyCallbackInfo) is the last argument passed to callback
39
40
            assert($args instanceof CallbackInfoInterface);
0 ignored issues
show
Bug introduced by
The class V8\CallbackInfoInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
41
42
            $isolate = $args->getIsolate();
43
            $context = $args->getContext();
44
45
            try {
46
                return $callback(...$arguments);
47
            } catch (TryCatchException $e) {
0 ignored issues
show
Bug introduced by
The class V8\Exceptions\TryCatchException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
48
                $native_exception = $e->getTryCatch()->getException();
49
50
                if (!$native_exception) {
51
                    $message = $this->getMessageFromException($e);
52
                    $isolate->throwException($context, ExceptionManager::createError($context, new StringValue($isolate, $message)), $e);
53
                } elseif ($native_exception instanceof ObjectValue) {
0 ignored issues
show
Bug introduced by
The class V8\ObjectValue does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
54
                    $isolate->throwException($context, $native_exception, $e);
55
                } else {
56
                    // TODO: convert non-object native exception to object and wire with external exception $e ?
57
                    $isolate->throwException($context, $native_exception);
58
                }
59
            } catch (NativeException $e) {
60
                $isolate->throwException($context, ExceptionManager::createError($context, new StringValue($isolate, $e->getMessage())), $e);
61
            } catch (EscapableException $e) {
62
                throw $e->getOriginal();
63
            } catch (Throwable $e) {
64
                // UNEXPECTED
65
                $message = $this->getMessageFromException($e);
66
                $isolate->throwException($context, ExceptionManager::createError($context, new StringValue($isolate, $message)), $e);
67
            }
68
69
            return null;
70
        };
71
    }
72
73
    protected function getMessageFromException(Throwable $e): string
74
    {
75
        return 'An internal exception occurred';
76
    }
77
}
78