Completed
Push — wip-public-release ( 8901b1...a92f7d )
by Bogdan
04:52
created

ParametersList   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 1
dl 0
loc 112
ccs 21
cts 28
cp 0.75
rs 10
c 0
b 0
f 0

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 ParameterSpecInterface[]
41
     */
42
    private $parameters;
43
44
    /**
45
     * @param ParameterSpecInterface[] ...$parameters
46
     */
47 9
    public function __construct(ParameterSpecInterface ...$parameters)
48
    {
49 9
        $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...
50
51 9
        $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...
52 9
        $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...
53 9
        $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...
54 9
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getNumberOfParameters(): int
60
    {
61
        return $this->number_of_parameters;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getNumberOfRequiredParameters(): int
68
    {
69
        return $this->number_of_required_parameters;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function isVariadic(): bool
76
    {
77
        return $this->variadic;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 1
    public function getParameters(): array
84
    {
85 1
        return $this->parameters;
86
    }
87
88
    /**
89
     * @param ParameterSpecInterface[] $parameters
90
     *
91
     * @return int
92
     */
93 9
    protected function getNumberOfParametersFromArray(array $parameters): int
94
    {
95 9
        return count($parameters);
96
    }
97
98
    /**
99
     * @param ParameterSpecInterface[] $parameters
100
     *
101
     * @return int
102
     */
103 9
    protected function getNumberOfRequiredParametersFromArray(array $parameters): int
104
    {
105 9
        $required = 0;
106
107 9
        foreach ($parameters as $p) {
108 1
            if ($p->isOptional() || $p->isVariadic()) {
109
                break;
110
            }
111
112 1
            $required++;
113
        }
114
115 9
        return $required;
116
    }
117
118
    /**
119
     * @param ParameterSpecInterface[] $parameters
120
     *
121
     * @return bool
122
     */
123 9
    protected function getIsVariadicFromArray(array $parameters): bool
124
    {
125 9
        foreach ($parameters as $p) {
126 1
            if ($p->isVariadic()) {
127 1
                return true;
128
            }
129
        }
130
131 9
        return false;
132
    }
133
}
134