Completed
Push — master ( 1f427e...eb5029 )
by Ron
01:13
created

DateTimeHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 52.63 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 20
loc 38
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 10 10 3
A createImmutable() 10 10 3
A getNextRunDateFromCronExpression() 0 9 2

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
namespace Kir\Services\Cmd\Dispatcher\Common;
3
4
use Cron\CronExpression;
5
use DateTime;
6
use DateTimeImmutable;
7
use DateTimeInterface;
8
use Exception;
9
use RuntimeException;
10
11
abstract class DateTimeHelper {
12 View Code Duplication
	public static function create($init = null): DateTime {
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...
13
		try {
14
			if($init instanceof DateTimeInterface) {
15
				return new DateTime($init->format('c'));
16
			}
17
			return new DateTime($init);
18
		} catch (Exception $e) {
19
			throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
20
		}
21
	}
22
	
23 View Code Duplication
	public static function createImmutable($init = null): DateTimeImmutable {
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...
24
		try {
25
			if($init instanceof DateTimeInterface) {
26
				return new DateTimeImmutable($init->format('c'));
27
			}
28
			return new DateTimeImmutable($init);
29
		} catch (Exception $e) {
30
			throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
31
		}
32
	}
33
	
34
	/**
35
	 * @param string $interval
36
	 * @param DateTimeInterface $now
37
	 * @return DateTimeImmutable
38
	 */
39
	public static function getNextRunDateFromCronExpression(string $interval, DateTimeInterface $now): DateTimeImmutable {
40
		try {
41
			$expr = new CronExpression($interval);
42
			$dt = $expr->getNextRunDate($now);
43
			return self::createImmutable($dt);
44
		} catch (Exception $e) {
45
			throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
46
		}
47
	}
48
}