Passed
Push — master ( f0b0ff...90d824 )
by Bogdan
04:21
created

NativeObjectExtractor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Extractors\PlainExtractors;
17
18
19
use Pinepain\JsSandbox\Extractors\Definition\PlainExtractorDefinitionInterface;
20
use Pinepain\JsSandbox\Extractors\ExtractorException;
21
use Pinepain\JsSandbox\Extractors\ExtractorInterface;
22
use Pinepain\JsSandbox\Extractors\ObjectComponents\ExtractorsObjectStoreInterface;
23
use UnexpectedValueException;
24
use V8\Context;
25
use V8\ObjectValue;
26
use V8\Value;
27
28
29
class NativeObjectExtractor implements PlainExtractorInterface
30
{
31
    /**
32
     * @var ExtractorsObjectStoreInterface
33
     */
34
    private $object_store;
35
36 6
    public function __construct(ExtractorsObjectStoreInterface $object_store)
37
    {
38 6
        $this->object_store = $object_store;
39 6
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 6
    public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor)
45
    {
46 6
        if ($value 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...
47
            try {
48 5
                $instance = $this->object_store->get($value);
49
50 4
                if ($definition->getNext()) {
51 3
                    foreach ($definition->getNext()->getVariations() as $variation) {
52 2
                        if (is_a($instance, $variation->getName())) {
53 2
                            return $instance;
54
                        }
55
                    }
56
57 2
                    throw new ExtractorException('Native object value constraint failed: value is not an instance of given classes/interfaces');
58
                }
59
60 1
                return $instance;
61 3
            } catch (UnexpectedValueException $e) {
62 1
                throw new ExtractorException('Unable to find bound native object');
63
            }
64
        }
65
66 1
        throw new ExtractorException('Value must be of the type object to be able to find instance, ' . $value->typeOf()->value() . ' given');
67
    }
68
}
69