Completed
Push — master ( 01907b...5981e6 )
by mehdi
03:03
created

Datium::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 58
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 29
c 2
b 0
f 1
nc 4
nop 0
dl 0
loc 58
rs 9.0077

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 $geregorian_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
116
        case 'now':
117
118
            $this->date_time = new DateTime('now');
119
120
            $this->gregorian_DayofWeek = $this->date_time->format('w');
0 ignored issues
show
Bug introduced by
The property gregorian_DayofWeek does not seem to exist. Did you mean geregorian_DayofWeek?

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...
121
122
            break;
123
124
        case 'make':
125
126
            $this->date_time = new DateTime('now');
127
128
            $this->date_time->setDate(
129
                self::$array_date[ 'year' ],
130
                self::$array_date[ 'month' ],
131
                self::$array_date[ 'day' ]
132
            );
133
134
            $this->date_time->setTime(
135
                self::$array_date[ 'hour' ],
136
                self::$array_date[ 'minute' ],
137
                self::$array_date[ 'second' ]
138
            );
139
140
            $this->gregorian_DayofWeek = $this->date_time->format('w');
0 ignored issues
show
Bug introduced by
The property gregorian_DayofWeek does not seem to exist. Did you mean geregorian_DayofWeek?

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...
141
142
            break;
143
144
        case 'set':
145
146
            $this->date_time = Datium::$static_date_time;
147
148
            $this->gregorian_DayofWeek = $this->date_time->format('w');
0 ignored issues
show
Bug introduced by
The property gregorian_DayofWeek does not seem to exist. Did you mean geregorian_DayofWeek?

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...
149
150
        }
151
152
        $this->convert_calendar = new Convert();
153
154
    }
155
156
    /**
157
    * Return all datetime parts as an object
158
    *
159
    * @return object
160
    */
161
    public function all()
162
    {
163
164
        return ( object ) array(
165
166
        'second' => $this->date_time->format('s'),
167
168
        'minute' => $this->date_time->format('m'),
169
170
        'hour' => $this->date_time->format('H'),
171
172
        'day' => $this->date_time->format('d'),
173
174
        'month' => $this->date_time->format('m'),
175
176
        'year' => $this->date_time->format('Y')
177
178
        );
179
180
    }
181
182
    /**
183
   * Get current datetime
184
   *
185
   * @since  Aug 17 2015
186
   * @return object
187
   */
188
    public static function now()
189
    {
190
191
        self::$call_type = 'now';
192
193
        return new Datium();
194
195
    }
196
197
    /**
198
   * Create new date time
199
   *
200
   * @param integer $year   Year number
201
   * @param integer $month  month number
202
   * @param integer $day    day number
203
   * @param integer $hour   hour number
204
   * @param integer $minute minute number
205
   * @param integer $second second number
206
   *
207
   * @return object
208
   */
209
    public static function create( $year = 2000, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0 )
210
    {
211
212
        /**
213
       * When we want to set a Datetime object to Datium
214
       */
215
        if(func_num_args() === 1 ) {
216
217
            self::$static_date_time = func_get_arg(0);
218
219
            self::$call_type = 'set';
220
221
        } else {
222
223
            self::$array_date = array(
224
              'year' => $year,
225
              'month' => $month,
226
              'day' => $day,
227
              'hour' => $hour,
228
              'minute' => $minute,
229
              'second' => $second
230
            );
231
232
            self::$call_type = 'make';
233
234
        }
235
236
          return new Datium();
237
238
    }
239
240
    /**
241
    * Select The range between two date object
242
    *
243
    * @param object $date_start Start of the DateTime
244
    * @param object $date_end   End of the DateTime
245
    *
246
    * @return object
247
    */
248
    public static function between( $date_start, $date_end )
249
    {
250
251
        self::$date_start = $date_start;
252
253
        self::$date_end = $date_end;
254
255
        self::$call_type = 'between';
256
257
        return new Datium();
258
259
    }
260
261
    /**
262
   * Convert from current calendar, what type is current calendar?
263
   *
264
   * @param object $calendar Assigned calendar to start from
265
   *
266
   * @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...
267
   */
268 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...
269
    {
270
271
        $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...
272
273
        $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...
274
275
276
        /**
277
     * We need this part for DayOf class
278
     */
279
        $this->calendar_type = $calendar;
280
281
        $this->translate_to = $calendar;
282
283
        return $this;
284
285
    }
286
287
    /**
288
    * Convert date to current Date
289
    *
290
    * @param object $calendar Assigned calendar to when calendar should start.
291
    *
292
    * @return object
293
    */
294 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...
295
    {
296
297
        $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...
298
299
        $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...
300
301
        /**
302
     * We need this part for DayOf class
303
     */
304
        $this->calendar_type = $calendar;
305
306
        $this->translate_to = $calendar;
307
308
        return $this;
309
310
    }
311
312
313
    /**
314
   * Difference between two time
315
   *
316
   * @param DateTime $start Start of the date
317
   * @param DateTime $end   End of the date
318
   *
319
   * @return object
320
   */
321
    public static function diff( $start, $end )
322
    {
323
324
        return date_diff($start, $end);
325
326
    }
327
328
    /**
329
   * Add new date value to current date
330
   *
331
   * @param string $value How much date should be added to current date
332
   *
333
   * @return object
334
   */
