Test Failed
Push — master ( 684014...621eb3 )
by Dev
01:54
created

Search::getSelector()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace rOpenDev\Google;
4
5
abstract class Search
6
{
7
    use CacheTrait, ConfSearchTrait, SleepTrait;
8
9
    // =======
10
    // -------
11
    // =======
12
13
    /** @var int Current page * */
14
    protected $page = 1;
15
16
    /**
17
     * @var string contain the current error
18
     */
19
    protected $error;
20
21
    protected $errors = [
22
        1 => 'Google Captcha',
23
        2 => 'Google `We\'re sorry` (flagged as automated request)',
24
        3 => 'Erreurs cURL',
25
    ];
26
27
    /**
28
     * Contient les erreurs provenant du cURL.
29
     */
30
    public $cErrors;
31
32
    public function generateGoogleSearchUrl()
33
    {
34
        $this->setParameter('q', $this->q);
35
        $url = 'https://www.google.'.$this->tld.'/search?'.$this->generateParameters(); //$url = 'https://www.google.'.$this->tld.'/search?q='.self::encodeLikeGoogle($kw)
36
        return $url;
37
    }
38
39
    protected function generateParameters()
40
    {
41
        return http_build_query($this->parameters, '', '&');
42
    }
43
44
    /**
45
     * @return string|false Contenu html de la page
46
     */
47
    abstract protected function requestGoogle(string $url);
48
49
    /*
50
     * Am I Kicked By Google ? Did you reach the google limits ?!
51
     *
52
     * @param string $output Html source
53
     *
54
     * @return int|false
55
     */
56
    protected function amIKickedByGoogleThePowerful($output)
57
    {
58
        /* Google respond :
59
         * We're sorry...... but your computer or network may be sending automated queries.
60
         * To protect our users, we can't process your request right now.'
61
         */
62
        if (false !== strpos($output, '<title>Sorry...</title>')) {
63
            return 2;
64
        }
65
66
        /* Captcha Google */
67
        elseif (false !== strpos($output, 'e=document.getElementById(\'captcha\');if(e){e.focus();}')) {
68
            return 1;
69
        }
70
71
        /* RAS */
72
        return false;
73
    }
74
75
    /**
76
     * @return string explaining the error
77
     */
78
    public function getError()
79
    {
80
        if (null !== $this->error) {
81
            return $this->errors[$this->error];
82
        }
83
84
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
85
    }
86
87
    /**
88
     * @return array|false containing the results with column type, link, title
89
     */
90
    public function extractResults()
91
    {
92
        for ($this->page = 1; $this->page <= $this->nbrPage; ++$this->page) {
93
            if (!isset($url)) {// On génère l'url pour la première requète... Ensuite, on utilisera le lien Suivant.
94
                $url = $this->generateGoogleSearchUrl();
95
            }
96
97
            $output = $this->requestGoogle($url);
98
            if (false === $output) {
99
                return false;
100
            }
101
102
            $extract = new ExtractResults($output);
103
104
            $this->result = array_merge($this->result, $extract->getOrganicResults());
105
106
            if ($this->nbrPage > 1 && isset($this->html->find('#pnnext')[0])) { // #pnnext // td.b a
0 ignored issues
show
Bug Best Practice introduced by
The property html does not exist on rOpenDev\Google\Search. Did you maybe forget to declare it?
Loading history...
107
                $url = 'https://www.google.'.$this->tld.str_replace('&amp;', '&', $this->html->find('#pnnext')[0]->href);
108
            } else {
109
                break;
110
            }
111
        }
112
113
        return $this->result;
114
    }
115
116
    /**
117
     * getNbrResults va chercher le nombre de résulats que Google affiche proposer.
118
     *
119
     * @return int
120
     */
121
    public function getNbrResults()
122
    {
123
        $url = $this->generateGoogleSearchUrl();
124
        $output = $this->requestGoogle($url);
125
        if (false !== $output) {
126
            $html = new \simple_html_dom();
127
            $html->load($output);
128
129
            $rS = $html->find('#resultStats');
130
            if (isset($rS[0]->plaintext)) {
131
                $s = (string) $this->normalizeTextFromGoogle($rS[0]->plaintext);
0 ignored issues
show
Bug introduced by
The method normalizeTextFromGoogle() does not exist on rOpenDev\Google\Search. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
                $s = (string) $this->/** @scrutinizer ignore-call */ normalizeTextFromGoogle($rS[0]->plaintext);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
133
                return intval(preg_replace('/[^0-9]/', '', $s));
134
            }
135
        }
136
    }
137
}
138