Completed
Push — main ( bdd72f...bbe282 )
by Niels
15s queued 12s
created

InvalidRequestBodyProblemException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 13
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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the OpenapiBundle package.
7
 *
8
 * (c) Niels Nijens <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Nijens\OpenapiBundle\ExceptionHandling\Exception;
15
16
use Throwable;
17
18
final class InvalidRequestBodyProblemException extends ProblemException
19
{
20
    /**
21
     * @var ViolationInterface[]
22
     */
23
    private $violations;
24
25
    public function __construct(
26
        string $typeUri,
27
        string $title,
28
        int $statusCode,
29
        string $message = '',
30
        Throwable $previous = null,
31
        ?string $instanceUri = null,
32
        array $headers = [],
33
        array $violations = []
34
    ) {
35
        parent::__construct($typeUri, $title, $statusCode, $message, $previous, $instanceUri, $headers);
36
37
        $this->violations = $violations;
38
    }
39
40
    public function getViolations(): array
41
    {
42
        return $this->violations;
43
    }
44
45
    /**
46
     * @param ViolationInterface[] $violations
47
     */
48
    public function withViolations(array $violations): ProblemExceptionInterface
49
    {
50
        $exception = $this->clone();
51
        $exception->violations = $violations;
52
53
        return $exception;
54
    }
55
56
    public function jsonSerialize(): array
57
    {
58
        $data = parent::jsonSerialize();
59
        $data['violations'] = $this->getViolations();
60
61
        return $data;
62
    }
63
64
    protected function clone()
65
    {
66
        $clone = parent::clone();
67
        $clone->violations = $this->violations;
68
69
        return $clone;
70
    }
71
}
72