Test Setup Failed
Push — master ( 1b5216...e31da6 )
by Gabriel
12:30
created

AbstractParser   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 135
ccs 0
cts 37
cp 0
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getContent() 0 13 3
generateContent() 0 1 ?
A isValidContent() 0 9 2
A doValidation() 0 3 1
A getScraper() 0 4 1
A setScraper() 0 4 1
A getCrawler() 0 4 1
A setCrawler() 0 4 1
A getArray() 0 4 1
A getModel() 0 8 1
A getNewModel() 0 7 1
getModelClassName() 0 1 ?
A getContentClassName() 0 4 1
1
<?php
2
3
namespace Sportic\Timing\RaceTecClient\Parsers;
4
5
use Sportic\Timing\RaceTecClient\Content\AbstractContent;
6
use Sportic\Timing\RaceTecClient\Content\ContentFactory;
7
use Sportic\Timing\RaceTecClient\Content\GenericContent;
8
use Sportic\Timing\RaceTecClient\Helper;
9
use Sportic\Timing\RaceTecClient\Models\AbstractModel;
10
use Sportic\Timing\RaceTecClient\Scrapers\AbstractScraper;
11
use Symfony\Component\DomCrawler\Crawler;
12
13
/**
14
 * Class AbstractParser
15
 * @package Sportic\Timing\RaceTecClient\Parsers
16
 */
17
abstract class AbstractParser
18
{
19
20
    /**
21
     * @var AbstractScraper
22
     */
23
    protected $scraper;
24
25
    /**
26
     * @var Crawler
27
     */
28
    protected $crawler;
29
30
    /**
31
     * @var null|boolean
32
     */
33
    protected $isValidContent = null;
34
35
    /**
36
     * @var null|AbstractContent
37
     */
38
    protected $contents = null;
39
40
    /**
41
     * @return mixed
42
     */
43
    public function getContent()
44
    {
45
        if ($this->contents === null) {
46
            if ($this->isValidContent()) {
47
                $contents = $this->generateContent();
48
                $this->contents = ContentFactory::createFromArray($contents);
49
            } else {
50
                $this->contents = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type null|object<Sportic\Timi...ontent\AbstractContent> of property $contents.

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...
51
            }
52
        }
53
54
        return $this->contents;
55
    }
56
57
    abstract protected function generateContent();
58
59
    /**
60
     * @return bool|null
61
     */
62
    public function isValidContent()
63
    {
64
        if ($this->isValidContent == null) {
65
            $this->doValidation();
66
            $this->isValidContent = true;
67
        }
68
69
        return $this->isValidContent;
70
    }
71
72
    /**
73
     * @return void
74
     */
75
    protected function doValidation()
76
    {
77
    }
78
79
    /**
80
     * @return AbstractScraper
81
     */
82
    public function getScraper()
83
    {
84
        return $this->scraper;
85
    }
86
87
    /**
88
     * @param AbstractScraper $scraper
89
     */
90
    public function setScraper($scraper)
91
    {
92
        $this->scraper = $scraper;
93
    }
94
95
    /**
96
     * @return Crawler
97
     */
98
    public function getCrawler(): Crawler
99
    {
100
        return $this->crawler;
101
    }
102
103
    /**
104
     * @param Crawler $crawler
105
     */
106
    public function setCrawler(Crawler $crawler)
107
    {
108
        $this->crawler = $crawler;
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function getArray()
115
    {
116
        return $this->getContent();
117
    }
118
119
    /**
120
     * @return AbstractModel
121
     */
122
    public function getModel()
123
    {
124
        $model      = $this->getNewModel();
125
        $parameters = $this->getContent();
126
        $model->setParameters($parameters);
0 ignored issues
show
Documentation introduced by
$parameters is of type false|object<Sportic\Tim...ontent\AbstractContent>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
128
        return $model;
129
    }
130
131
    /**
132
     * @return AbstractModel
133
     */
134
    public function getNewModel()
135
    {
136
        $className = $this->getModelClassName();
137
        $model     = new $className();
138
139
        return $model;
140
    }
141
142
    abstract public function getModelClassName();
143
144
    /**
145
     * @return string
146
     */
147
    protected function getContentClassName()
148
    {
149
        return GenericContent::class;
150
    }
151
}
152