Completed
Push — master ( de9ece...1b5216 )
by Gabriel
05:03
created

ResultPage   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 180
Duplicated Lines 14.44 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 3
dl 26
loc 180
c 0
b 0
f 0
ccs 0
cts 88
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A generateContent() 0 10 1
A getModelClassName() 0 4 1
A parseFullName() 0 6 1
A parseFinishTime() 0 6 1
A parsePositions() 0 17 1
C parseResultBio() 0 25 7
B parseSplits() 0 22 5
A parseSplitsHeader() 10 19 4
A parseSplitRow() 16 16 4
A parseSplitCell() 0 9 2

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 Sportic\Timing\RaceTecClient\Parsers;
4
5
use DOMElement;
6
use Sportic\Timing\RaceTecClient\Models\Split;
7
8
/**
9
 * Class ResultPage
10
 * @package Sportic\Timing\RaceTecClient\Parsers
11
 */
12
class ResultPage extends AbstractParser
13
{
14
    protected $returnContent = [];
15
16
    /**
17
     * @inheritdoc
18
     */
19
    protected function generateContent()
20
    {
21
        $this->returnContent['full_name'] = $this->parseFullName();
22
        $this->returnContent['time']      = $this->parseFinishTime();
23
        $this->parsePositions();
24
        $this->parseResultBio();
25
        $this->returnContent['splits'] = $this->parseSplits();
26
27
        return $this->returnContent;
28
    }
29
30
    public function getModelClassName()
31
    {
32
        // TODO: Implement getModelClassName() method.
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    protected function parseFullName()
39
    {
40
        return trim(
41
            $this->getCrawler()->filter('#ctl00_Content_Main_lblName')->text()
42
        );
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    protected function parseFinishTime()
49
    {
50
        return trim(
51
            $this->getCrawler()->filter('#ctl00_Content_Main_lblResFinishTime')->text()
52
        );
53
    }
54
55
    protected function parsePositions()
56
    {
57
        $posGenData = $this->getCrawler()->filter('#ctl00_Content_Main_lblResOPos')->text();
58
        list($posGen, $participants) = explode('/', $posGenData);
59
        $this->returnContent['pos_gen'] = trim($posGen);
60
        $this->returnContent['race']['participants'] = trim($participants);
61
62
        $posGenData = $this->getCrawler()->filter('#ctl00_Content_Main_lblResGPos')->text();
63
        list($posGen, $participants) = explode('/', $posGenData);
64
        $this->returnContent['pos_gender'] = trim($posGen);
65
        $this->returnContent['gender']['participants'] = trim($participants);
66
67
        $posGenData = $this->getCrawler()->filter('#ctl00_Content_Main_lblResCPos')->text();
68
        list($posGen, $participants) = explode('/', $posGenData);
69
        $this->returnContent['pos_category'] = trim($posGen);
70
        $this->returnContent['category']['participants'] = trim($participants);
71
    }
72
73
    protected function parseResultBio()
74
    {
75
        $table = $this->getCrawler()->filter('#ctl00_Content_Main_grdBio');
76
        $rows = $table->filter('tbody > tr');
77
78
        foreach ($rows as $row) {
79
            $column = $row->childNodes[0]->nodeValue;
80
            $value = $row->childNodes[2]->nodeValue;
81
82
            switch ($column) {
83
                case 'Race No':
84
                    $this->returnContent['bib'] = $value;
85
                    break;
86
                case 'Gender':
87
                    $this->returnContent['gender']['name'] = ($value == 'Female' ? 'female' : 'male');
88
                    break;
89
                case 'Category':
90
                    $this->returnContent['category']['name'] = $value;
91
                    break;
92
                case 'Status':
93
                    $this->returnContent['status']['name'] = $value;
94
                    break;
95
            }
96
        }
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    protected function parseSplits()
103
    {
104
        $return     = [];
105
        $headerData = [];
106
        $splitRows  = $this->getCrawler()->filter(
107
            '#ctl00_Content_Main_grdSplits_DXMainTable > tbody > tr'
108
        );
109
        if ($splitRows->count() > 0) {
110
            foreach ($splitRows as $resultRow) {
111
                if ($resultRow->getAttribute('id') === 'ctl00_Content_Main_grdSplits_DXHeadersRow') {
112
                    $headerData = $this->parseSplitsHeader($resultRow);
113
                } else {
114
                    $result = $this->parseSplitRow($resultRow, $headerData);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->parseSplitRow($resultRow, $headerData) (which targets Sportic\Timing\RaceTecCl...ltPage::parseSplitRow()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
115
                    if ($result) {
116
                        $return[] = $result;
117
                    }
118
119
                }
120
            }
121
        }
122
        return $return;
123
    }
124
125
    /**
126
     * @param DOMElement $row
127
     *
128
     * @return array
129
     */
130
    protected function parseSplitsHeader($row)
131
    {
132
        $return = [];
133
134
        $fieldMap = Split::getLabelMaps();
135
        $colNum   = 0;
136 View Code Duplication
        foreach ($row->childNodes as $node) {
0 ignored issues
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...
137
            if ($node instanceof DOMElement) {
138
                $fieldName = trim($node->nodeValue);
139
                $labelFind = array_search($fieldName, $fieldMap);
140
                if ($labelFind) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $labelFind of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
141
                    $return[$colNum] = $labelFind;
142
                }
143
                $colNum++;
144
            }
145
        }
146
147
        return $return;
148
    }
149
150
    /**
151
     * @param $row
152
     * @param $headerData
153
     *
154
     * @return Split|null
155
     */
156 View Code Duplication
    protected function parseSplitRow($row, $headerData)
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...
157
    {
158
        $parameters = [];
159
        $i          = 0;
160
        foreach ($row->childNodes as $cell) {
161
            if ($cell instanceof DOMElement) {
162
                $parameters = $this->parseSplitCell($i, $cell, $headerData, $parameters);
163
                $i++;
164
            }
165
        }
166
        if (count($parameters)) {
167
            return new Split($parameters);
168
        }
169
170
        return null;
171
    }
172
173
174
    /**
175
     * @param $colCount
176
     * @param $cell
177
     * @param $headerData
178
     * @param $parameters
179
     *
180
     * @return array
181
     */
182
    protected function parseSplitCell($colCount, $cell, $headerData, $parameters)
183
    {
184
        if (isset($headerData[$colCount])) {
185
            $field              = $headerData[$colCount];
186
            $parameters[$field] = trim($cell->nodeValue);
187
        }
188
189
        return $parameters;
190
    }
191
}
192