Completed
Push — master ( eaa02d...91cedd )
by mehdi
02:17
created

Datium::__construct()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 53
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 53
rs 8.9849
cc 4
eloc 29
nc 4
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Main Datium class
4
 *
5
 * @category Core
6
 * @package  OpenCafe\Datium
7
 * @author   Mehdi Hosseini <[email protected]>
8
 * @license  icense https://opensource.org/licenses/MIT
9
 * @link     https://github.com/opencafe/datium
10
 * @since    Aug 17, 2015
11
 */
12
13
namespace OpenCafe;
14
15
use DateTime;
16
use DateInterval;
17
use OpenCafe\Tools\Convert;
18
use OpenCafe\Tools\Leap;
19
use OpenCafe\Tools\DayOf;
20
use OpenCafe\Tools\Lang;
21
22
use OpenCafe\Datium;
23
24
/**
25
 * Main Datium class
26
 *
27
 * @category Core
28
 * @package  OpenCafe\Datium
29
 * @author   Mehdi Hosseini <[email protected]>
30
 * @license  icense https://opensource.org/licenses/MIT
31
 * @link     https://github.com/opencafe/datium
32
 * @since    Aug 17, 2015
33
 */
34
class Datium
35
{
36
37
    /**
38
   * Store DateTime object
39
   */
40
    protected $date_time;
41
42
    protected static $static_date_time;
43
44
    /**
45
   * Store config file statements
46
   *
47
   * @param array
48
   */
49
    protected $config;
50
51
    protected $date_interval_expression;
52
53
    protected static $date_start;
54
55
    protected static $date_end;
56
57
    protected $translate_from;
58
59
    protected $translate_to;
60
61
    protected $toConfig;
62
63
    protected $fromConfig;
64
65
    /**
66
   * Return store day number
67
   *
68
   * @param integer
69
   */
70
    protected $day_of;
71
72
    protected $leap;
73
74
    protected $events;
75
76
    protected $translate;
77
78
    protected $gregorian_DayofWeek;
79
80
    protected $convert_calendar;
81
82
    protected $calendar_type;
83
84
    protected static $array_date;
85
86
    protected static $call_type;
87
88
    protected $translate_from_file;
89
90
    protected $translate_to_file;
91
92
    protected $language;
93
94
    /**
95
    * Datium class constructure
96
    */
97
    public function __construct()
98
    {
99
100
        $this->language = 'en';
101
102
        $this->translate_from = 'gregorian';
103
104
        $this->translate_to = 'gregorian';
105
106
        $this->config = include 'Config.php';
107
108
        $this->calendar_type = $this->config[ 'default_calendar' ];
109
110
        date_default_timezone_set($this->config[ 'timezone' ]);
111
112
        $this->calendar_type = 'gregorian';
113
114
        switch (Datium::$call_type) {
115
            case 'now':
116
                $this->date_time = new DateTime('now');
117
118
                $this->gregorian_DayofWeek = $this->date_time->format('w');
119
120
                break;
121
122
            case 'make':
123
                $this->date_time = new DateTime('now');
124
125
                $this->date_time->setDate(
126
                    self::$array_date[ 'year' ],
127
                    self::$array_date[ 'month' ],
128
                    self::$array_date[ 'day' ]
129
                );
130
131
                $this->date_time->setTime(
132
                    self::$array_date[ 'hour' ],
133
                    self::$array_date[ 'minute' ],
134
                    self::$array_date[ 'second' ]
135
                );
136
137
                $this->gregorian_DayofWeek = $this->date_time->format('w');
138
139
                break;
140
141
            case 'set':
142
                $this->date_time = Datium::$static_date_time;
143
144
                $this->gregorian_DayofWeek = $this->date_time->format('w');
145
        }
146
147
        $this->convert_calendar = new Convert();
148
149
    }
150
151
    /**
152
    * Return all datetime parts as an object
153
    *
154
    * @return object
155
    */
156
    public function all()
157
    {
158
159
        return ( object ) array(
160
161
        'second' => $this->date_time->format('s'),
162
163
        'minute' => $this->date_time->format('m'),
164
165
        'hour' => $this->date_time->format('H'),
166
167
        'day' => $this->date_time->format('d'),
168
169
        'month' => $this->date_time->format('m'),
170
171
        'year' => $this->date_time->format('Y')
172
173
        );
174
175
    }
176
177
    /**
178
   * Get current datetime
179
   *
180
   * @since  Aug 17 2015
181
   * @return object
182
   */
183
    public static function now()
184
    {
185
186
        self::$call_type = 'now';
187
188
        return new Datium();
189
190
    }
191
192
    /**
193
   * Create new date time
194
   *
195
   * @param integer $year   Year number
196
   * @param integer $month  month number
197
   * @param integer $day    day number
198
   * @param integer $hour   hour number
199
   * @param integer $minute minute number
200
   * @param integer $second second number
201
   *
202
   * @return object
203
   */
204
    public static function create($year = 2000, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0)
205
    {
206
207
        /**
208
       * When we want to set a Datetime object to Datium
209
       */
210
        if (func_num_args() === 1) {
211
            self::$static_date_time = func_get_arg(0);
212
213
            self::$call_type = 'set';
214
        } else {
215
            self::$array_date = array(
216
              'year' => $year,
217
              'month' => $month,
218
              'day' => $day,
219
              'hour' => $hour,
220
              'minute' => $minute,
221
              'second' => $second
222
            );
223
224
            self::$call_type = 'make';
225
        }
226
227
          return new Datium();
228
229
    }
230
231
    /**
232
    * Select The range between two date object
233
    *
234
    * @param object $date_start Start of the DateTime
235
    * @param object $date_end   End of the DateTime
236
    *
237
    * @return object
238
    */
239
    public static function between($date_start, $date_end)
240
    {
241
242
        self::$date_start = $date_start;
243
244
        self::$date_end = $date_end;
245
246
        self::$call_type = 'between';
247
248
        return new Datium();
249
250
    }
251
252
    /**
253
   * Convert from current calendar, what type is current calendar?
254
   *
255
   * @param object $calendar Assigned calendar to start from
256
   *
257
   * @return $object
0 ignored issues
show
Documentation introduced by
The doc-type $object could not be parsed: Unknown type name "$object" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
258
   */
259 View Code Duplication
    public function from($calendar)
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...
260
    {
261
262
        $this->convert = new Convert($this->date_time);
0 ignored issues
show
Bug introduced by
The property convert does not seem to exist. Did you mean convert_calendar?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
263
264
        $this->date_time = $this->convert->from($calendar);
0 ignored issues
show
Bug introduced by
The property convert does not seem to exist. Did you mean convert_calendar?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
265
266
267
        /**
268
     * We need this part for DayOf class
269
     */
270
        $this->calendar_type = $calendar;
271
272
        $this->translate_to = $calendar;
273
274
        return $this;
275
276
    }
277
278
    /**
279
    * Convert date to current Date
280
    *
281
    * @param object $calendar Assigned calendar to when calendar should start.
282
    *
283
    * @return object
284
    */
285 View Code Duplication
    public function to($calendar)
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...
286
    {
287
288
        $this->convert = new Convert($this->date_time);
0 ignored issues
show
Bug introduced by
The property convert does not seem to exist. Did you mean convert_calendar?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
289
290
        $this->date_time = $this->convert->to($calendar);
0 ignored issues
show
Bug introduced by
The property convert does not seem to exist. Did you mean convert_calendar?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
291
292
        /**
293
     * We need this part for DayOf class
294
     */
295
        $this->calendar_type = $calendar;
296
297
        $this->translate_to = $calendar;
298
299
        return $this;
300
301
    }
302
303
304
    /**
305
   * Difference between two time
306
   *
307
   * @param DateTime $start Start of the date
308
   * @param DateTime $end   End of the date
309
   *
310
   * @return object
311
   */
312
    public static function diff($start, $end)
313
    {
314
315
        return date_diff($start, $end);
316
317
    }
318
319
    /**
320
   * Add new date value to current date
321
   *
322
   * @param string $value How much date should be added to current date
323
   *
324
   * @return object
325
   */
326 View Code Duplication
    public function add($value)
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...
327
    {
328
329
        $this->date_interval_expression = str_replace(
330
            $this->config[ 'date_simple' ],
331
            $this->config[ 'date_interval' ],
332
            $value
333
        );
334
335
        $this->date_interval_expression = str_replace(
336
            ' ',
337
            '',
338
            'P' . $this->date_interval_expression
339
        );
340
341
        $this->date_time->add(
342
            new DateInterval($this->date_interval_expression)
343
        );
344
345
        return $this;
346
347
    }
348
349
    /**
350
   * Sub date from current date
351
   *
352
   * @param string $value How much date should increase from current date
353
   *
354
   * @return obejct
355
   */
356 View Code Duplication
    public function sub($value)
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...
357
    {
358
359
        $this->date_interval_expression = str_replace(
360
            $this->config[ 'date_simple' ],
361
            $this->config[ 'date_interval' ],
362
            $value
363
        );
364
365
        $this->date_interval_expression = str_replace(
366
            ' ',
367
            '',
368
            'P' . $this->date_interval_expression
369
        );
370
371
        $this->date_time->sub(
372
            new DateInterval($this->date_interval_expression)
373
        );
374
375
        return $this;
376
377
    }
378
379
    /**
380
   * Check if current year is leap or not
381
   *
382
   * @param string $type Name of the calendar to caculate leap year
383
   *
384
   * @return boolean
385
   */
386
    public function leap($type = 'gregorian')
387
    {
388
389
        $this->leap = new Leap($this->date_time->format('Y'), $type);
390
391
        return $this->leap;
392
393
    }
394
395
    /**
396
    * Caculate day of year or week
397
    *
398
    * @since Aug, 22 2015
399
    *
400
    * @return integer
401
    */
402
    public function dayOf()
403
    {
404
405
        $this->day_of = new DayOf($this->date_time, $this->calendar_type);
0 ignored issues
show
Bug introduced by
It seems like $this->calendar_type can also be of type object; however, OpenCafe\Tools\DayOf::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
406
407
        return $this->day_of;
408
409
    }
410
411
    // public function events()
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
412
    // {
413
    //
414
    //     if (Datium::$call_type == 'between' ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
415
    //
416
    //         $this->events = new Events(Datium::$date_start, Datium::$date_end);
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
417
    //
418
    //     } else {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
419
    //
420
    //         $this->events = new Events($this->date_time);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
421
    //
422
    //     }
423
    //
424
    //     return $this->events;
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
425
    //
426
    // }
427
428
    /**
429
    * Return Datetime as a original object
430
    *
431
    * @since Oct 22, 2015
432
    *
433
    * @return object
434
    */
435
    public function object()
436
    {
437
438
        return $this->date_time;
439
440
    }
441
442
    /**
443
    * Translate current date string to selected language
444
    *
445
    * @param string $language language short name fa, en, ar ...
446
    *
447
    * @return object
448
    */
449
    public function lang($language = 'fa')
450
    {
451
452
        $this->language = $language;
453
454
        $this->result = new Lang();
0 ignored issues
show
Bug introduced by
The property result 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...
455
456
        $this->result->setConfig($this->language);
457
458
        $this->toConfig = $this->result->getConfig();
459
460
        return $this;
461
462
    }
463
464
    /**
465
   * Return fainal result
466
   *
467
   * @param string $format Date format
468
   *
469
   * @since Aug 17 2015
470
   *
471
   * @return string
472
   */
473
    public function get($format = 'Y-m-d H:i:s')
474
    {
475
476
        // $this->translate_from_file = include( 'Lang/en/general.php' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
477
        //
478
        // $this->translate_to_file = include( 'Lang/' . $this->language . '/general.php' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
479
480
        if (is_null($this->fromConfig)) {
481
            $this->fromConfig = include 'CalendarSettings/' .
482
                                ucfirst($this->translate_from) . '.php';
483
        }
484
485
486
        if (is_null($this->toConfig)) {
487
            $this->toConfig = include 'CalendarSettings/' .
488
                                       ucfirst($this->translate_to) . '.php';
489
        }
490
491
        $string_date = $this->date_time->format($format);
492
493
        if ($this->translate_to != 'gregorian') {
494
            $string_date = str_replace(
495
                $this->fromConfig[ 'month' ],
496
                $this->toConfig[ 'month' ],
497
                $string_date
498
            );
499
500
            $string_date = str_replace(
501
                $this->fromConfig[ 'days_of_week' ],
502
                $this->toConfig[ 'days_of_week' ][ $this->gregorian_DayofWeek ],
503
                $string_date
504
            );
505
506
            $string_date = str_replace(
507
                $this->fromConfig[ 'numbers' ],
508
                $this->toConfig[ 'numbers' ],
509
                $string_date
510
            );
511
512
            $string_date = str_replace(
513
                $this->fromConfig[ 'am_time' ],
514
                $this->toConfig[ 'am_time' ],
515
                $string_date
516
            );
517
518
            $string_date = str_replace(
519
                $this->fromConfig[ 'pm_time' ],
520
                $this->toConfig[ 'pm_time' ],
521
                $string_date
522
            );
523
524
            $string_date = str_replace(
525
                $this->fromConfig[ 'end_of_days' ],
526
                $this->toConfig[ 'end_of_days' ],
527
                $string_date
528
            );
529
        }
530
531
        // foreach( $this->translate_to_file as $key => $value ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
532
        //
533
          // $string_date = str_replace(
534
          //   $this->translate_from_file[ $key ],
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
535
          //   $this->translate_to_file[ $key ],
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
536
          //   $string_date
537
          // );
538
        //
539
        // }
540
541
        return $string_date;
542
543
    }
544
}
545