Completed
Push — master ( a67fbc...6c0a08 )
by Samuel
11s
created

DtStartTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 24.32 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 0
dl 9
loc 37
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C transform() 9 28 7

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
class DtStartTransformer implements TransformerInterface
6
{
7
    const RRULE_PARAMETER = 'DTSTART';
8
9
    /**
10
     * @param string $rRule
11
     * @return \DateTime
12
     */
13
    public function transform($rRule)
14
    {
15
        if(preg_match('/' . $this::RRULE_PARAMETER . ';TZID=([a-zA-Z_-]+[\/[a-zA-Z_+\-0-9]+]?):([0-9]{8}T[0-9]{6})/', $rRule, $matches)) {
16
            try {
17
                return \DateTime::createFromFormat('Ymd\THis', $matches[2], new \DateTimeZone($matches[1]));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFor...TimeZone($matches[1])); of type DateTime|false adds false to the return on line 17 which is incompatible with the return type documented by Recurrence\RruleTransfor...tTransformer::transform of type DateTime|null. It seems like you forgot to handle an error condition.
Loading history...
18
            } catch (\Exception $e) {
19
                throw new \InvalidArgumentException(sprintf('Invalid RRULE [%s] option : [%s] with timezone [%s]', $this::RRULE_PARAMETER, $matches[2], $matches[1]));
20
            }
21
        }
22
23 View Code Duplication
        if(preg_match('/' . $this::RRULE_PARAMETER . '=([0-9]{8}T[0-9]{6}Z)/', $rRule, $matches)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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
            return \DateTime::createFromFormat('Ymd\THisZ', $matches[1], new \DateTimeZone('UTC'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFor... \DateTimeZone('UTC')); of type DateTime|false adds false to the return on line 24 which is incompatible with the return type documented by Recurrence\RruleTransfor...tTransformer::transform of type DateTime|null. It seems like you forgot to handle an error condition.
Loading history...
25
        }
26
27 View Code Duplication
        if(preg_match('/' . $this::RRULE_PARAMETER . '=([0-9]{8}T[0-9]{6})/', $rRule, $matches)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
28
            return \DateTime::createFromFormat('Ymd\THis', $matches[1]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFor...d\\THis', $matches[1]); of type DateTime|false adds false to the return on line 28 which is incompatible with the return type documented by Recurrence\RruleTransfor...tTransformer::transform of type DateTime|null. It seems like you forgot to handle an error condition.
Loading history...
29
        }
30
31 View Code Duplication
        if(preg_match('/' . $this::RRULE_PARAMETER . '=([0-9]{8})/', $rRule, $matches)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
32
            return \DateTime::createFromFormat('Ymd', $matches[1]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFormat('Ymd', $matches[1]); of type DateTime|false adds false to the return on line 32 which is incompatible with the return type documented by Recurrence\RruleTransfor...tTransformer::transform of type DateTime|null. It seems like you forgot to handle an error condition.
Loading history...
33
        }
34
35
        if(preg_match('/' . $this::RRULE_PARAMETER . '/', $rRule, $matches)) {
36
            throw new \InvalidArgumentException(sprintf('Invalid RRULE [%s]', $this::RRULE_PARAMETER));
37
        }
38
39
        return null;
40
    }
41
}