Test Failed
Push — feature-laravel-5.4 ( 556e60...89a18e )
by Kirill
03:48
created

AbstractSerializer::formatDateTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of laravel.su package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace App\GraphQL\Serializers;
12
13
use Carbon\Carbon;
14
use Illuminate\Support\Collection;
15
use Illuminate\Database\Eloquent\Model;
16
17
/**
18
 * Class AbstractSerializer.
19
 */
20
abstract class AbstractSerializer
21
{
22
    /**
23
     * @param  Collection|Model[]|null $collection
24
     * @return Collection
25
     */
26
    public static function collection(?Collection $collection): Collection
27
    {
28
        if ($collection === null) {
29
            return new Collection();
30
        }
31
32
        return $collection->map(static::map());
0 ignored issues
show
Bug introduced by
It seems like $collection is not always an object, but can also be of type array<integer,object<Ill...tabase\Eloquent\Model>>. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
33
    }
34
35
    /**
36
     * @param  Model|null $model
37
     * @return array
38
     */
39
    public static function serialize(?Model $model): array
40
    {
41
        if ($model === null) {
42
            return [];
43
        }
44
45
        return app(static::class)->toArray($model);
46
    }
47
48
    /**
49
     * @return \Closure
50
     */
51
    public static function map(): \Closure
52
    {
53
        return function (Model $model) {
54
            return static::serialize($model);
55
        };
56
    }
57
58
    /**
59
     * @param  Model $model
60
     * @return array
61
     */
62
    public function toArray(Model $model): array
63
    {
64
        return $model->toArray();
65
    }
66
67
    /**
68
     * @param \DateTime|null $date
69
     * @return string
70
     */
71
    protected function formatDateTime(?\DateTime $date): string
72
    {
73
        if ($date === null) {
74
            $date = Carbon::now();
75
        }
76
77
        return $date->format(Carbon::RFC3339);
78
    }
79
}
80