DayOf   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 108
Duplicated Lines 14.81 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 10
c 3
b 1
f 0
lcom 1
cbo 0
dl 16
loc 108
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A year() 0 6 1
A week() 0 5 1
C lastDayMonth() 16 35 7

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 OpenCafe\Tools;
4
5
/**
6
 * Define Day of current date.
7
 */
8
class DayOf
9
{
10
	/**
11
     * @var object
12
     */
13
    protected $date_time;
14
15
	/**
16
     * @var array
17
     */
18
    protected $config;
19
20
	/**
21
	 * @var string
22
	 */
23
	protected $calendar_type;
24
25
    /**
26
     * @var int
27
     */
28
    protected $day_of_week;
29
30
	/**
31
   	 * Constructor of DayOf class
32
     */
33
    public function __construct($date_time, $calendar_type = 'gregorian', $day_of_week = null)
34
    {
35
36
		$this->config = include __DIR__.'/CalendarSettings/' . ucfirst($calendar_type) . '.php';
37
38
		$this->calendar_type = $calendar_type;
39
40
        $this->date_time = $date_time;
41
42
        $this->day_of_week = $day_of_week;
43
44
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
45
46
    }
47
48
    /**
49
   * Which day of year is current day.
50
   *
51
   * @since  Aug, 03 2015
52
   * @return integer
53
   */
54
    public function year()
55
    {
56
57
        return $this->config[ 'day_of_year' ]( $this->date_time );
58
59
    }
60
61
62
    /**
63
   * Which day of week is current day.
64
   *
65
   * @since  Aug, 09 2015
66
   * @return integer
67
   */
68
    public function week()
69
    {
70
        return $this->config[ 'day_of_week' ]( $this->date_time, $this->day_of_week );
71
72
    }
73
74
	/**
75
	 * Return last day of current month
76
	 *
77
	 * @since  Oct, 18 2016
78
	 * @return integer
79
	 */
80
	public function lastDayMonth() {
81
82
		$days = 0;
0 ignored issues
show
Unused Code introduced by
$days is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
84
		switch ( $this->calendar_type ) {
85
86 View Code Duplication
			case 'gregorian':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
87
88
				$days = ( intval( $this->date_time->format( 'm' ) ) == 2 &&
89
				      	$this->config[ 'leap_year' ]( $this->date_time->format( 'Y' )) ) ?
90
						$this->config[ 'month_days_number' ][ intval( $this->date_time->format( 'm' ) ) ] + 1 :
91
						$this->config[ 'month_days_number' ][ intval( $this->date_time->format( 'm' ) ) ];
92
93
				break;
94
95 View Code Duplication
			case 'jalali':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
96
97
				$days = ( intval( $this->date_time->format( 'm' ) ) == 12 &&
98
				  		$this->config[ 'leap_year' ]( $this->date_time->format( 'Y' )) ) ?
99
				 	 	$this->config[ 'month_days_number' ][ intval( $this->date_time->format( 'm' ) ) ] + 1 :
100
					 	$this->config[ 'month_days_number' ][ intval( $this->date_time->format( 'm' ) ) ];
101
102
			  	break;
103
104
		  default:
105
106
			$days = $this->config[ 'month_days_number' ][ intval( $this->date_time->format( 'm' ) ) ];
107
108
			break;
109
110
		}
111
112
		return $days;
113
114
	}
115
}
116