Completed
Push — master ( 390b51...0e9cfd )
by Mario
11:43
created

DailyForecastDenormalizer::denormalizeSingle()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
c 0
b 0
f 0
rs 9.568
cc 3
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Marek\OpenWeatherMap\Denormalizer;
6
7
use Marek\OpenWeatherMap\API\Value\Response\APIResponse;
8
use Marek\OpenWeatherMap\API\Value\Response\Clouds;
9
use Marek\OpenWeatherMap\API\Value\Response\GeographicCoordinates;
10
use Marek\OpenWeatherMap\API\Value\Response\Main;
11
use Marek\OpenWeatherMap\API\Value\Response\Rain;
12
use Marek\OpenWeatherMap\API\Value\Response\Snow;
13
use Marek\OpenWeatherMap\API\Value\Response\Sys;
14
use Marek\OpenWeatherMap\API\Value\Response\Weather\AggregatedWeather;
15
use Marek\OpenWeatherMap\API\Value\Response\Weather\Weather;
16
use Marek\OpenWeatherMap\API\Value\Response\WeatherValue;
17
use Marek\OpenWeatherMap\API\Value\Response\Wind;
18
19 View Code Duplication
class DailyForecastDenormalizer extends AbstractDenormalizer
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
    public function denormalize(array $data, APIResponse $response): APIResponse
22
    {
23
        if ($response instanceof AggregatedWeather) {
24
            return $this->denormalizeMultiple($data, $response);
25
        }
26
27
        return $this->denormalizeSingle($data, $response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Marek\OpenWeather...e\Response\APIResponse> is not a sub-type of object<Marek\OpenWeather...sponse\Weather\Weather>. It seems like you assume a child class of the class Marek\OpenWeatherMap\API...ue\Response\APIResponse to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
28
    }
29
30
    /**
31
     * @param $data
32
     * @param Weather $weather
33
     *
34
     * @return Weather
35
     */
36
    protected function denormalizeSingle(array $data, Weather $weather): Weather
37
    {
38
        $innerWeather = [];
39
        foreach ($data['weather'] as $w) {
40
            $innerWeather[] = $this->denormalizer->denormalize($w, WeatherValue::class);
41
        }
42
43
        $weather->id = $data['id'];
44
        $weather->name = $data['name'];
45
        $weather->visibility = empty($data['visibility']) ?? $data['visibility'];
46
        $weather->coord = $this->getValue('coord', $data, GeographicCoordinates::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getValue('coord',...phicCoordinates::class) can also be of type array. However, the property $coord is declared as type object<Marek\OpenWeather...\GeographicCoordinates>. 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...
47
        $weather->rain = $this->getValue('rain', $data, Rain::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getValue('rain', ...e\Response\Rain::class) can also be of type array. However, the property $rain is declared as type object<Marek\OpenWeather...PI\Value\Response\Rain>. 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...
48
        $weather->snow = $this->getValue('snow', $data, Snow::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getValue('snow', ...e\Response\Snow::class) can also be of type array. However, the property $snow is declared as type object<Marek\OpenWeather...PI\Value\Response\Snow>. 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...
49
        $weather->wind = $this->getValue('wind', $data, Wind::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getValue('wind', ...e\Response\Wind::class) can also be of type array. However, the property $wind is declared as type object<Marek\OpenWeather...PI\Value\Response\Wind>. 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...
50
        $weather->clouds = $this->getValue('clouds', $data, Clouds::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getValue('clouds'...Response\Clouds::class) can also be of type array. However, the property $clouds is declared as type object<Marek\OpenWeather...\Value\Response\Clouds>. 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...
51
        $weather->main = $this->getValue('main', $data, Main::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getValue('main', ...e\Response\Main::class) can also be of type array. However, the property $main is declared as type object<Marek\OpenWeather...PI\Value\Response\Main>. 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...
52
        $weather->sys = $this->getValue('sys', $data, Sys::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getValue('sys', $...ue\Response\Sys::class) can also be of type array. However, the property $sys is declared as type object<Marek\OpenWeather...API\Value\Response\Sys>. 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...
53
        $weather->weather = $innerWeather;
54
        $weather->dt = empty($data['dt']) ? null : new \DateTimeImmutable("@{$data['dt']}");
0 ignored issues
show
Documentation Bug introduced by
It seems like empty($data['dt']) ? nul...table("@{$data['dt']}") of type object<DateTimeImmutable> is incompatible with the declared type object<DateTime> of property $dt.

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...
55
56
        return $weather;
57
    }
58
59
    /**
60
     * @param array $data
61
     * @param AggregatedWeather $weather
62
     *
63
     * @return AggregatedWeather
64
     */
65
    protected function denormalizeMultiple($data, AggregatedWeather $weather): AggregatedWeather
66
    {
67
        $weathers = [];
68
        foreach ($data['list'] as $datum) {
69
            $weathers[] = $this->denormalizeSingle($datum, new Weather());
70
        }
71
72
        $weather->count = empty($data['cnt']) ? $data['count'] : $data['cnt'];
73
        $weather->weathers = $weathers;
74
75
        return $weather;
76
    }
77
}
78