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

DtStartTransformer::transform()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 28
Code Lines 15

Duplication

Lines 9
Ratio 32.14 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 28
rs 6.7272
cc 7
eloc 15
nc 7
nop 1
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
}