AbstractParser::getCrawler()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Sportic\Timing\RaceTecClient\Parsers;
4
5
use Sportic\Timing\CommonClient\Content\ContentFactory;
6
use Sportic\Timing\CommonClient\Models\AbstractModel;
7
use Sportic\Timing\RaceTecClient\Scrapers\AbstractScraper;
8
use Symfony\Component\DomCrawler\Crawler;
9
10
/**
11
 * Class AbstractParser
12
 * @package Sportic\Timing\RaceTecClient\Parsers
13
 */
14
abstract class AbstractParser
15
{
16
17
    /**
18
     * @var AbstractScraper
19
     */
20
    protected $scraper;
21
22
    /**
23
     * @var Crawler
24
     */
25
    protected $crawler;
26
27
    /**
28
     * @var null|boolean
29
     */
30
    protected $isValidContent = null;
31
32
    /**
33
     * @var null|AbstractContent
34
     */
35
    protected $contents = null;
36
37
    /**
38
     * @return mixed
39
     */
40
    public function getContent()
41
    {
42
        if ($this->contents === null) {
43
            if ($this->isValidContent()) {
44
                $contents = $this->generateContent();
45
                $this->contents = ContentFactory::createFromArray($contents);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Sportic\Timing\CommonCl...ateFromArray($contents) of type object<Sportic\Timing\Co...ontent\AbstractContent> is incompatible with the declared type null|object<Sportic\Timi...arsers\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...
46
            } else {
47
                $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...arsers\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...
48
            }
49
        }
50
51
        return $this->contents;
52
    }
53
54
    abstract protected function generateContent();
55
56
    /**
57
     * @return bool|null
58
     */
59
    public function isValidContent()
60
    {
61
        if ($this->isValidContent == null) {
62
            $this->doValidation();
63
            $this->isValidContent = true;
64
        }
65
66
        return $this->isValidContent;
67
    }
68
69
    /**
70
     * @return void
71
     */
72
    protected function doValidation()
73
    {
74
    }
75
76
    /**
77
     * @return AbstractScraper
78
     */
79
    public function getScraper()
80
    {
81
        return $this->scraper;
82
    }
83
84
    /**
85
     * @param AbstractScraper $scraper
86
     */
87
    public function setScraper($scraper)
88
    {
89
        $this->scraper = $scraper;
90
    }
91
92
    /**
93
     * @return Crawler
94
     */
95
    public function getCrawler(): Crawler
96
    {
97
        return $this->crawler;
98
    }
99
100
    /**
101
     * @param Crawler $crawler
102
     */
103
    public function setCrawler(Crawler $crawler)
104
    {
105
        $this->crawler = $crawler;
106
    }
107
108
    /**
109
     * @return array
110
     */
111
    public function getArray()
112
    {
113
        return $this->getContent();
114
    }
115
116
    /**
117
     * @return AbstractModel
118
     */
119
    public function getModel()
120
    {
121
        $model      = $this->getNewModel();
122
        $parameters = $this->getContent();
123
        $model->setParameters($parameters);
0 ignored issues
show
Bug introduced by
It seems like $parameters defined by $this->getContent() on line 122 can also be of type false or object<Sportic\Timing\Co...ontent\AbstractContent>; however, Sportic\Timing\CommonCli...tModel::setParameters() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

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