ArrayWrapper::wrapAsArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 3
crap 6
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;
17
18
19
use V8\ArrayObject;
20
use V8\Context;
21
use V8\IntegerValue;
22
use V8\Isolate;
23
use V8\ObjectValue;
24
25
26
class ArrayWrapper implements WrapperInterface, WrapperAwareInterface
27
{
28
    use WrapperAwareTrait;
29
30
    /**
31
     * @param Isolate $isolate
32
     * @param Context $context
33
     * @param array   $value
34
     *
35
     * @return ArrayObject|ObjectValue
36
     * @throws WrapperException
37
     */
38
    public function wrap(Isolate $isolate, Context $context, $value)
39
    {
40
        // NOTE: js arrays, unlike php, will have gaps if indices are not monotonically increasing from 0.
41
        //       Also, non-integer keys does not affect array length as they become properties of array object,
42
        //       so if we have associative array, we will wrap it as object.
43
        if ($this->isAssoc($value)) {
44
            return $this->wrapAsObject($isolate, $context, $value);
45
        } else {
46
            return $this->wrapAsArray($isolate, $context, $value);
47
        }
48
    }
49
50
    private function isAssoc(array $value)
51
    {
52
        if (!$value) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $value of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53
            return false;
54
        }
55
56
        sort($value);
57
58
        return array_keys($value) != range(0, count($value) - 1);
59
    }
60
61 View Code Duplication
    private function wrapAsArray(Isolate $isolate, Context $context, array $value): ArrayObject
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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
    {
63
        $ret = new ArrayObject($context);
64
        // TODO: write test to ensure items have the same order, sort() was breaking that order
65
        $key = 0;
66
        foreach ($value as $val) {
67
            $js_val = $this->wrapper->wrap($isolate, $context, $val);
68
            $js_key = new IntegerValue($isolate, $key++);
69
            $ret->set($context, $js_key, $js_val);
70
        }
71
72
        return $ret;
73
    }
74
75 View Code Duplication
    private function wrapAsObject(Isolate $isolate, Context $context, array $value): ObjectValue
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
76
    {
77
        $ret = new ObjectValue($context);
78
79
        foreach ($value as $key => $val) {
80
            $js_val = $this->wrapper->wrap($isolate, $context, $val);
81
            $js_key = $this->wrapper->wrap($isolate, $context, $key);
82
            $ret->set($context, $js_key, $js_val);
83
        }
84
85
        return $ret;
86
    }
87
}
88