Completed
Pull Request — master (#28)
by Andreas
03:51
created

Entry   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 74.07 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 100
loc 135
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C parse() 84 84 7
A getLatLonForLocation() 16 16 2
A getEventPage() 0 17 3

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
 * Copyright (c) 2015-2015 Andreas Heigl<[email protected]>
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @author    Andreas Heigl<[email protected]>
24
 * @copyright 2015-2015 Andreas Heigl/callingallpapers.com
25
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
26
 * @version   0.0
27
 * @since     06.03.2012
28
 * @link      http://github.com/joindin/callingallpapers
29
 */
30
namespace Callingallpapers\Parser\PapercallIo;
31
32
use Callingallpapers\Entity\Cfp;
33
use Callingallpapers\Service\TimezoneService;
34
use GuzzleHttp\Client;
35
36
class Entry
37
{
38
    /** @var  Cfp */
39
    protected $cfp;
40
41
    /** @var Client */
42
    protected $client;
43
44
    public function __construct(Cfp $cfp, Client $client, TimezoneService $timezoneService)
45
    {
46
        $this->cfp = $cfp;
47
        $this->client = $client;
48
        $this->tzService = $timezoneService;
0 ignored issues
show
Bug introduced by
The property tzService does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
49
    }
50
51 View Code Duplication
    public function parse($uri)
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...
52
    {
53
        $cfp = clone($this->cfp);
54
        try {
55
            $dom = new \DOMDocument('1.0', 'UTF-8');
56
57
            $content = file_Get_contents($uri);
58
            $content = mb_convert_encoding($content, 'UTF-8');
59
            $dom->loadHTML('<?xml version="1.0" charset="UTF-8" ?>' . $content);
60
            $dom->preserveWhiteSpace = false;
61
62
            $timezone = 'UTC';
63
            $xpath = new \DOMXPath($dom);
64
65
            $eventLocation = new Location();
66
            $cfp->location = $eventLocation->parse($dom, $xpath);
67
68
            try {
69
                $location = $this->getLatLonForLocation($cfp->location);
70
                $cfp->latitude = $location[0];
0 ignored issues
show
Documentation Bug introduced by
The property $latitude was declared of type double, but $location[0] is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
71
                $cfp->longitude = $location[1];
0 ignored issues
show
Documentation Bug introduced by
The property $longitude was declared of type double, but $location[1] is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
72
                $timezone = $this->tzService->getTimezoneForLocation($location[0], $location[1]);
73
            } catch (\UnexpectedValueException $e) {
74
                error_log($e->getMessage());
75
            }
76
            $cfp->timezone = $timezone;
77
78
79
            $closingDateParser = new ClosingDate($timezone);
80
            $cfp->dateEnd = $closingDateParser->parse($dom, $xpath);
0 ignored issues
show
Documentation Bug introduced by
It seems like $closingDateParser->parse($dom, $xpath) of type object<Callingallpapers\Entity\Cfp> is incompatible with the declared type object<DateTimeInterface> of property $dateEnd.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
82
            $eventPageDom = $this->getEventPage($xpath);
83
            $eventXpath = new \DOMXPath(($eventPageDom));
84
85
            $descriptionParser = new Description();
86
            $cfp->description = $descriptionParser->parse($dom, $xpath);
87
88
            $openingDateParser = new OpeningDate($timezone);
89
            try {
90
                $cfp->dateStart = $openingDateParser->parse($dom, $xpath);
91
            } catch (\Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
92
93
            $cfpUriParser = new Uri();
94
            $cfp->uri = $cfpUriParser->parse($dom, $xpath);
95
96
            $confNameParser = new EventName();
97
            $cfp->conferenceName = $confNameParser->parse($dom, $xpath);
98
99
            $confUriParser = new EventUri();
100
            $cfp->conferenceUri  = $confUriParser->parse($eventPageDom, $eventXpath);
101
102
            $eventStartDate = new EventStartDate($timezone);
103
            $cfp->eventStartDate = $eventStartDate->parse($dom, $xpath);
104
105
            try {
106
                $eventEndDate      = new EventEndDate($timezone);
107
                $cfp->eventEndDate = $eventEndDate->parse($dom, $xpath);
108
            } catch (\InvalidArgumentException $e) {
109
                $cfp->eventEndDate = $cfp->eventStartDate;
110
            }
111
112
            $eventLocation = new Location();
113
            $cfp->location = $eventLocation->parse($dom, $xpath);
114
115
            try {
116
                $location = $this->getLatLonForLocation($cfp->location);
117
                $cfp->latitude = $location[0];
118
                $cfp->longitude = $location[1];
119
            } catch (\UnexpectedValueException $e) {
120
                error_log($e->getMessage());
121
            }
122
123
            try {
124
                $tags = new Tags();
125
                $cfp->tags = $tags->parse($eventPageDom, $eventXpath);
126
            } catch (\InvalidArgumentException $e) {
127
                $cfp->tags = [];
128
            }
129
130
            return $cfp;
131
        } catch (\Exception $e) {
132
            throw $e;
133
        }
134
    }
135
136 View Code Duplication
    protected function getLatLonForLocation($location)
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...
137
    {
138
        $result = $this->client->get(sprintf(
139
            'https://nominatim.openstreetmap.org/search?q=%1$s&format=json',
140
            urlencode($location)
141
        ));
142
143
        $locations = json_decode($result->getBody()->getContents());
144
145
        if (empty($locations)) {
146
            return [0, 0];
147
        }
148
        $location = $locations[0];
149
150
        return [$location->lat, $location->lon];
151
    }
152
153
    public function getEventPage($xpath)
154
    {
155
        $confPath = $xpath->query("//h3/a[contains(@class, 'summary')]");
156
157
        if (! $confPath || $confPath->length == 0) {
158
            throw new \InvalidArgumentException('We can\'t find an EventPage');
159
        }
160
161
        $dom = new \DOMDocument('1.0', 'UTF-8');
162
163
        $content = file_get_contents('https://lanyrd.com' . $confPath->item(0)->attributes->getNamedItem('href')->textContent);
164
        $content = mb_convert_encoding($content, 'UTF-8');
165
        $dom->loadHTML('<?xml version="1.0" charset="UTF-8" ?>' . $content);
166
        $dom->preserveWhiteSpace = false;
167
168
        return $dom;
169
    }
170
}
171