CategoryHydrator::fromStdClass()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Audiens\AdForm\Entity;
4
5
use DateTime;
6
use stdClass;
7
8
/**
9
 * Class CategoryHydrator
10
 */
11
class CategoryHydrator extends Category
12
{
13
    /**
14
     * Hydrate a category from a stdClass, intended to be used for
15
     * instancing a category from \json_decode()
16
     *
17
     * @param stdClass $stdClass
18
     *
19
     * @return Category
20
     */
21
    public static function fromStdClass(stdClass $stdClass): Category
22
    {
23
        $category = new Category();
24
        $category->id = (int)$stdClass->id;
25
        $category->name = $stdClass->name;
26
        $category->dataProviderId = (int)$stdClass->dataProviderId;
27
28
        // might not be set in JSON
29
        if (isset($stdClass->parentId)) {
30
            $category->parentId = (int)$stdClass->parentId;
31
        }
32
33
        $category->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...
34
        $category->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...
35
36
        return $category;
37
    }
38
}
39