Completed
Push — master ( 5aa9e7...387997 )
by mehdi
03:34
created

Convert   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 340
Duplicated Lines 28.24 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 11
Bugs 1 Features 8
Metric Value
wmc 28
c 11
b 1
f 8
lcom 1
cbo 1
dl 96
loc 340
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A to() 0 7 1
C shamsiToGregorian() 0 59 9
B shamsiToGhamari() 0 75 6
B ghamariToShamsi() 48 48 5
B ghamariToGregorian() 48 48 5

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 namespace Datium\Tools;
2
3
/************************************************************
4
 * Convert Calendars types together
5
 ************************************************************
6
 *
7
 * @since Oct 27, 2015
8
 *
9
 *\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
10
 */
11
class Convert {
12
13
  /**
14
   * @var integer
15
   */
16
  protected $year;
17
18
  /**
19
   * @var integer
20
   */
21
  protected $month;
22
23
  /**
24
   * @var $day
25
   */
26
  protected $day;
27
28
  /**
29
   * @var DateTime
30
   */
31
  public $date_time;
32
33
  /**
34
   * @var array
35
   */
36
  protected $config;
37
38
  /**
39
   * @var integer
40
   */
41
  protected $leap;
42
43
  /**
44
   * @var integer
45
   */
46
  protected $temp_day;
47
48
  /**
49
   * @var DateTime
50
   */
51
  protected $date;
52
53
  /**
54
   * @var array
55
   */
56
  protected $calendar_file;
57
58
  /************************************************************
59
   * Convert class constructor
60
   ************************************************************
61
   *
62
   * @since Oct 27, 2015
63
   *
64
   *\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
65
   */
66
  public function __construct( $date_time = NULL ) {
67
68
    if ( $date_time !== NULL ) {
69
70
        $this->date_time = $date_time;
71
72
    }
73
74
    $this->config = include( 'Config.php' );
75
76
    $this->persian_month = $this->config['month']['persian'];
0 ignored issues
show
Bug introduced by
The property persian_month does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
77
78
  }
79
80
  /************************************************************
81
   * Convert to specific calendar
82
   ************************************************************
83
   *
84
   * @since Oct 26, 2015
85
   *
86
   *\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
87
   */
88
  public function to( $calendar ) {
89
90
    $this->calendar_file = include( 'CalendarSettings/' . ucfirst( $calendar ) . '.php' );
91
92
    return $this->calendar_file[ 'convert_to' ]( $this->date_time );
93
94
  }
95
96
/**
97
   *convert shamsi year to gregorian year
98
   * @since Oct, 16 2015
99
   * @return object
100
   */
101
public function shamsiToGregorian( $date_time ) {
102
103
$this->date_time = $date_time;
104
105
$this->year = $this->date_time->format('Y');
106
107
$this->month = $this->date_time->format('m');
108
109
$this->day = $this->date_time->format('d');
110
111
$days_of_year = 0;
112
113
foreach ( $this->config['shamsi_month_days'] as $month => $value ) {
114
115
  if( $this->month > $month ) $days_of_year += $value;
116
117
}
118
119
$days_of_year += $this->day;
120
121
$days_of_leap_years =  intval( ( ( $this->year - 1 ) / 4 )  );
122
123
$days_of_shamsi_years = ( ( ( $this->year - 1 ) * 365 ) + $days_of_year + $days_of_leap_years );
124
125
$days_of_gregorain_years = $days_of_shamsi_years + 226899;
126
127
if ( $this->month < 10 )  {
128
129
$days_of_gregorain_years = $days_of_gregorain_years - intval( ( ( $this->year + 621 ) / 4 ) );
130
131
}
132
133
elseif ( ( ( 10 == $this->month ) && ( $this->day > 10 ) ) || ( $this->month > 10 ) ) {
134
135
$days_of_gregorain_years = $days_of_gregorain_years - intval( ( ( $this->year + 622 ) / 4 ) );
136
137
}
138
139
$gregorian_month = ( $days_of_gregorain_years % 365 );
140
141
$gregorian_year = intval( $days_of_gregorain_years / 365 ) + 1;
142
143
foreach ($this->config['gregorian_month_days'] as $month => $value) {
144
145
  if ( $gregorian_month < $value ) break;
146
147
    $gregorian_month -= $value;
148
}
149
150
  $gregorian_day = $gregorian_month;
151
152
  $gregorian_month = $month;
0 ignored issues
show
Bug introduced by
The variable $month does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
153
154
  $this->date_time->setDate( $gregorian_year, $gregorian_month, $gregorian_day );
155
156
157
 return $this->date_time;
158
159
}
160
161
/**
162
   *convert shamsi year to ghamari year
163
   * @since Oct, 17 2015
164
   * @return object
165
   */
166
public function shamsiToGhamari( $date_time ) {
167
168
    $this->date_time = $date_time;
169
170
    $this->year = $this->date_time->format('Y');
171
172
    $this->month = $this->date_time->format('n');
173
174
    $this->day = $this->date_time->format('d');
175
176
    $this->temp_day = 0 ;
177
178
    for ( $i = 1 ; $i < $this->month ; $i++ ) {
179
180
        $this->temp_day += $this->config['shamsi_month_days'][$i];
181
182
      }
183
184
     $this->temp_day += $this->day;
185
186
     $this->leap = new Leap( $this->year );
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Datium\Tools\Leap($this->year) of type object<Datium\Tools\Leap> is incompatible with the declared type integer of property $leap.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
187
188
    if( $this->leap->get() && $this->month > 11 ) $this->temp_day++;
189
190
    $_year = ( ( ( ( ( $this->year - 1 ) * 365.2422 ) + $this->temp_day ) - 119) / 354.3670 ) + 1;
191
192
    $_year = explode( '.', $_year );
193
194
    $this->year = $_year[0];
195
196
    $_month = $_year[1];
197
198
     $var_temp = '0.0';
199
200
      for ( $i = strlen( $_month ); $i > 2; $i-- ) {
201
202
        $var_temp .= '0';
203
204
     }
205
206
     $var_temp .= '1';
207
208
    $_month = $_month * $var_temp ;
209
210
    $_month = ( $_month * 12 ) + 1;
211
212
    $_month = explode( '.', $_month );
213
214
    $this->month = $_month[0];
215
216
    $_day = $_month[1];
217
218
    $var_temp = '0.0';
219
220
    for ( $i = strlen( $_day );  $i > 2;  $i-- ) {
221
222
       $var_temp .= '0' ;
223
224
    }
225
226
    $var_temp .= '1';
227
228
    $_day = $_day * $var_temp;
229
230
    $_day = ( $_day * 29.530 );
231
232
    $_day = explode( '.', $_day );
233
234
    $this->day = $_day[0];
235
236
   $this->date_time->setDate( $this->year, $this->month, $this->day );
237
238
   return $this->date_time;
239
240
}
241
242
/**
243
   *convert ghamari year to shamsi year
244
   * @since Oct, 17 2015
245
   * @return object
246
   */
247 View Code Duplication
public function ghamariToShamsi( $date_time ) {
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...
248
249
$this->date_time = $date_time;
250
251
$this->year = $this->date_time->format('Y');
252
253
$this->month = $this->date_time->format('m');
254
255
$this->day = $this->date_time->format('d');
256
257
$days_of_year = 0;
258
259
foreach ( $this->config['islamic_month_days'] as $month => $value ) {
260
261
  if( $this->month > $month ) $days_of_year += $value;
262
263
}
264
265
$days_of_year += $this->day;
266
267
$days_of_leap_years =  intval( ( ( $this->year - 1 ) / 3 )  );
268
269
$days_of_ghamari_years = ( ( ( $this->year - 1 ) * 354 ) + $days_of_year + $days_of_leap_years );
270
271
$days_of_shamsi_years = $days_of_ghamari_years + 179;
272
273
$days_of_shamsi_years = $days_of_shamsi_years - intval( ( ( $this->year - 43 ) / 4 ) );
274
275
$shamsi_month = ( $days_of_shamsi_years % 365 );
276
277
$shamsi_year = intval( $days_of_shamsi_years / 365 ) + 1;
278
279
foreach ($this->config['shamsi_month_days'] as $month => $value) {
280
281
  if ( $shamsi_month < $value ) break;
282
283
    $shamsi_month -= $value;
284
}
285
286
  $shamsi_day = $shamsi_month;
287
288
  $shamsi_month = $month;
0 ignored issues
show
Bug introduced by
The variable $month does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
289
290
  $this->date_time->setDate( $shamsi_year, $shamsi_month, $shamsi_day );
291
292
 return $this->date_time;
293
294
}
295
296
  /**
297
    * convert ghamari year to gregorian year
298
    * @since Oct, 17 2015
299
    * @return object
300
    */
301 View Code Duplication
  public function ghamariToGregorian( $date_time ) {
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...
302
303
    $this->date_time = $date_time;
304
305
    $this->year = $this->date_time->format('Y');
306
307
    $this->month = $this->date_time->format('m');
308
309
    $this->day = $this->date_time->format('d');
310
311
    $days_of_year = 0;
312
313
    foreach ( $this->config['islamic_month_days'] as $month => $value ) {
314
315
      if( $this->month > $month ) $days_of_year += $value;
316
317
    }
318
319
    $days_of_year += $this->day;
320
321
    $days_of_leap_years =  intval( ( ( $this->year - 1 ) / 3 )  );
322
323
    $days_of_ghamari_years = ( ( ( $this->year - 1 ) * 354 ) + $days_of_year + $days_of_leap_years );
324
325
    $days_of_gregorain_years = $days_of_ghamari_years + 227078;
326
327
    $days_of_gregorain_years = $days_of_gregorain_years - intval( ( ( $this->year + 578 ) / 4 ) );
328
329
    $gregorian_month = ( $days_of_gregorain_years % 365 );
330
331
    $gregorian_year = intval( $days_of_gregorain_years / 365 ) + 1;
332
333
    foreach ($this->config['gregorian_month_days'] as $month => $value) {
334
335
      if ( $gregorian_month < $value ) break;
336
337
        $gregorian_month -= $value;
338
    }
339
340
      $gregorian_day = $gregorian_month;
341
342
      $gregorian_month = $month;
0 ignored issues
show
Bug introduced by
The variable $month does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
343
344
      $this->date_time->setDate( $gregorian_year, $gregorian_month, $gregorian_day );
345
346
     return $this->date_time;
347
348
  }
349
350
}
351
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
352