Completed
Push — master ( 15ded1...7e0974 )
by Ariel
06:13
created

BaseCalendar   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 104
Duplicated Lines 28.85 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 16
c 2
b 0
f 1
lcom 1
cbo 1
dl 30
loc 104
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A timezone() 0 8 2
A forService() 0 6 1
A forDate() 0 6 1
A atTime() 0 10 2
A withDuration() 0 6 1
A getUTCDateTime() 0 4 1
A date() 0 4 1
A find() 18 18 2
A prepare() 12 12 4

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
namespace Timegridio\Concierge\Calendar;
4
5
use Carbon\Carbon;
6
7
abstract class BaseCalendar
8
{
9
    protected $vacancies = [];
10
11
    protected $service = null;
12
13
    protected $duration = null;
14
15
    protected $date = null;
16
17
    protected $time = null;
18
19
    protected $timezone = 'UTC';
20
21
    public function __construct($vacancies, $timezone = 'UTC')
22
    {
23
        $this->vacancies = $vacancies;
24
25
        $this->timezone = $timezone;
26
    }
27
28
    public function timezone($timezone = null)
29
    {
30
        if ($timezone === null) {
31
            return $this->timezone;
32
        }
33
34
        return $this;
35
    }
36
37
    public function forService($service = null)
38
    {
39
        $this->service = $service;
40
41
        return $this;
42
    }
43
44
    public function forDate($date)
45
    {
46
        $this->date = $date;
47
48
        return $this;
49
    }
50
51
    public function atTime($time, $timezone = null)
52
    {
53
        $this->time = $time;
54
55
        if ($timezone !== null) {
56
            $this->timezone = $timezone;
57
        }
58
59
        return $this;
60
    }
61
62
    public function withDuration($duration)
63
    {
64
        $this->duration = $duration;
65
66
        return $this;
67
    }
68
69
    public function getUTCDateTime()
70
    {
71
        return Carbon::parse("{$this->date} {$this->time} {$this->timezone}")->timezone('UTC');
72
    }
73
74
    final protected function date()
75
    {
76
        return Carbon::parse($this->date);
77
    }
78
79 View Code Duplication
    public function find()
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...
80
    {
81
        $this->prepare();
82
83
        $results = $this->vacancies->get();
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->vacancies (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
84
85
        $fromDatetime = $this->getUTCDateTime();
86
87
        if ($this->duration !== null) {
88
            $toDatetime = $fromDatetime->addMinutes($this->duration);
89
90
            $results = $results->reject(function ($vacancy) use ($fromDatetime, $toDatetime) {
91
                return !$vacancy->hasRoomBetween($fromDatetime, $toDatetime);
92
            });
93
        }
94
95
        return $results;
96
    }
97
98 View Code Duplication
    protected function prepare()
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...
99
    {
100
        if ($this->service !== null) {
101
            $this->vacancies->forService($this->service);
0 ignored issues
show
Bug introduced by
The method forService cannot be called on $this->vacancies (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
102
        }
103
104
        if ($this->date !== null &&  $this->time !== null) {
105
            $this->vacancies->forDateTime($this->getUTCDateTime());
0 ignored issues
show
Bug introduced by
The method forDateTime cannot be called on $this->vacancies (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
106
        }
107
108
        return $this;
109
    }
110
}
111