Completed
Push — master ( 6551be...435af8 )
by Mario
09:41
created

HourForecastDenormalizer::hydrate()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 11
nop 2
dl 0
loc 36
rs 8.7217
c 0
b 0
f 0
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
class HourForecastDenormalizer extends AbstractDenormalizer
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);
47
        $weather->rain = $this->getValue('rain', $data, Rain::class);
48
        $weather->snow = $this->getValue('snow', $data, Snow::class);
49
        $weather->wind = $this->getValue('wind', $data, Wind::class);
50
        $weather->clouds = $this->getValue('clouds', $data, Clouds::class);
51
        $weather->main = $this->getValue('main', $data, Main::class);
52
        $weather->sys = $this->getValue('sys', $data, Sys::class);
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
57
        return $weather;
58
    }
59
60
    /**
61
     * @param array $data
62
     * @param AggregatedWeather $weather
63
     *
64
     * @return AggregatedWeather
65
     */
66
    protected function denormalizeMultiple($data, AggregatedWeather $weather): AggregatedWeather
67
    {
68
        $weathers = [];
69
        foreach ($data['list'] as $datum) {
70
            $weathers[] = $this->denormalizeSingle($datum, new Weather());
71
        }
72
73
        $weather->count = empty($data['cnt']) ? $data['count'] : $data['cnt'];
74
        $weather->weathers = $weathers;
75
76
        return $weather;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function hydrate($data, APIResponse $response)
83
    {
84
        if (!$response instanceof AggregatedHourForecast) {
0 ignored issues
show
Bug introduced by
The class Marek\OpenWeatherMap\Den...\AggregatedHourForecast does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
85
            return $response;
86
        }
87
88
        if (is_string($data)) {
89
            $data = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
90
        }
91
92
        $hourForecasts = [];
93
        foreach ($data['list'] as $item) {
94
            $innerWeather = [];
95
            foreach ($item['weather'] as $w) {
96
                $innerWeather[] = $this->hydrator->hydrate($w, new WeatherValue());
0 ignored issues
show
Bug introduced by
The property hydrator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
97
            }
98
99
            $hourForecast = new HourForecast();
100
            $hourForecast->rain = $this->getValue('rain', $item, new Rain());
0 ignored issues
show
Documentation introduced by
new \Marek\OpenWeatherMa...I\Value\Response\Rain() is of type object<Marek\OpenWeather...PI\Value\Response\Rain>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
101
            $hourForecast->snow = $this->getValue('snow', $item, new Snow());
0 ignored issues
show
Documentation introduced by
new \Marek\OpenWeatherMa...I\Value\Response\Snow() is of type object<Marek\OpenWeather...PI\Value\Response\Snow>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
102
            $hourForecast->wind = $this->getValue('wind', $item, new Wind());
0 ignored issues
show
Documentation introduced by
new \Marek\OpenWeatherMa...I\Value\Response\Wind() is of type object<Marek\OpenWeather...PI\Value\Response\Wind>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
            $hourForecast->clouds = $this->getValue('clouds', $item, new Clouds());
0 ignored issues
show
Documentation introduced by
new \Marek\OpenWeatherMa...Value\Response\Clouds() is of type object<Marek\OpenWeather...\Value\Response\Clouds>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
            $hourForecast->main = $this->getValue('main', $item, new Main());
0 ignored issues
show
Documentation introduced by
new \Marek\OpenWeatherMa...I\Value\Response\Main() is of type object<Marek\OpenWeather...PI\Value\Response\Main>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
105
            $hourForecast->weather = $innerWeather;
106
            $hourForecast->sys = $this->getValue('sys', $item, new Sys());
0 ignored issues
show
Documentation introduced by
new \Marek\OpenWeatherMap\API\Value\Response\Sys() is of type object<Marek\OpenWeather...API\Value\Response\Sys>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
            $hourForecast->dt = $this->getDateTimeFromTimestamp('dt', $item);
108
            $hourForecast->dt_txt = empty($data['dt_txt']) ? null : \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $data['dt_txt']);
109
110
            $hourForecasts[] = $hourForecast;
111
        }
112
113
        $response->count = $this->get('cnt', $data);
0 ignored issues
show
Bug introduced by
The method get() does not seem to exist on object<Marek\OpenWeather...urForecastDenormalizer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
114
        $response->list = $hourForecasts;
115
116
        return $response;
117
    }
118
}
119