SegmentHydrator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 26
c 1
b 0
f 1
dl 0
loc 50
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromStdClass() 0 40 4
1
<?php declare(strict_types=1);
2
3
namespace Audiens\AdForm\Entity;
4
5
use Audiens\AdForm\Enum\SegmentStatus;
6
use stdClass;
7
8
/**
9
 * Class SegmentHydrator
10
 */
11
class SegmentHydrator extends Segment
12
{
13
    /**
14
     * Hydrate a segment from a stdClass, intended to be used for
15
     * instancing a segment from \json_decode()
16
     *
17
     * @param stdClass $stdClass
18
     *
19
     * @return Segment
20
     */
21
    public static function fromStdClass(stdClass $stdClass): Segment
22
    {
23
        $segment = new Segment();
24
25
        $segment->id = (int) $stdClass->id;
26
        $segment->dataProviderId = (int)$stdClass->dataProviderId;
27
        $segment->status = new SegmentStatus($stdClass->status);
28
        $segment->categoryId = (int)$stdClass->categoryId;
29
        $segment->refId = $stdClass->refId;
30
        $segment->fee = (float)$stdClass->fee;
31
        $segment->ttl = (int)$stdClass->ttl;
32
        $segment->name = $stdClass->name;
33
34
        // might not be set in JSON
35
        if (isset($stdClass->formula)) {
36
            $segment->formula = $stdClass->formula;
37
        }
38
39
        // might not be set in JSON
40
        if (isset($stdClass->extractionRule)) {
41
            $segment->extractionRule = $stdClass->extractionRule;
42
        }
43
44
        $segment->audience = (bool)$stdClass->audience;
45
        $segment->audienceBySources = $stdClass->audienceBySources;
46
        $segment->audienceByUserIdentityType = $stdClass->audienceByUserIdentityType;
47
        $segment->isExtended = (bool)$stdClass->isExtended;
48
49
        $segment->frequency = (int)$stdClass->frequency;
50
        $segment->isCrossDevice = (bool)$stdClass->isCrossDevice;
51
        $segment->hasDataUsagePermissions = (bool)$stdClass->hasDataUsagePermissions;
52
53
        $segment->updatedAt = DateParser::parse($stdClass->updatedAt);
0 ignored issues
show
Documentation Bug introduced by
It seems like Audiens\AdForm\Entity\Da...e($stdClass->updatedAt) can also be of type false. However, the property $updatedAt is declared as type DateTime|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...
54
        $segment->createdAt = DateParser::parse($stdClass->createdAt);
0 ignored issues
show
Documentation Bug introduced by
It seems like Audiens\AdForm\Entity\Da...e($stdClass->createdAt) can also be of type false. However, the property $createdAt is declared as type DateTime|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...
55
56
        if (isset($stdClass->unifiedTaxonomyLabelsIds)) {
57
            $segment->unifiedTaxonomyLabelIds = $stdClass->unifiedTaxonomyLabelsIds;
58
        }
59
60
        return $segment;
61
    }
62
}
63