Test Failed
Push — master ( fa8c20...ab35ba )
by Nicolas
02:05
created

ConfigurationParser::setUrlTransformer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Cp\Parser;
4
use Cp\Transformer\UrlTransformer;
5
6
/**
7
 * Class ConfigurationParser
8
 */
9
class ConfigurationParser extends AbstractCpParser
10
{
11
    /**
12
     * @var UrlTransformer
13
     */
14
    protected $urlTransformer;
15
16
    /**
17
     * @param string $url
18
     *
19
     * @return string
20
     * @throws \Exception
21
     */
22
    public function parseToJson($url)
23
    {
24
        $this->loadContent($url);
25
        $configurations = $this->parser->find('ul#blocsplans li');
26
        if (0 >= $configurations->count()) {
27
            throw new \Exception(sprintf('Configuration not found for this url: %s', $url));
28
        }
29
30
        $configurationList = [];
31
        foreach ($configurations as $conf) {
0 ignored issues
show
Bug introduced by
The expression $configurations of type array|object<PHPHtmlParser\Dom\AbstractNode> 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...
32
            foreach ($conf->find('p a') as $link) {
33
                if (null !== $configurationFor = $this->urlTransformer->reverseConfiguration($link->href))
34
                $configurationList[] = $configurationFor;
35
            }
36
        }
37
38
        return json_encode($configurationList, true);
39
    }
40
41
    /**
42
     * @param UrlTransformer $urlTransformer
43
     */
44
    public function setUrlTransformer(UrlTransformer $urlTransformer)
45
    {
46
        $this->urlTransformer = $urlTransformer;
47
    }
48
}