Passed
Push — master ( c83602...d2aead )
by Nicolas
01:50
created

ConfigurationParser::getUrlTransformer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
ccs 4
cts 4
cp 1
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Cp\Parser;
4
5
use Cp\Transformer\UrlTransformer;
6
use JMS\Serializer\Exception\LogicException;
7
8
/**
9
 * Class ConfigurationParser
10
 */
11
class ConfigurationParser extends AbstractCpParser
12
{
13
    /**
14
     * @var UrlTransformer
15
     */
16
    protected $urlTransformer;
17
18
    /**
19
     * @param string $url
20
     *
21
     * @return string
22
     * @throws \Exception
23
     */
24 3
    public function parseToJson($url)
25
    {
26 3
        $this->loadContent($url);
27 3
        $configurations = $this->parser->find('ul#blocsplans li');
28 3
        if (0 >= $configurations->count()) {
29 1
            throw new \Exception(sprintf('Configuration not found for this url: %s', $url));
30
        }
31
32 2
        $configurationList = [];
33 2
        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...
34 2
            foreach ($conf->find('p a') as $link) {
35 2
                $configurationFor = $this->getUrlTransformer()->reverseConfiguration($link->href);
36 1
                if (null !== $configurationFor) {
37 1
                    $configurationList[] = $configurationFor;
38 1
                }
39 1
            }
40 1
        }
41
42 1
        return json_encode($configurationList, true);
43
    }
44
45
    /**
46
     * @return UrlTransformer
47
     *
48
     * @throws LogicException
49
     */
50 2
    public function getUrlTransformer()
51
    {
52 2
        if (null === $this->urlTransformer) {
53 1
            throw new LogicException('urlTransformer is not set, call setUrlTransformer for it');
54
        }
55
56 1
        return $this->urlTransformer;
57
    }
58
59
    /**
60
     * @param UrlTransformer $urlTransformer
61
     */
62 3
    public function setUrlTransformer(UrlTransformer $urlTransformer)
63
    {
64 3
        $this->urlTransformer = $urlTransformer;
65 3
    }
66
}
67