Passed
Push — master ( 97e252...64a533 )
by Petr
13:07
created

Manufacturer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 13
ccs 1
cts 1
cp 1
crap 1
rs 10

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 kalanis\simple_vin\Support;
4
5
6
final class Manufacturer implements \Stringable
7
{
8 8
    public function __construct(
9
        public readonly string $code,
10
        public readonly string $brand,
11
        public readonly string $types = '',
12
        public readonly string $model = '',
13
        public readonly string $manufacturer = '',
14
        public readonly string $country = '',
15
        public readonly ?int   $from = null,
16
        public readonly ?int   $to = null,
17
        public readonly string $notes = '',
18
        public readonly bool   $canCompareDates = false,
19
    )
20
    {
21 8
    }
22
23 1
    public function desc(): string
24
    {
25 1
        return
26 1
            ($this->manufacturer ? $this->manufacturer . ' - ' : '')
27 1
            . $this->brand . ' ' . $this->model
28 1
            . ($this->types ? (' ' . $this->types) : '')
29 1
            . (($this->from || $this->to) ? (' (' . ($this->from ?: '') . '-' . ($this->to ?: '') . ')') : '')
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->from of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $this->to of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
30 1
            . ($this->country ? ', ' . $this->country : '');
31
    }
32
33 1
    public function __toString(): string
34
    {
35 1
        return $this->desc();
36
    }
37
}
38