TvReleaseContext::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace App\Services\TvProcessing;
4
5
/**
6
 * Value object containing release information for TV processing.
7
 */
8
class TvReleaseContext
9
{
10
    public function __construct(
11
        public readonly int $releaseId,
12
        public readonly string $searchName,
13
        public readonly int $groupsId,
14
        public readonly int $categoriesId,
15
        public readonly int $videosId = 0,
16
        public readonly int $tvEpisodesId = 0,
17
        public readonly ?string $guid = null,
18
        public readonly ?string $leftGuid = null,
19
    ) {}
20
21
    /**
22
     * Create from a Release model array/object.
23
     */
24
    public static function fromRelease(array|object $release): self
25
    {
26
        // Handle Eloquent models properly - use toArray() instead of casting
27
        if ($release instanceof \Illuminate\Database\Eloquent\Model) {
0 ignored issues
show
introduced by
$release is never a sub-type of Illuminate\Database\Eloquent\Model.
Loading history...
28
            $data = $release->toArray();
29
        } else {
30
            $data = is_array($release) ? $release : (array) $release;
0 ignored issues
show
introduced by
The condition is_array($release) is always true.
Loading history...
31
        }
32
33
        return new self(
34
            releaseId: (int) ($data['id'] ?? 0),
35
            searchName: $data['searchname'] ?? '',
36
            groupsId: (int) ($data['groups_id'] ?? 0),
37
            categoriesId: (int) ($data['categories_id'] ?? 0),
38
            videosId: (int) ($data['videos_id'] ?? 0),
39
            tvEpisodesId: (int) ($data['tv_episodes_id'] ?? 0),
40
            guid: $data['guid'] ?? null,
41
            leftGuid: $data['leftguid'] ?? null,
42
        );
43
    }
44
45
    /**
46
     * Check if the release name matches a pattern.
47
     */
48
    public function matchesPattern(string $pattern): bool
49
    {
50
        return (bool) preg_match($pattern, $this->searchName);
51
    }
52
}
53
54