PlanParser::getPlan()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Cp\Parser;
4
5
/**
6
 * Class PlanParser
7
 */
8
class PlanParser extends AbstractCpParser
9
{
10
    /**
11
     * @param string $url
12
     *
13
     * @return string
14
     * @throws \Exception
15
     */
16 2
    public function parseToJson($url)
17
    {
18 2
        $this->loadContent($url);
19 2
        $weeks = $this->parser->find('#plans table');
20 2
        if (0 >= $weeks->count()) {
21 1
            throw new \Exception(sprintf('Plan not found for this url: %s', $url));
22
        }
23
24 1
        $plan = $this->getPlan();
25 1
        foreach ($weeks as $training) {
0 ignored issues
show
Bug introduced by
The expression $weeks 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...
26 1
            $week = ['name' => $training->find('thead tr')->find('td')[1]->innerHtml];
27 1
            foreach ($training->find('tbody tr') as $seance) {
28
                $training = [
29 1
                    'type' => strip_tags($seance->find('td')[0]->innerHtml),
30 1
                    'content' => strip_tags($seance->find('td')[1]->innerHtml),
31 1
                ];
32 1
                $week['trainings'][] = $training;
33 1
            }
34 1
            $plan['weeks'][] = $week;
35 1
        }
36
37 1
        return json_encode($plan, true);
38
    }
39
40
    /**
41
     * @param string $url
42
     *
43
     * @return string
44
     * @throws \Exception
45
     */
46 1
    public function parseToHtml($url)
47
    {
48 1
        $this->loadContent($url);
49
50 1
        return sprintf('%s%s%s', '<table>', $this->parser->find('#plans table')->innerHtml, '</table>');
51
    }
52
53
    /**
54
     * @return array
55
     */
56 1
    private function getPlan()
57
    {
58 1
        $nameOfPlan = strip_tags($this->parser->find('.article-content-main h1')->innerHtml);
59 1
        $typeOfPlan = strip_tags($this->parser->find('.article-content-main h3')->innerHtml);
60
61
        return [
62 1
            'name' => strip_tags($nameOfPlan),
63 1
            'type' => strip_tags($typeOfPlan),
64 1
            'weeks' => [],
65 1
        ];
66
    }
67
}
68