Completed
Push — wip-public-release ( 7c11a5...54ec9d )
by Bogdan
05:19
created

ParametersList   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 1
dl 0
loc 114
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getNumberOfParameters() 0 4 1
A getNumberOfRequiredParameters() 0 4 1
A isVariadic() 0 4 1
A getParameters() 0 4 1
A getNumberOfParametersFromArray() 0 4 1
A getNumberOfRequiredParametersFromArray() 0 14 4
A getIsVariadicFromArray() 0 10 3
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the pinepain/php-v8-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\Specs;
17
18
19
use Pinepain\JsSandbox\Specs\Parameters\ParameterSpecInterface;
20
21
22
class ParametersList implements ParametersListInterface
23
{
24
    /**
25
     * @var int
26
     */
27
    private $number_of_parameters = 0;
28
29
    /**
30
     * @var int
31
     */
32
    private $number_of_required_parameters = 0;
33
34
    /**
35
     * @var bool
36
     */
37
    private $variadic = false;
38
39
    /**
40
     * @var Parameters\ParameterSpecInterface[]
41
     */
42
    private $parameters;
43
44
    // TODO: constructor should accept all props by value, not guess them all
45
46
    /**
47
     * @param Parameters\ParameterSpecInterface[] ...$parameters
48
     */
49
    public function __construct(ParameterSpecInterface ...$parameters)
50
    {
51
        $this->parameters = $parameters;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parameters of type array<integer,array<inte...rameterSpecInterface>>> is incompatible with the declared type array<integer,object<Pin...arameterSpecInterface>> of property $parameters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
53
        $this->number_of_parameters          = $this->getNumberOfParametersFromArray($parameters);
0 ignored issues
show
Documentation introduced by
$parameters is of type array<integer,array<inte...rameterSpecInterface>>>, but the function expects a array<integer,object<Pin...arameterSpecInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
        $this->number_of_required_parameters = $this->getNumberOfRequiredParametersFromArray($parameters);
0 ignored issues
show
Documentation introduced by
$parameters is of type array<integer,array<inte...rameterSpecInterface>>>, but the function expects a array<integer,object<Pin...arameterSpecInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
        $this->variadic                      = $this->getIsVariadicFromArray($parameters);
0 ignored issues
show
Documentation introduced by
$parameters is of type array<integer,array<inte...rameterSpecInterface>>>, but the function expects a array<integer,object<Pin...arameterSpecInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getNumberOfParameters(): int
62
    {
63
        return $this->number_of_parameters;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function getNumberOfRequiredParameters(): int
70
    {
71
        return $this->number_of_required_parameters;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function isVariadic(): bool
78
    {
79
        return $this->variadic;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function getParameters(): array
86
    {
87
        return $this->parameters;
88
    }
89
90
    /**
91
     * @param Parameters\ParameterSpecInterface[] ...$parameters
92
     *
93
     * @return int
94
     */
95
    protected function getNumberOfParametersFromArray(array $parameters): int
96
    {
97
        return count($parameters);
98
    }
99
100
    /**
101
     * @param Parameters\ParameterSpecInterface[] ...$parameters
102
     *
103
     * @return int
104
     */
105
    protected function getNumberOfRequiredParametersFromArray(array $parameters): int
106
    {
107
        $required = 0;
108
109
        foreach ($parameters as $p) {
110
            if ($p->isOptional() || $p->isVariadic()) {
111
                break;
112
            }
113
114
            $required++;
115
        }
116
117
        return $required;
118
    }
119
120
    /**
121
     * @param Parameters\ParameterSpecInterface[] ...$parameters
122
     *
123
     * @return bool
124
     */
125
    protected function getIsVariadicFromArray(array $parameters): bool
126
    {
127
        foreach ($parameters as $p) {
128
            if ($p->isVariadic()) {
129
                return true;
130
            }
131
        }
132
133
        return false;
134
    }
135
}
136