Item::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 2
nop 9
dl 0
loc 17
rs 9.2
c 0
b 0
f 0

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
 * Created by PhpStorm.
4
 * User: ninoskopac
5
 * Date: 01/05/2018
6
 * Time: 23:13
7
 */
8
declare(strict_types=1);
9
namespace iTunesPodcastFeed;
10
11
use iTunesPodcastFeed\Interfaces\Item as ItemInterface;
12
use iTunesPodcastFeed\Traits\RssEscape;
13
14
class Item implements ItemInterface
15
{
16
    use RssEscape;
17
18
    /**
19
     * @var string
20
     */
21
    private $title;
22
    /**
23
     * @var string
24
     */
25
    private $fileUrl;
26
    /**
27
     * @var string
28
     */
29
    private $duration;
30
    /**
31
     * @var string
32
     */
33
    private $description;
34
    /**
35
     * @var string
36
     */
37
    private $publishDate;
38
    /**
39
     * @var int
40
     */
41
    private $fileSizeBytes;
42
    /**
43
     * @var string
44
     */
45
    private $mime;
46
    /**
47
     * @var null|string
48
     */
49
    private $guid;
50
    /**
51
     * @var bool|string
52
     */
53
    private $link;
54
55
    private static $template;
56
57
    public function __construct(
58
        string $title, string $fileUrl, string $duration,
59
        string $description, int $publishDate, int $fileSizeBytes,
60
        string $mime, ?string $guid = null, ?string $link = null
61
    ) {
62
        $this->title = $this->escape($title);
63
        $this->fileUrl = $fileUrl;
64
        $this->duration = $duration;
65
        $this->description = $this->escape($description);
66
        $this->publishDate = date('r', $publishDate);
67
        $this->fileSizeBytes = $fileSizeBytes;
68
        $this->mime = $mime;
69
        $this->guid = $guid ?: $this->fileUrl;
70
        $this->link = $link ?: $this->fileUrl;
71
72
        if (empty(self::$template)) // avoid disk IO for every item instance
73
            self::$template = file_get_contents(__DIR__ . '/templates/item.xml');
74
    }
75
76
    public function getXml(): string {
77
        $template = self::$template;
78
79
        foreach (get_object_vars($this) as $propName => $propValue) {
80
            $template = str_replace(sprintf('{{%s}}', $propName), $propValue, $template);
81
        }
82
83
        return $template;
84
    }
85
}