335 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...
336
    {
337
338
        $this->date_interval_expression = str_replace(
339
            $this->config[ 'date_simple' ],
340
            $this->config[ 'date_interval' ],
341
            $value
342
        );
343
344
        $this->date_interval_expression = str_replace(
345
            ' ',
346
            '',
347
            'P' . $this->date_interval_expression
348
        );
349
350
        $this->date_time->add(
351
            new DateInterval($this->date_interval_expression)
352
        );
353
354
        return $this;
355
356
    }
357
358
    /**
359
   * Sub date from current date
360
   *
361
   * @param string $value How much date should increase from current date
362
   *
363
   * @return obejct
364
   */
365 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...
366
    {
367
368
        $this->date_interval_expression = str_replace(
369
            $this->config[ 'date_simple' ],
370
            $this->config[ 'date_interval' ],
371
            $value
372
        );
373
374
        $this->date_interval_expression = str_replace(
375
            ' ',
376
            '',
377
            'P' . $this->date_interval_expression
378
        );
379
380
        $this->date_time->sub(
381
            new DateInterval($this->date_interval_expression)
382
        );
383
384
        return $this;
385
386
    }
387
388
    /**
389
   * Check if current year is leap or not
390
   *
391
   * @param string $type Name of the calendar to caculate leap year
392
   *
393
   * @return boolean
394
   */
395
    public function leap( $type = 'gregorian' )
396
    {
397
398
        $this->leap = new Leap($this->date_time->format('Y'), $type);
399
400
        return $this->leap;
401
402
    }
403
404
    /**
405
    * Caculate day of year or week
406
    *
407
    * @since Aug, 22 2015
408
    *
409
    * @return integer
410
    */
411
    public function dayOf()
412
    {
413
414
        $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...
415
416
        return $this->day_of;
417
418
    }
419
420
    // 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...
421
    // {
422
    //
423
    //     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...
424
    //
425
    //         $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...
426
    //
427
    //     } 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...
428
    //
429
    //         $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...
430
    //
431
    //     }
432
    //
433
    //     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...
434
    //
435
    // }
436
437
    /**
438
    * Return Datetime as a original object
439
    *
440
    * @since Oct 22, 2015
441
    *
442
    * @return object
443
    */
444
    public function object()
445
    {
446
447
        return $this->date_time;
448
449
    }
450
451
    /**
452
    * Translate current date string to selected language
453
    *
454
    * @param string $language language short name fa, en, ar ...
455
    *
456
    * @return object
457
    */
458
    public function lang( $language = 'fa' )
459
    {
460
461
        $this->language = $language;
462
463
        $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...
464
465
        $this->result->setConfig($this->language);
466
467
        $this->toConfig = $this->result->getConfig();
468
469
        return $this;
470
471
    }
472
473
    /**
474
   * Return fainal result
475
   *
476
   * @param string $format Date format
477
   *
478
   * @since Aug 17 2015
479
   *
480
   * @return string
481
   */
482
    public function get( $format = 'Y-m-d H:i:s' )
483
    {
484
485
        // $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...
486
        //
487
        // $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...
488
489
        if (is_null($this->fromConfig) ) {
490
491
            $this->fromConfig = include 'CalendarSettings/' .
492
                                ucfirst($this->translate_from) . '.php';
493
494
        }
495
496
497
        if (is_null($this->toConfig) ) {
498
499
            $this->toConfig = include 'CalendarSettings/' .
500
                                       ucfirst($this->translate_to) . '.php';
501
502
        }
503
504
        $string_date = $this->date_time->format($format);
505
506
        if ($this->translate_to != 'gregorian' ) {
507
508
            $string_date = str_replace(
509
                $this->fromConfig[ 'month' ],
510
                $this->toConfig[ 'month' ],
511
                $string_date
512
            );
513
514
            $string_date = str_replace(
515
                $this->fromConfig[ 'days_of_week' ],
516
                $this->toConfig[ 'days_of_week' ][ $this->gregorian_DayofWeek ],
0 ignored issues
show
Bug introduced by
The property gregorian_DayofWeek does not seem to exist. Did you mean geregorian_DayofWeek?

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...
517
                $string_date
518
            );
519
520
            $string_date = str_replace(
521
                $this->fromConfig[ 'numbers' ],
522
                $this->toConfig[ 'numbers' ],
523
                $string_date
524
            );
525
526
            $string_date = str_replace(
527
                $this->fromConfig[ 'am_time' ],
528
                $this->toConfig[ 'am_time' ],
529
                $string_date
530
            );
531
532
            $string_date = str_replace(
533
                $this->fromConfig[ 'pm_time' ],
534
                $this->toConfig[ 'pm_time' ],
535
                $string_date
536
            );
537
538
            $string_date = str_replace(
539
                $this->fromConfig[ 'end_of_days' ],
540
                $this->toConfig[ 'end_of_days' ],
541
                $string_date
542
            );
543
544
        }
545
546
        // 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...
547
        //
548
          // $string_date = str_replace(
549
          //   $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...
550
          //   $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...
551
          //   $string_date
552
          // );
553
        //
554
        // }
555
556
        return $string_date;
557
558
    }
559
560
}
561