Completed
Push — develop ( 0ca1ab...29c245 )
by Nate
03:14
created

TransformerCollectionTrait::setTransformer()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
rs 9.1768
cc 5
nc 7
nop 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/hubspot/license
6
 * @link       https://www.flipboxfactory.com/software/hubspot/
7
 */
8
9
namespace flipbox\hubspot\criteria\traits;
10
11
use flipbox\hubspot\helpers\TransformerHelper;
12
use flipbox\hubspot\transformers\collections\DynamicTransformerCollection;
13
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
trait TransformerCollectionTrait
20
{
21
    /**
22
     * @var TransformerCollectionInterface|null
23
     */
24
    protected $transformer = ['class' => DynamicTransformerCollection::class];
25
26
    /**
27
     * @param $value
28
     * @return $this
29
     */
30
    public function transformer($value)
31
    {
32
        return $this->setTransformer($value);
33
    }
34
35
    /**
36
     * @param $value
37
     * @return $this
38
     */
39
    public function setTransformer($value)
40
    {
41
        if (empty($value)) {
42
            $this->transformer = null;
43
            return $this;
44
        }
45
46
        if (is_string($value)) {
47
            if (TransformerHelper::isTransformerCollectionClass($value)) {
48
                $value = ['class' => $value];
49
            } else {
50
                $value = ['handle' => [$value]];
51
            }
52
        }
53
54
        if (array_key_exists('class', $value)) {
55
            $this->transformer = $value;
56
            return $this;
57
        }
58
59
        TransformerHelper::populateTransformerCollection(
60
            $this->getTransformer(),
1 ignored issue
show
Bug introduced by
It seems like $this->getTransformer() targeting flipbox\hubspot\criteria...Trait::getTransformer() can also be of type object or string; however, flipbox\hubspot\helpers\...TransformerCollection() does only seem to accept null|object<flipbox\hubs...merCollectionInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61
            $value
62
        );
63
64
        return $this;
65
    }
66
67
    /**
68
     * @return TransformerCollectionInterface|null
69
     */
70
    public function getTransformer()
71
    {
72
        if ($this->transformer === false) {
73
            return null;
74
        }
75
76
        if ($this->transformer instanceof TransformerCollectionInterface) {
77
            return $this->transformer;
78
        }
79
80
        // Prevent subsequent resolves (since it already didn't)
81
        if (null === ($this->transformer = TransformerHelper::resolveCollection($this->transformer))) {
1 ignored issue
show
Documentation Bug introduced by
It seems like \flipbox\hubspot\helpers...ion($this->transformer) can also be of type string. However, the property $transformer is declared as type object<flipbox\hubspot\t...llectionInterface>|null. 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...
82
            $this->transformer = false;
1 ignored issue
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type object<flipbox\hubspot\t...llectionInterface>|null of property $transformer.

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...
83
            return null;
84
        }
85
86
        return $this->transformer;
87
    }
88
}
89