Passed
Push — wip-public-release ( a47743...14cdbd )
by Bogdan
02:52
created

ArrayExtractor::extract()   C

Complexity

Conditions 9
Paths 9

Size

Total Lines 61
Code Lines 35

Duplication

Lines 31
Ratio 50.82 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 31
loc 61
ccs 0
cts 31
cp 0
rs 6.7603
c 0
b 0
f 0
cc 9
eloc 35
nc 9
nop 4
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\ArrayObject;
23
use V8\Context;
24
use V8\IntegerValue;
25
use V8\ObjectValue;
26
use V8\Value;
27
28
29
class ArrayExtractor implements PlainExtractorInterface
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor)
35
    {
36
        if ($value instanceof ArrayObject) {
0 ignored issues
show
Bug introduced by
The class V8\ArrayObject 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...
37
            $length  = $value->length();
38
            $isolate = $context->getIsolate();
39
40
            $out = [];
41
42
            $next = $definition->getNext();
43
44
            for ($i = 0; $i < $length; $i++) {
45
                $item = $value->get($context, new IntegerValue($isolate, $i));
46
47
                if ($next) {
48
                    try {
49
                        $out[] = $extractor->extract($context, $item, $definition);
50
                    } catch (ExtractorException $e) {
51
                        throw new ExtractorException("Failed to convert array item #{$i}: " . $e->getMessage());
52
                    }
53
                } else {
54
                    $out[] = $item;
55
                }
56
            }
57
58
            return $out;
59
        }
60
61 View Code Duplication
        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...
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...
62
            $own_properties = $value->getOwnPropertyNames($context);
63
64
            $length  = $own_properties->length();
65
            $isolate = $context->getIsolate();
66
67
            $out = [];
68
69
            $next = $definition->getNext();
70
71
            for ($i = 0; $i < $length; $i++) {
72
                /** @var \V8\PrimitiveValue $prop */
73
                $prop = $own_properties->get($context, new IntegerValue($isolate, $i));
74
                $item = $value->get($context, $prop);
75
76
                $prop_name = $prop->value();
77
78
                if ($next) {
79
                    try {
80
                        $out[$prop_name] = $extractor->extract($context, $item, $definition);
81
                    } catch (ExtractorException $e) {
82
                        throw new ExtractorException("Failed to convert array item #{$prop_name}: " . $e->getMessage());
83
                    }
84
                } else {
85
                    $out[$prop_name] = $item;
86
                }
87
            }
88
89
            return $out;
90
91
        }
92
93
        throw new ExtractorException('Value must be of array type, ' . $value->typeOf()->value() . ' given instead');
94
    }
95
}
96