ExtendedFlightInfo   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 124
Duplicated Lines 5.65 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 7
loc 124
rs 10
c 0
b 0
f 0
ccs 32
cts 32
cp 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A setAircraftType() 0 6 1
A isValidTravelClass() 7 7 2
A getAllowedTravelClass() 0 8 1
A toArray() 0 15 1
A create() 0 19 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Model\Message\Attachment\Template\Airline;
6
7
use Kerox\Messenger\Exception\InvalidKeyException;
8
9
class ExtendedFlightInfo extends AbstractFlightInfo implements TravelClassInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    protected $connectionId;
15
16
    /**
17
     * @var string
18
     */
19
    protected $segmentId;
20
21
    /**
22
     * @var string
23
     */
24
    protected $aircraftType;
25
26
    /**
27
     * @var string
28
     */
29
    protected $travelClass;
30
31
    /**
32
     * ExtendedFlightInfo constructor.
33
     *
34
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\Airport        $departureAirport
35
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\Airport        $arrivalAirport
36
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightSchedule $flightSchedule
37
     *
38
     * @throws \Kerox\Messenger\Exception\MessengerException
39
     */
40 2
    public function __construct(
41
        string $connectionId,
42
        string $segmentId,
43
        string $flightNumber,
44
        Airport $departureAirport,
45
        Airport $arrivalAirport,
46
        FlightSchedule $flightSchedule,
47
        string $travelClass
48
    ) {
49 2
        parent::__construct($flightNumber, $departureAirport, $arrivalAirport, $flightSchedule);
50
51 2
        $this->isValidTravelClass($travelClass);
52
53 1
        $this->connectionId = $connectionId;
54 1
        $this->segmentId = $segmentId;
55 1
        $this->travelClass = $travelClass;
56 1
    }
57
58
    /**
59
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\Airport        $departureAirport
60
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\Airport        $arrivalAirport
61
     * @param \Kerox\Messenger\Model\Message\Attachment\Template\Airline\FlightSchedule $flightSchedule
62
     *
63
     * @throws \Kerox\Messenger\Exception\MessengerException
64
     *
65
     * @return \Kerox\Messenger\Model\Message\Attachment\Template\Airline\ExtendedFlightInfo
66
     */
67 2
    public static function create(
68
        string $connectionId,
69
        string $segmentId,
70
        string $flightNumber,
71
        Airport $departureAirport,
72
        Airport $arrivalAirport,
73
        FlightSchedule $flightSchedule,
74
        string $travelClass
75
    ): self {
76 2
        return new self(
77 2
            $connectionId,
78
            $segmentId,
79
            $flightNumber,
80
            $departureAirport,
81
            $arrivalAirport,
82
            $flightSchedule,
83
            $travelClass
84
        );
85
    }
86
87
    /**
88
     * @return ExtendedFlightInfo
89
     */
90 1
    public function setAircraftType(string $aircraftType): self
91
    {
92 1
        $this->aircraftType = $aircraftType;
93
94 1
        return $this;
95
    }
96
97
    /**
98
     * @throws \Kerox\Messenger\Exception\MessengerException
99
     */
100 2 View Code Duplication
    public function isValidTravelClass(string $travelClass): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102 2
        $allowedTravelClass = $this->getAllowedTravelClass();
103 2
        if (!\in_array($travelClass, $allowedTravelClass, true)) {
104 1
            throw new InvalidKeyException(sprintf('travelClass must be either "%s".', implode(', ', $allowedTravelClass)));
105
        }
106 1
    }
107
108 2
    public function getAllowedTravelClass(): array
109
    {
110
        return [
111 2
            self::ECONOMY,
112 2
            self::BUSINESS,
113 2
            self::FIRST_CLASS,
114
        ];
115
    }
116
117 1
    public function toArray(): array
118
    {
119
        $array = [
120 1
            'connection_id' => $this->connectionId,
121 1
            'segment_id' => $this->segmentId,
122 1
            'flight_number' => $this->flightNumber,
123 1
            'aircraft_type' => $this->aircraftType,
124 1
            'travel_class' => $this->travelClass,
125 1
            'departure_airport' => $this->departureAirport,
126 1
            'arrival_airport' => $this->arrivalAirport,
127 1
            'flight_schedule' => $this->flightSchedule,
128
        ];
129
130 1
        return array_filter($array);
131
    }
132
}
133