Completed
Push — master ( 6bfcaa...57efb7 )
by Vitaly
02:48
created

CollectionValue   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 0
dl 0
loc 26
ccs 7
cts 7
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 13 6
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 06.08.16 at 13:44
5
 */
6
namespace samsonframework\container\annotation;
7
8
/**
9
 * Generic collection value annotation.
10
 *
11
 * @package samsonframework\container\annotation
12
 */
13
class CollectionValue
14
{
15
    /** @var array Collection of class collection */
16
    public $collection = [];
17
18
    /**
19
     * Scope constructor.
20
     *
21
     * @param array $scopeOrScopes Class collection
22
     *
23
     * @throws \Exception Thrown when neither string nor string[] is passed
24
     */
25 5
    public function __construct($scopeOrScopes)
26
    {
27 5
        if (is_array($scopeOrScopes) && array_key_exists('value', $scopeOrScopes)) {
28 4
            $value = $scopeOrScopes['value'];
29
30 4
            if (!is_array($value) && !is_string($value)) {
31 1
                throw new \InvalidArgumentException('Only string or array is allowed');
32
            }
33
34
            // Always store array
35 3
            $this->collection = is_array($value) ?: [$value];
0 ignored issues
show
Documentation Bug introduced by
It seems like is_array($value) ?: array($value) can also be of type boolean. However, the property $collection is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
36
        }
37 4
    }
38
}
39