Yandex::run()   C
last analyzed

Complexity

Conditions 11
Paths 21

Size

Total Lines 61
Code Lines 36

Duplication

Lines 29
Ratio 47.54 %

Importance

Changes 0
Metric Value
dl 29
loc 61
rs 6.2318
c 0
b 0
f 0
cc 11
eloc 36
nc 21
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenon
5
 * Date: 23/04/16
6
 * Time: 01:43.
7
 */
8
9
namespace Aszone\SearchHacking\Engines;
10
11
use Aszone\SearchHacking\Utils;
12
13
class Yandex extends Engine
14
{
15
    public function run()
16
    {
17
        $exit = false;
18
        $count = 0;
19
        $numPaginator = 0;
20
        $countProxyVirgin = rand(0, count($this->listOfVirginProxies) - 1);
21
        $resultFinal = array();
22
23
        $countProxyFail = array();
24
25
        while ($exit == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
26
            if ($count != 0) {
27
                $numPaginator = $count;
28
            }
29
30
            $urlOfSearch = 'https://yandex.ru/search/?text='.urlencode($this->commandData['dork']).'&p='.$numPaginator.'&lr=10136';
31
32
            $this->output('Page '.$count."\n");
33
34 View Code Duplication
            if ($this->commandData['virginProxies']) {
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...
35
                $body = Utils::getBodyByVirginProxies($urlOfSearch, $this->listOfVirginProxies[$countProxyVirgin], $this->proxy);
36
37
                //Check if next group of return data or not
38
                $arrLinks = array();
39
                if (!$this->checkCaptcha($body) and $body != 'repeat') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
40
                    $arrLinks = Utils::getLinks($body);
41
                } else {
42
                    --$count;
43
                    //Count the proxys with fail and all fail proxys, finish action
44
                    $countProxyFail[$countProxyVirgin] = $this->listOfVirginProxies[$countProxyVirgin];
45
                    $this->output("You has a problem with proxy, probaly you estress the engenier ...\n");
46
                }
47
48
                //Check if next virgin proxy or repeat of 0
49
                if ($countProxyVirgin == count($this->listOfVirginProxies) - 1) {
50
                    $countProxyVirgin = 0;
51
                } else {
52
                    ++$countProxyVirgin;
53
                }
54
            } else {
55
                $body = Utils::getBody($urlOfSearch, $this->proxy);
56
57
                $arrLinks = Utils::getLinks($body);
58
            }
59
60
            $this->output("\n".$urlOfSearch."\n");
61
62
            $results = Utils::sanitazeLinks($arrLinks);
0 ignored issues
show
Bug introduced by
It seems like $arrLinks can also be of type object<Symfony\Component\DomCrawler\Crawler>; however, Aszone\SearchHacking\Utils::sanitazeLinks() 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...
63
64 View Code Duplication
            if (((count($results) == 0 && $body != 'repeat') && !$this->checkCaptcha($body))
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...
65
                || (count($countProxyFail) == count($this->listOfVirginProxies))) {
66
                $exit = true;
67
            }
68
69
            $resultFinal = array_merge($resultFinal, $results);
70
71
            ++$count;
72
        }
73
74
        return $resultFinal;
75
    }
76
77
    private function checkCaptcha($body)
78
    {
79
        return preg_match("/https:\/\/yandex.ru\/showcaptcha\?/", $body, $matches, PREG_OFFSET_CAPTURE);
80
    }
81
}
82