Passed
Push — master ( d2ebdc...958928 )
by Bogdan
05:05
created

AssocExtractor::extract()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 20

Duplication

Lines 31
Ratio 86.11 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 31
loc 36
ccs 0
cts 18
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 20
nc 5
nop 4
crap 30
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 V8\Context;
23
use V8\IntegerValue;
24
use V8\ObjectValue;
25
use V8\Value;
26
27
28
class AssocExtractor implements PlainExtractorInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor)
34
    {
35 View Code Duplication
        if ($value instanceof ObjectValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            $own_properties = $value->getOwnPropertyNames($context);
37
38
            $length  = $own_properties->length();
39
            $isolate = $context->getIsolate();
40
41
            $out = [];
42
43
            $next = $definition->getNext();
44
45
            for ($i = 0; $i < $length; $i++) {
46
                /** @var \V8\PrimitiveValue $prop */
47
                $prop = $own_properties->get($context, new IntegerValue($isolate, $i));
48
                $item = $value->get($context, $prop);
49
50
                $prop_name = $prop->value();
51
52
                if ($next) {
53
                    try {
54
                        $out[$prop_name] = $extractor->extract($context, $item, $definition);
55
                    } catch (ExtractorException $e) {
56
                        throw new ExtractorException("Failed to convert assoc item #{$prop_name}: " . $e->getMessage());
57
                    }
58
                } else {
59
                    $out[$prop_name] = $item;
60
                }
61
            }
62
63
            return $out;
64
65
        }
66
67
        throw new ExtractorException('Value must be of object type, ' . $value->typeOf()->value() . ' given instead');
68
    }
69
}
70