Completed
Pull Request — master (#14)
by Samuel
03:28
created

DtStartTransformer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 25.71 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 9
loc 35
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B transform() 9 26 6

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 Recurrence\RruleTransformer;
4
5
/**
6
 * Class DtStartTransformer
7
 * @package Recurrence\RruleTransformer
8
 */
9
class DtStartTransformer extends AbstractRruleTransformer implements TransformerInterface
10
{
11
    const RRULE_PARAMETER = 'DTSTART';
12
13
    /**
14
     * @param string $rRule
15
     * @return \DateTime
16
     */
17
    public function transform($rRule)
18
    {
19 1
        if (preg_match('/'.$this::RRULE_PARAMETER.';TZID=([a-zA-Z_-]+[\/[a-zA-Z_+\-0-9]+]?):([0-9]{8}T[0-9]{6})/', $rRule, $matches)) {
20
            try {
21 1
                return \DateTime::createFromFormat('Ymd\THis', $matches[2], new \DateTimeZone($matches[1]));
22 1
            } catch (\Exception $e) {
23 1
                throw new \InvalidArgumentException(sprintf('Invalid RRULE [%s] option : [%s] with timezone [%s]', $this::RRULE_PARAMETER, $matches[2], $matches[1]));
24
            }
25
        }
26
27 1 View Code Duplication
        if (preg_match('/'.$this::RRULE_PARAMETER.'=([0-9]{8}T[0-9]{6}Z)/', $rRule, $matches)) {
28 1
            return \DateTime::createFromFormat('Ymd\THisZ', $matches[1], new \DateTimeZone('UTC'));
29
        }
30
31 1 View Code Duplication
        if (preg_match('/'.$this::RRULE_PARAMETER.'=([0-9]{8}T[0-9]{6})/', $rRule, $matches)) {
32 1
            return \DateTime::createFromFormat('Ymd\THis', $matches[1]);
33
        }
34
35 1 View Code Duplication
        if (preg_match('/'.$this::RRULE_PARAMETER.'=([0-9]{8})/', $rRule, $matches)) {
36 1
            return \DateTime::createFromFormat('Ymd', $matches[1]);
37
        }
38
39 1
        $this->throwExceptionOnInvalidParameter($rRule, $this::RRULE_PARAMETER);
40
41 1
        return null;
42
    }
43
}
44