GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Crawler   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 100
Duplicated Lines 28 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 28
loc 100
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A hasError() 0 19 4
C scraping() 28 28 8
A clearString() 0 4 1
A scrap() 0 5 1

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 namespace zServices\ReceitaFederal\Services\Portais\AN;
2
3
use zServices\Miscellany\Interfaces\CrawlerInterface;
4
use Symfony\Component\DomCrawler\Crawler as BaseCrawler;
5
use zServices\Miscellany\Exceptions\NoSelectorsConfigured;
6
use zServices\Miscellany\Exceptions\InvalidCaptcha;
7
use zServices\Miscellany\Exceptions\ErrorFoundData;
8
9
/**
10
* 
11
*/
12
class Crawler extends BaseCrawler implements CrawlerInterface
13
{
14
15
	/**
16
	 * Selectors to crawler
17
	 * @var array
18
	 */
19
	private $selectors = [];
20
21
	/**
22
	 * Get selectors and initialize crawler in HTML
23
	 * @param string $html      
24
	 * @param array  $selectors
25
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
26
	 */
27
	public function __construct($html, $selectors)
28
	{
29
		$this->selectors = $selectors;
30
31
		parent::__construct($html);
32
	}
33
34
	/**
35
	 * Verifica antes de fazer o crawler se possui erros
36
	 * na requisição
37
	 * @return boolean|null 
38
	 */
39
	public function hasError()
40
	{
41
		if (count($this->selectors) == 0) {
42
			throw new NoSelectorsConfigured("NoSelectorsConfigured", 1);
43
		}
44
45
		// verifica se a página seguida na requisição 
46
		// é página de erro da receita federal
47
		$node = $this->filter($this->selectors['error']);
48
49
		if ($node->count()) {
50
			 throw new ErrorFoundData($this->clearString($node->text()), 1);
51
		}
52
        
53
		// CNPJ informado é válido?
54
		if ($this->filter('#imgCaptcha')->count()) {
55
			throw new InvalidCaptcha('Captcha inválido', 99);
56
		}
57
	}
58
59
	/**
60
	 * Extrai informações do HTML através do DOM
61
	 *
62
	 * @return array
63
	 */
64 View Code Duplication
	public function scraping()
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...
65
	{
66
		$scrapped = [];
67
68
		$this->hasError();
69
70
		foreach ($this->selectors as $name => $selector) {
71
			if (is_string($selector)) {
72
				$node = $this->scrap($selector);
73
74
				if ($node->count()) {
75
					$scrapped[$name] = $this->clearString($node->text());
76
				}
77
			} elseif (is_array($selector)) {
78
				foreach ($selector as $selector => $repeat) {
0 ignored issues
show
Bug introduced by
The expression $selector of type integer|string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
79
					$node = $this->scrap($selector);
80
					if ($node->count()) {
81
						foreach ($node->filter($repeat) as $loop)
82
						{
83
							$scrapped[$name][] = $this->clearString($loop->nodeValue);
84
						}
85
					}
86
				}
87
			}
88
		}
89
90
		return $scrapped;
91
	}
92
93
	/**
94
	 * Limpa o valor repassado
95
	 * @param  string $string
96
	 * @return string
97
	 */
98
	public function clearString($string)
99
	{
100
		return trim(preg_replace(['/[\s]+/mu'], ' ', $string));
101
	}
102
103
	/**
104
	 * Filtra selector no crawler
105
	 */
106
	public function scrap($selector)
107
	{
108
		$node = $this->filter($selector);
109
		return $node;
110
	}
111
}