Completed
Pull Request — master (#37)
by Andreas
05:11
created

ClosingDate   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 50 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 15
loc 30
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 15 15 3
A clearClosingHour() 0 4 1

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
declare(strict_types=1);
4
5
/**
6
 * Copyright Andrea Heigl <[email protected]>
7
 *
8
 * Licenses under the MIT-license. For details see the included file LICENSE.md
9
 */
10
namespace Callingallpapers\Subcommands\Sessionize\Parser;
11
12
use DateTimeImmutable;
13
use DateTimeZone;
14
use DOMDocument;
15
use DOMXPath;
16
use InvalidArgumentException;
17
use function str_replace;
18
use function trim;
19
20
class ClosingDate
21
{
22
    protected $timezone;
23
24
    public function __construct(DateTimeZone $timezone)
25
    {
26
        $this->timezone = $timezone;
27
    }
28
29 View Code Duplication
    public function parse(DOMDocument $dom, DOMXPath $xpath) : DateTimeImmutable
0 ignored issues
show
Unused Code introduced by
The parameter $dom is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
30
    {
31
        $closingDateHolder = $xpath->query('//div[./div/span[contains(text(), "CfS closes at")]]');
32
33
        if (! $closingDateHolder || $closingDateHolder->length == 0) {
34
            throw new InvalidArgumentException('The CfP does not seem to have a closing date');
35
        }
36
37
        $closingDay = $closingDateHolder->item(0)->getElementsByTagName('h2')->item(0)->textContent;
38
        $closingHour = $closingDateHolder->item(0)->getElementsByTagName('span')->item(0)->textContent;
39
40
        $closingHour = $this->clearClosingHour($closingHour);
41
42
        return new DateTimeImmutable($closingDay . ' ' . $closingHour, $this->timezone);
43
    }
44
45
    private function clearClosingHour(string $closingHour) : string
46
    {
47
        return trim(str_replace('CfS closes at', '', $closingHour));
48
    }
49
}
50